简体   繁体   中英

How do I set default location and Zoom level for google map api v2?

When my map shows, it always start at a fixed location (near Africa).

Then, I use the following code to center the map to the location I want.

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

My question is that could I set a default location and zoom level before the map shows?

Because I don't want my users to see the animation at the beginning.

Thanks.

你可以使用它来直接缩放而不需要动画:

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));

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 ie, initialLatLng.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM