>我有以下代码在我的应用程序中显示用户位置:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, LocationListener {
private GoogleMap googleMap;
private Marker userMarker;
private Marker destinationMarker;
private Location userLocation;
private Location destinationLocation;
private static final int REQUEST_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(this);
}
private void setupLocationManager() {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (locationManager != null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION
, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE);
}
return;
}
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
onLocationChanged(location);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 5, this);
}
}
}
@Override
public void onMapReady(final GoogleMap googleMap) {
this.googleMap = googleMap;
googleMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng latLng) {
destinationLocation = new Location(LocationManager.GPS_PROVIDER);
destinationLocation.setLatitude(latLng.latitude);
destinationLocation.setLongitude(latLng.longitude);
Polyline line = googleMap.addPolyline(new PolylineOptions()
.add(new LatLng(userLocation.getLatitude(),
userLocation.getLongitude()), new LatLng(destinationLocation.getLatitude(),
destinationLocation.getLongitude())).color(Color.BLUE).width(10).geodesic(true));
destinationMarker = googleMap.addMarker(new MarkerOptions().position(latLng).title("Destination Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_destination_marker)));
}
});
setupLocationManager();
}
@Override
public void onLocationChanged(Location location) {
if (location != null && googleMap != null) {
userLocation = location;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
if (userMarker != null) {
userMarker.remove();
}
userMarker = googleMap.addMarker(new MarkerOptions().position(latLng).title("Your Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_user_marker)));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
googleMap.animateCamera(cameraUpdate);
}else {
Toast.makeText(MainActivity.this,"Location is null",Toast.LENGTH_LONG).show();
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE) {
if (grantResults[0] == 0) {
setupLocationManager();
}
}
}
}
我的清单权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
我有两个问题:
- 首先:我的代码在真实设备中不显示用户位置(应用程序始终 显示位置为空(位置更改((中的吐司消息( I 不知道为什么(已解决(
- 第二:我想在道路上显示我的折线。我知道
isGeodesic()
功能,但当我使用时它没有显示...
在 onMapReady(( 中添加googleMap.setMyLocationEnabled(true);
,这将在谷歌地图上显示用户位置。
@Override
public void onMapReady(final GoogleMap googleMap) {
this.googleMap = googleMap;
final LatLng current_position = new LatLng(latitude, longitude);
MarkerOptions marker = new MarkerOptions().position(current_position).title("XY Address").snippet("XY Address");
googleMap.addMarker(marker);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(current_position, 12));
googleMap.getUiSettings().setMapToolbarEnabled(true);
googleMap.setMyLocationEnabled(true);
}
您确定"位置"为空,因为在您的示例中,如果"googleMap"为空,也可以显示 toast?
如果您的"位置"为空,请确保您在 Android 清单文件中具有以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
然后检查您的位置权限是否已授予您的应用程序(如果您使用的是 API 版本 23 或更高版本的设备,这一点非常重要,但我认为您授予它是因为您正在覆盖"onRequestPermissionsResult"方法(。
如果一切正常,请检查您的设备位置是否已启用,如果没有 - 启用它并再次等待位置更改。