95

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.

Moh Sakkijha
  • 2,705
  • 1
  • 14
  • 19
dong221
  • 3,390
  • 6
  • 29
  • 31
  • This qustion helped.... Even if location is not enabled ...animateCamera points to somewhere in africa ... wiered ! ...`moveCamera` is the best solution for this – Devrath May 20 '15 at 10:41
  • 3
    hahahhaha.......+1 for this question, you asked my heart out . :D..."it always start at a fixed location (near Africa)"........rofll.....thanks even though. – akash89 Jun 25 '15 at 12:42
  • 2
    @akash89 It starts from LatLng(0.0,0.0) as expected: https://www.google.com/maps/@0,0,5z I'm not sure what makes you roll on the floor laughing literally (if I'm not mistaken), but I'm happy that you are happy :) – Positive Navid Apr 16 '20 at 03:52

5 Answers5

179

you can use this to zoom directly without the animation :

map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(xxxx,xxxx) , 14.0f) );
Moh Sakkijha
  • 2,705
  • 1
  • 14
  • 19
  • i have minimum latitude/longitude and maximum latitude/longitude then how to set??? – Pratik Butani Jul 29 '13 at 06:55
  • you can add new question for that so i can post the full answer for you, but in short you will have to use LatLngBounds.Builder to build bounds and use that. – Moh Sakkijha Jul 30 '13 at 06:04
  • 1
    @Saksak My problem is that I am waiting on the GPS to grab `MyLocation` so that I can move and zoom the map to the user's location. Since the map is initialized in the XML, I can't figure out how to keep the jump from happening. Ideally, I would hide the map until `MyLocation` is found then show it. – theblang Nov 25 '13 at 15:24
  • when i placed map it gave me an error and told me to create a variable 'map'. is it possible to import or something? – Hanzawa Naoki Dec 16 '14 at 14:45
  • @huicheese map is the name of your instance of GoogleMap, replace it with the name you have. – Moh Sakkijha Dec 17 '14 at 15:34
  • One can also replace `moveCamera` with `animateCamera` for a smooth zoom – Pierre May 30 '17 at 12:47
  • animate camera is nice, but if your map is just initialised, it smooth-zooms very slowly from fully zoomed out, try a mixture – Saik Caskey Sep 06 '17 at 16:33
93

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));
gyamana
  • 1,202
  • 1
  • 16
  • 28
Anthony
  • 4,720
  • 1
  • 22
  • 9
6

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.

Jaydipsinh Zala
  • 16,558
  • 7
  • 41
  • 45
3

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.

theblang
  • 10,215
  • 9
  • 69
  • 120
2

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);
Mahesh Gawhane
  • 348
  • 3
  • 20