2

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

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?


}
4

2 回答 2

2

像这样

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

     map.animateCamera(CameraUpdateFactory.newCameraPosition(position));

文档

于 2013-05-04T19:12:57.633 回答
1

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

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

于 2013-05-04T19:18:08.610 回答