首先平移,然后在Android中缩放到当前位置

时间:2016-04-06 14:14:07

标签: android google-maps

从Android开始,我使用的是Google Maps API演示版的略微修改版本。单击MyLocationButton时的默认行为是Android平移到当前位置。我刚添加了一行来放大。

@Override
public boolean onMyLocationButtonClick() {
    Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
    // Return false so that we don't consume the event and the default behavior still occurs
    // (the camera animates to the user's current position).
    float zoomLevel = (float) 17.0;
    mMap.moveCamera(CameraUpdateFactory.zoomTo(zoomLevel));
    return false;
}

这样做,应用程序缩放到指定的级别然后平移到该位置。但我更喜欢Pan 首先,然后是Zoom。在我看来,我可以通过

来实现这一目标
  1. 从中获取当前位置 onMyLocationButtonClick匿名函数,或
  2. 在发生默认行为后运行代码
  3. 可悲的是,我不知道如何做其中任何一件事。

1 个答案:

答案 0 :(得分:0)

1)你无法调查"匿名"功能,但你知道它做了什么。你需要的是你当前的位置,你可以看看getting the last known location,然后,当你点击你的按钮时,你移动/"动画"相机到LatLng,然后使用你的.zoomTo。不要忘记将您的返回值更改为return true;,以便您的locationbuttonclick事件不会传递给此匿名'功能amymore。

2)不是实现步骤(1)所必需的位置管理器,而是可以在摄像机位置发生变化后,每次都向地图添加一个camerachangeListener并添加ur zoomTo方法。单击按钮时,它将自动触发onCameraChange事件,然后放大。这里有一些示例代码:

mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {
            // (the camera animates to the user's current position).
            float zoomLevel = (float) 17.0;
            if (buttonClicked){
                mMap.moveCamera(CameraUpdateFactory.zoomTo(zoomLevel));
                buttonClicked = false;
            }
        }
    });

其中mMap是你在mapMap中的googlemap ... 不幸的是,如果相机发生变化,您的相机将始终放大,例如当您的用户滚动地图时。为了防止这种情况,您可以使用布尔类变量 buttonClicked 来指示单击按钮的时间:

@Override
public boolean onMyLocationButtonClick() {
     Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
     buttonClicked = true;
     return false;
 }