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

AndroidGoogle MapsGoogle Maps-Api-3

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

you can use this to zoom directly without the animation :

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

Solution 2 - Android

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

Solution 3 - Android

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.

Solution 4 - Android

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.

Solution 5 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondong221View Question on Stackoverflow
Solution 1 - AndroidMoh SakkijhaView Answer on Stackoverflow
Solution 2 - AndroidAnthonyView Answer on Stackoverflow
Solution 3 - AndroidJaydipsinh ZalaView Answer on Stackoverflow
Solution 4 - AndroidtheblangView Answer on Stackoverflow
Solution 5 - AndroidMahesh GawhaneView Answer on Stackoverflow