I have a Android App I am building that uses Google Maps with a location listener. When the map first comes up I have the zoom set to 12 in the location listener, I am fairly new at Google Maps development so I was wondering how I can update the location without affecting the zoom once the user has pinched to change the zoom? Below is my location listener.
/**
*Mylocationlistener class will give the current GPS location
*with the help of Location Listener interface
*/
private class Mylocationlistener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
// ---Get current location latitude, longitude---
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
currentLatLng = new LatLng(location.getLatitude(), location.getLongitude());
Marker currentLocationMarker = map.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
if (!firstPass){
currentLocationMarker.remove();
}
firstPass = false;
Toast.makeText(MapViewActivity.this,"Latitude = "+
location.getLatitude() + "" +"Longitude = "+ location.getLongitude(),
Toast.LENGTH_LONG).show();
}
}
You can add a local variable in your listener and use it to zoom only for the first location. The code would look like:
private class Mylocationlistener implements LocationListener {
private boolean zoomed = false;
@Override
public void onLocationChanged(Location location) {
if (location != null) {
// ---Get current location latitude, longitude---
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
currentLatLng = new LatLng(location.getLatitude(), location.getLongitude());
Marker currentLocationMarker = map.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 15));
// Zoom in, animating the camera.
if (!zoomed) {
map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
zoomed = true;
}
if (!firstPass){
currentLocationMarker.remove();
}
firstPass = false;
Toast.makeText(MapViewActivity.this,"Latitude = "+
location.getLatitude() + "" +"Longitude = "+ location.getLongitude(),
Toast.LENGTH_LONG).show();
}
}
尝试使用这个:
map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLocationMarker.getPosition(), map.getCameraPosition().zoom));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.