即使仅移动地图也要调用onLocationChanged

时间:2020-02-07 14:58:40

标签: android android-maps-v2 android-maps

locationListener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    centerOnMapLocation(location,"Your location");
                }

仅当我从onLocationChanged在模拟器中设置位置,即更改用户位置时,才应调用此方法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单击设置位置时,相机和标记器将移动到新位置。没关系。但是之后,当我尝试在地图中移动时,一旦我停止触摸屏幕,相机就会再次移回标记。

基本上,当我在地图屏幕上滑动以在地图内移动时,只要我的幻灯片停止(意味着我停止触摸屏幕),相机就会再次移至标记。我希望标记保持在那里,并且应该能够像往常一样在地图内移动

为什么相机仅在位置更改而不是地图屏幕更改时移动

1 个答案:

答案 0 :(得分:1)

以下行为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

相关问题