如何设置谷歌地图 api v2的默认位置和缩放级别?

当我的地图显示,它总是开始在一个固定的位置(非洲附近)。

然后,我使用下面的代码将地图定位到我想要的位置。

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(loc.getLatitude(), loc.getLongitude()), 14.0f));

我的问题是,我可以设置一个默认的位置和缩放水平之前,地图显示?

因为我不希望我的用户在开始时看到动画。

谢谢。

101426 次浏览

you can use this to zoom directly without the animation :

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

Take a look at the documentation here:

https://developers.google.com/maps/documentation/android-api/map#configure_initial_state

The way to do it is slightly different depending on whether you are adding the map via XML or programmatically. If you are using XML, you can do it as follows:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
map:cameraBearing="112.5"
map:cameraTargetLat="-33.796923"
map:cameraTargetLng="150.922433"
map:cameraTilt="30"
map:cameraZoom="13"/>

If you are doing it programmatically, you can do the following:

CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(-33, 150))
.zoom(13)
.build();
MapFragment.newInstance(new GoogleMapOptions()
.camera(camera));

I created a handle to SupportMapFragment and set its visibility to View.INVISIBLE using SupportMapFragment.getView().setVisibility(). Then, in the onLocationChanged callback that my class implements, I check if INVISIBLE and set to VISIBLE if true. This gets rid of the jumping you see on load while allowing you to dynamically initialize the starting position. In my case I am centering the camera around the user's location, so onLocationChanged is called immediately because of setMyLocationEnabled(true).

Your requirements may be different, but either way you just need to set the visibility to INVISIBLE and then find a good place to set VISIBLE after your appropriate data has been loaded.

you can use directy CameraUpdate using static latlong

 LatLong latlong = new LatLong(lat, long);
CameraUpdate cameraPosition = CameraUpdateFactory.newLatLngZoom(latLong, 15);
mGoogleMap.moveCamera(cameraPosition);
mGoogleMap.animateCamera(cameraPosition);

Just in case you want to load Google Map at any initial Location programmatically then you can use this piece of code,

FragmentManager fm = getFragmentManager(); // getChildFragmentManager inside fragments.
CameraPosition cp = new CameraPosition.Builder()
.target(initialLatLng) // your initial co-ordinates here. like, LatLng initialLatLng
.zoom(zoom_level)
.build();
SupportMapFragment mapFragment = SupportMapFragment.newInstance(new GoogleMapOptions().camera(cp));
fm.beginTransaction().replace(R.id.rl_map, mapFragment).commit();

Add this piece of code for layout

<RelativeLayout
android:id="@+id/rl_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

This will load GoogleMap at particular Location directly i.e, initialLatLng.