5

im having a weird problem with Google maps for android.

All is working fine, but when the user starts the activity with the map, i want to zoom in and position the camera on a especified location.

this is my code:

    LatLng l = new LatLng(40.446951,-3.693295);
    defaultPos = mapa.addMarker(new MarkerOptions().position(new LatLng(l.latitude, l.longitude)).icon(BitmapDescriptorFactory.defaultMarker((float) 250.0)));
    Log.d("mappoi","zoom to");
    mapa.animateCamera(CameraUpdateFactory.zoomTo(17.0f));
    mapa.animateCamera(CameraUpdateFactory.newLatLng(l));

So, as you can see, im creating a new LatLng object and using it to create a Marker on the map, then i want to center camera on that position and zoom in.

I dont know why, but the zoom only works the first time it gets executed, so when i fresh install my app it works fine, but if i go back and restart the activity, it doesnt do any zoom and keeps the default zoom level, even if i close and restart my app it doesnt work anymore, only if i uninstall and reinstall again it works again, but only once.

I cant imagine whats going wrong, if it works first time why not the next?

Thanks for any help.

EDIT: My activity is getting killed when i press back button, so onCreate is called every time.

Nanoc
  • 2,381
  • 1
  • 20
  • 35

3 Answers3

6

Thanks to gnuanu comment for pointing me to:

How to directly move camera to current location in Google Maps Android API v2?

The solution was to use:

CameraUpdateFactory.newLatLngZoom()

instead of

CameraUpdateFactory.zoomTo()
CameraUpdateFactory.newLatLng()

Maybe newLatLng was changing the zoom level.

Community
  • 1
  • 1
Nanoc
  • 2,381
  • 1
  • 20
  • 35
0

It's not clear from your question that, in your activity from where you execute the above mentioned code. I presume that you are executing the code from the onCreate() function. In that case, instead of pressing "Back" button to exit the App, try killing the app and launch it again and check whether it works or not.

Or if you need the map to zoom in every time you launch the activity, move the code to onResume() function of your activity.

gnuanu
  • 2,252
  • 3
  • 29
  • 42
  • Yeah, that code its at the end of onCreate(), i´ve said that killing the app and restarting doesnt work, i´ve moved that code to onResume() and still not working. – Nanoc Oct 03 '13 at 08:15
  • 1
    Similar question with code sample is here, give it a try http://stackoverflow.com/a/13905821/1230782 – gnuanu Oct 03 '13 at 08:29
  • The solution was to use newLatLngZoom() instead of both newLatLng() and zoomTo() maybe newLatLng is changing zoom lvl, i dont know, its working fine now. – Nanoc Oct 03 '13 at 08:36
-1

You should move your code (Any code which manipulate the map) to onResume() method of activity. then it will work fine.