【问题标题】:Zoom GoogleMap to my location将 GoogleMap 缩放到我的位置
【发布时间】:2013-05-04 19:06:53
【问题描述】:

我有一个SherlockFragmentActivity,它使用Google Maps Android API v2 显示GoogleMap。我希望在片段启动后立即将地图缩放到我当前的位置。我在documentation 中找不到这样的方法。如何做到这一点?

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ui_settings_demo);
    setUpMapIfNeeded();
}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map))
                .getMap();
        if (mMap != null) {
            setUpMap();
        }
    } 
}

private void setUpMap() {
    mMap.setMyLocationEnabled(true);
    //How to zoom to my current location?


}

【问题讨论】:

    标签: android google-maps google-maps-android-api-2


    【解决方案1】:

    喜欢这个

    CameraPosition position = new CameraPosition.Builder()
            .target(new LatLng(Lat,Lon))
            .zoom(zoom).build();
    
         map.animateCamera(CameraUpdateFactory.newCameraPosition(position));
    

    Docs

    【讨论】:

      【解决方案2】:

      除非您已经知道自己的位置,否则这是不可能的,因为找到该位置需要时间。

      setMyLocationEnabled(true) 仅允许用户选择在其位置上居中/缩放地图。如果您想自己强制,则必须使用LocationManager 自己找到位置并相应地设置相机位置。或者将两者结合起来,就像我在 this sample project 中所做的那样。但是,这需要一些时间。

      【讨论】:

      • 我已经知道位置,我只是删除了一些行。你的代码很有帮助。我会检查一下。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2014-04-16
      • 2013-03-30
      • 2016-01-06
      • 1970-01-01
      • 2013-04-29
      • 2020-05-28
      • 1970-01-01
      • 2014-05-16
      相关资源
      最近更新 更多