【问题标题】:while moving in camera by dragging it always moves to current location every second通过拖动在相机中移动时,它总是每秒移动到当前位置
【发布时间】:2017-04-05 11:35:09
【问题描述】:

通过拖动移动到地图时,它总是每秒移回当前位置,我无法在我所在位置附近的地图中看到视图,如何阻止它每秒移动到当前位置?只有当我的位置按钮被点击时,我才想移动到我的位置.. 那么任何人都可以帮助我吗?提前致谢。我的代码是

@Override
public void onLocationChanged(Location location)
{
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.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_MAGENTA));
mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
*/

CameraUpdate center=
CameraUpdateFactory.newLatLng(latLng);
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15.0f);

mGoogleMap.moveCamera(center);
mGoogleMap.animateCamera(zoom);
//move map camera
// mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,15.0f));

}

【问题讨论】:

    标签: android camera zooming move android-maps-v2


    【解决方案1】:

    我已经实现了一个条件来实现您的目标。第一次调用onLocationChanged() 时,它会将相机移动到当前位置。之后,它将每秒捕获当前位置并将其存储到mLastLocation = location;,但它不会移动相机。当用户按下按钮时,它将从mLastLocation 中获取LatLong,并根据需要将相机移动到当前位置。

    boolean isFirstTime = true;   
    
        @Override
    public void onLocationChanged(Location location)
    {
       mLastLocation = location;
       if (mCurrLocationMarker != null) {
         mCurrLocationMarker.remove();
       }
    
      if(isFirstTime){
        //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_MAGENTA));
        mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
        */
    
        CameraUpdate center=
        CameraUpdateFactory.newLatLng(latLng);
        CameraUpdate zoom=CameraUpdateFactory.zoomTo(15.0f);
    
        mGoogleMap.moveCamera(center);
        mGoogleMap.animateCamera(zoom);
        //move map camera
       // mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,15.0f));
       isFirstTime = false;
      }
    
    }
    

    打开按钮点击:

    btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                     LatLng latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
                     CameraUpdate center=
                     CameraUpdateFactory.newLatLng(latLng);
                     CameraUpdate zoom=CameraUpdateFactory.zoomTo(15.0f);    
                     mGoogleMap.moveCamera(center);
                     mGoogleMap.animateCamera(zoom);
                }
            });
    

    【讨论】:

    • 那么我在哪里可以将该代码放置一次以显示当前位置?
    • 但我想在第一次加载时显示当前位置一次
    • 在 onLocationChanged 监听器中查看stackoverflow.com/a/13905821/7784663
    • 但是当我们在地图上或任何其他位置移动时,这不会更新相机和移动相机
    • 检查我更新的答案。它将检查 如果它是第一次加载活动 。如果是 - 然后 移动相机onLocationChanged() 不断获取当前位置更新并将其存储到`mLastLocation。首次加载后,仅当用户单击按钮到当前位置时,相机才会移动。希望这就足够了。
    【解决方案2】:

    这很容易。我总是在地图上标记新位置。

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == PLACE_PICKER_REQUEST) {
                if (resultCode == RESULT_OK) {
    
                    Place place = PlacePicker.getPlace(this,data);
    
                    CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(place.getLatLng())
                            .zoom(17).build();
    
                    Marker marker = mMap.addMarker(new MarkerOptions().position(place.getLatLng()).title(place.getName().toString()));
                    markers.add(marker);
    
                    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    
                }
            }
    

    我将相机移动到标记位置。让我知道它是否有效,我会帮助你:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-09
      • 2018-09-13
      • 2017-10-09
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 2016-06-15
      相关资源
      最近更新 更多