How to center my location by clicking on a Marker?

Asked

Viewed 140 times

1

Hi!

I am developing an application that works on Google Maps API v2 and I have defined the method mMap.setMyLocationEnabed(true) to obtain the user’s location.

My question is: how do I, when clicking on this blue dot that appears, center the map?

  • http://stackoverflow.com/questions/13904505/how-to-get-center-of-map-for-v2-android-maps
 here explains, a guy had the same doubts as you do.

  • This sets the center of the map, but this I had already set. I had already set the center of the map to appear only one country. But there is no way to automatically zoom in on the blue dot? so the user doesn’t have to zoom in on himself?

1 answer

1

Follows:

LatLng isMe = new LatLng(mMap.getMyLocation().getLatitude(), map.getMyLocation().getLongitude());

    final CameraPosition position = new CameraPosition(isMe, ZOOM_APROXIMADO, map.getCameraPosition().tilt, map.getCameraPosition().bearing);
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(position));

I hope I’ve helped!

Cordial Greetings,

Correction as commented:
To maintain the standard, the way Voce did :

LatLng isMe = new LatLng(mGoogleMap.getMyLocation().getLatitude(), mGoogleMap.getMyLocation().getLongitude());  
    CameraPosition cp = new CameraPosition.Builder().target(isMe).zoom(5).build();  
    mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp)); 

Let’s just change the fixed position, to its Position on the Map (the Blue Dot).

  • Failed to launch the app... Where have "ZOOM_APROXIMADO" should I change by a value, for example 5? And I think the variable "map" should be the same as "mmap"...

  • Exact! And ZOOM_APROXIMADO is a numeric value. Change to 13, for example

  • I had like this and the app ran and showed the map... LatLng currentLatLng = new LatLng(40.4830101,-4.0875573); 

 CameraPosition cameraPosition = new CameraPosition.Builder()
 .target(currentLatLng)
 .zoom(5)
 .build();
 mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); After your suggestion gave the error in the logcat: "Could not find class 'android.app.Appopsmanager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza" You know the reason why.?

  • What is the targetSdkVersion of your application ?? If it is less than 19 this may be the problem!

  • I edited the answer, to maintain the standard you had already made!

  • Now gives a Fatal Exception: Main error java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.locaisproximos/in.wptrafficanalyzer.locaisproximos.MainActivity}: java.lang.IllegalStateException: MyLocation layer not enabled My phone has API 16, I already changed in Androidmanifest.xml the targetSdkVersion to 19. What will be the error now?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.