Android getDeviceLocation()方法更新当前位置标记
我按照官方文档显示谷歌地图并跟踪用户位置。 现在多亏了Android getDeviceLocation()方法更新当前位置标记,android,google-maps,google-maps-api-3,Android,Google Maps,Google Maps Api 3,我按照官方文档显示谷歌地图并跟踪用户位置。 现在多亏了googleMap.setMyLocationEnabled(true)行我可以在地图上显示一个蓝点,它会根据用户位置进行更新。我试图做的是,我想显示一个自定义标记,而不是默认的蓝点 public class tab2_fragment extends Fragment { private static final String TAG = "Tab2Fragment"; MapView mMapView; pr
googleMap.setMyLocationEnabled(true)代码>行我可以在地图上显示一个蓝点,它会根据用户位置进行更新。我试图做的是,我想显示一个自定义标记,而不是默认的蓝点
public class tab2_fragment extends Fragment {
private static final String TAG = "Tab2Fragment";
MapView mMapView;
private GoogleMap googleMap;
private Location mLastKnownLocation;
private final LatLng mDefaultLocation = new LatLng(-33.8523341, 151.2106085);
private boolean zoomed = false;
private boolean firstPass = true;
private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
private boolean mLocationPermissionGranted;
PlaceDetectionClient mPlaceDetectionClient;
FusedLocationProviderClient mFusedLocationProviderClient;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab2_fragment, container, false);
final Context context = getActivity();
getLocationPermission();
// Construct a PlaceDetectionClient.
mPlaceDetectionClient = Places.getPlaceDetectionClient(context, null);
// Construct a FusedLocationProviderClient.
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context);
mMapView = (MapView) rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
// googleMap.setMapStyle(
// MapStyleOptions.loadRawResourceStyle(
//this, R.raw.mapstyle));
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
// Turn on the My Location layer and the related control on the map.
updateLocationUI();
getDeviceLocation();
// Get the current location of the device and set the position of the map.
//googleMap.setMyLocationEnabled(true);
}
});
return rootView;
}
private void getLocationPermission() {
Context context = getActivity();
/*
* Request location permission, so that we can get the location of the
* device. The result of the permission request is handled by a callback,
* onRequestPermissionsResult.
*/
if (ContextCompat.checkSelfPermission(context,
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
} else {
ActivityCompat.requestPermissions(this.getActivity(),
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],
@NonNull int[] grantResults) {
mLocationPermissionGranted = false;
switch (requestCode) {
case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
}
}
}
updateLocationUI();
}
private void updateLocationUI() {
if (googleMap == null) {
return;
}
try {
if (mLocationPermissionGranted) {
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
} else {
googleMap.setMyLocationEnabled(false);
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
mLastKnownLocation = null;
getLocationPermission();
}
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}
private void getDeviceLocation() {
/*
* Get the best and most recent location of the device, which may be null in rare
* cases when a location is not available.
*/
try {
if (mLocationPermissionGranted) {
Task<Location> locationResult = mFusedLocationProviderClient.getLastLocation();
locationResult.addOnCompleteListener(getActivity(), new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful()) {
// Set the map's camera position to the current location of the device.
mLastKnownLocation = task.getResult();
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(mLastKnownLocation.getLatitude(),
mLastKnownLocation.getLongitude()), 20));
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()))
.title("Current Location"));
} else {
Log.d(TAG, "Current location is null. Using defaults.");
Log.e(TAG, "Exception: %s", task.getException());
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
}
}
});
}
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}
}
不,它不是每次用户位置更改时都调用的,因此您需要使用onLocationChanged(location location)当位置更改时调用它。为了更好地理解,请参见此私有静态最终LatLng YOURLOCATION=新LatLng(-37.813144.962);private Marker melbourne=mMap.addMarker(新MarkerOptions().position(YOURLOCATION).title(“墨尔本”).snippet(“人口:4137400”).icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow));请使用此选项在地图上添加自定义图标
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()))
.title("Current Location"));