Android 如何在不更改用户设置的缩放的情况下更新地图标记?
我正在构建一个Android应用程序,它使用带有位置侦听器的谷歌地图。当地图第一次出现时,我在location listener中将缩放设置为12,我是Google Maps开发的新手,所以我想知道,一旦用户点击更改缩放,如何在不影响缩放的情况下更新位置?下面是我的位置侦听器Android 如何在不更改用户设置的缩放的情况下更新地图标记?,android,google-maps-api-3,google-maps-markers,currentlocation,Android,Google Maps Api 3,Google Maps Markers,Currentlocation,我正在构建一个Android应用程序,它使用带有位置侦听器的谷歌地图。当地图第一次出现时,我在location listener中将缩放设置为12,我是Google Maps开发的新手,所以我想知道,一旦用户点击更改缩放,如何在不影响缩放的情况下更新位置?下面是我的位置侦听器 /** *Mylocationlistener class will give the current GPS location *with the help of Location Listener interfa
/**
*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();
}
}
您可以在侦听器中添加一个局部变量,并使用它仅缩放第一个位置。代码如下所示:
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();
}
}
您可以在侦听器中添加一个局部变量,并使用它仅缩放第一个位置。代码如下所示:
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));
尝试使用以下方法:
map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLocationMarker.getPosition(), map.getCameraPosition().zoom));
你一定是新来的。这是多年前的回答。你一定是新来的。这是多年前的回答。