[英]Disable auto zoom in google map
我已經制作了一個Android應用程序,用於顯示用戶的當前位置。
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (currLocationMarker != null) {
currLocationMarker.remove();
}
//Place current location marker
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
currLocationMarker = mGoogleMap.addMarker(markerOptions);
mGoogleMap.animateCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()) , 6.0f) );
}
它為我提供了帶有標記的當前位置,但是問題是,每當我嘗試從當前位置縮小時,它會在幾秒鍾內再次聚焦到同一位置(當前),因此我看不到標記上的其他點(標記)地圖。
如何在Google地圖中禁用此自動放大功能?
僅在第一次將相機對准當前位置:
private boolean firstLoad = true;
在您的onLocationChanged
:
if (firstLoad) {
mGoogleMap.animateCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()) , 6.0f) );
firstLoad = false;
}
您應該僅第一次使用animateCamera方法。 您可以使用布爾值只調用一次。 全局聲明一個布爾值並將其設置為false,一旦調用此方法,請將其設置為true,然后再調用此cameraAnimate方法,檢查布爾值是否為false,然后僅使用它。
聲明一個全局布爾標志
boolean isFisrtTime = true;
更換
mGoogleMap.animateCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()) , 6.0f) );
與
if(isFisrtTime){
mGoogleMap.animateCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()) , 6.0f) );
isFisrtTime = false; //this is will work at start only
}
您的代碼是否在onMapReady
方法上onMapReady
? 如果不考慮將其移到那里。 我認為您在onLocationChanged
中調用animateCamera
,並且每次位置更新和地圖重新聚焦到您的位置時都會調用代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.