locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
centerOnMapLocation(location,"Your location");
}
仅在仿真器中从onLocationChanged
的i 设置位置,即更改用户位置时,才应调用[[此方法extented controls
]。
public void centerOnMapLocation(Location location, String title){
if(location!=null) {
LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.clear();
mMap.addMarker(new MarkerOptions().position(userLocation).title(title));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 12));
}
}
centerOnMapLocation
将相机移动到用户所在的位置。当我从仿真器中的extented controls
单击设置位置时,摄像机和标记器将移动到新位置。没关系。但是之后,当我尝试在地图中移动时,一旦我停止触摸屏幕,相机就会再次移回标记。
基本上,当我在地图屏幕上滑动以在地图内移动时,只要我的幻灯片停止(意味着我停止触摸屏幕),相机就会再次移至标记。我希望标记保留在那里,并且应该能够像往常一样在地图内移动
为什么当摄像机仅在位置更改时移动而不在地图屏幕更改时移动
以下行提供对locationLister的位置更新。
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
要求位置更新时的第三个参数是minimum distance
,直到位置更新为止。在以上情况下,当传递0
时,它将连续更新位置,并且locationLister
将其作为新位置,然后cameraZoom
将被更新。
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,1,locationListener);
要解决此问题,只需将minimum distance
的值更改为任意值,此处为1
。现在,在更改位置之前,它不会调用centerOnMapLocation
。