无法放大以上全国,java谷歌地图V2

时间:2015-04-20 14:59:45

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

我正在开发一个项目,其中的地图在一个国家(希腊)显示100个标记。当我的地图在我的移动Android手机中启动应用程序时打开,地图放大非洲和平板电脑设备,它放大不同的位置国家。通过我的设备设置禁用我的位置时会发生这种情况。当我的地图首次加载时,如何将地图设置为在整个希腊国家上方放大?在任何Android设备应用程序安装?

请检查下面的代码:

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

}

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

/**
 * Check mMap. In case it is not null then setUpMap
 */
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();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

当我激活当前位置时,它会在我的位置上放大第14级。启用位置设置时,我需要保留此功能。

// Show the current location in Google Map
            mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

            // Zoom in the Google Map
            mMap.animateCamera(CameraUpdateFactory.zoomTo(14));

2 个答案:

答案 0 :(得分:1)

你可以这样做:

map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(xxxx,xxxx) , 14.0f) );

其中coords,例如" middle"希腊您需要设置一些SharedPreference以了解应用程序是否第一次打开。

答案 1 :(得分:0)

您可以使用加载地图时调用您的新版API: http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.OnMapLoadedCallback.html 您可以通过使用简单的“单例”或静态变量轻松检查它是否是第一次。

关于Zoom,我建议你构建一个LatLngBounds并放大它。 您可以通过迭代所有标记来完成此操作:

LatLngBounds.Builder zoomTo = LatLngBounds.builder();
for (Marker marker : markersList) {
     zoomTo.include(marker.getPosition());
}
final LatLngBounds workArea = zoomTo.build();
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
     @Override
     public void onMapLoaded() {
           mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(workArea, 50));
           mMap.setOnMapLoadedCallback(null);
         }
 });