简体   繁体   中英

Center a map marker in Android

I use the following code to display a single marker at a zoom level but it doesn't center tha marker on the map. Only one marker will ever be shown:

LatLng latLng = new LatLng(Latitude, Longitude);
cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 11.0f);

This code will center it but it doesn't provide any zoom:

LatLngBounds bounds = latLngBuilder.build();
cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 30);

I need to center AND zoom.

Try the following

LatLng coordinate = new LatLng(Latitude, Latitude);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 11.0f);
map.animateCamera(yourLocation);

You could also do this (cleaner way)

CameraPosition cameraPosition = new CameraPosition.Builder()
    .target(Latitude, Latitude) // Center Set
    .zoom(11.0f)                // Zoom
    .bearing(90)                // Orientation of the camera to east
    .tilt(30)                   // Tilt of the camera to 30 degrees
    .build();                   // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

From https://developers.google.com/maps/documentation/android/views?hl=fr-FR#moving_the_camera

The correct value for zoom is between 2.0 and 22.00.

After this you've to add this line

GoogleMap mMap;
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latlng, 10));

About zoom you can read this from documentation zoom: the desired zoom level, in the range of 2.0 to 21.0. Values below this range are set to 2.0, and values above it are set to 21.0. Increase the value to zoom in. Not all areas have tiles at the largest zoom levels.

You can check this on http://developer.android.com/reference/com/google/android/gms/maps/CameraUpdateFactory.html

this is because you need to move you camera to the CameraUpdateFactory you created, like this:

LatLng latLng = new LatLng(Latitude, Longitude);
map.animateCamera(CameraUpdateFactory.newLatLng(latLng, 11);

if you don't want the animation, then you could just use:

map.moveCamera(CameraUpdateFactory.newLatLng(latLng, 11);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM