Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect behaviour with zoom on Android Google maps

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.

like image 878
Nanoc Avatar asked Oct 03 '13 07:10

Nanoc


People also ask

How do I fix zoom on Google Maps?

You can change the zoom level of the map using simple steps. Step 1 Go to Add or Edit Map page . Step 2 Select 'Default zoom level' in the 'Map Information section'. Step 3 click save map and see the changes.

Can you use Google Maps and zoom at the same time?

Google Maps - cannot zoom and move at the same time with animateCamera, but can with moveCamera.

Why is my Google Maps acting weird?

There are various reasons why this happens. It's possible the location accuracy option is disabled, you're using an older version of the app, or you don't have proper access to the internet. The Google Maps app itself may have issues, too. Cache files and other app data can sometimes cause various issues with the app.


1 Answers

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.

like image 131
Nanoc Avatar answered Oct 08 '22 09:10

Nanoc