Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center marker icon (Android)

I realized that setting the icon on a marker, it positions the LatLng center on the bottom of the icon drawable. I Set the marker like this:

BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.mi_posicion_marker);
mMarkerMiPosicion = mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("I am here!").icon(icono));

How do I do to "center" the marker position on the center of the icon drawable?

like image 717
IIRed-DeathII Avatar asked Aug 19 '15 14:08

IIRed-DeathII


1 Answers

you have to use the "Anchor" parameter(s) to select the position: https://developers.google.com/android/reference/com/google/android/gms/maps/model/MarkerOptions.html#anchor(float, float)

Specifies the anchor to be at a particular point in the marker image.

The anchor specifies the point in the icon image that is anchored to the marker's position on the Earth's surface.

The anchor point is specified in the continuous space [0.0, 1.0] x [0.0, 1.0], where (0, 0) is the top-left corner of the image, and (1, 1) is the bottom-right corner. The anchoring point in a W x H image is the nearest discrete grid point in a (W + 1) x (H + 1) grid, obtained by scaling the then rounding. For example, in a 4 x 2 image, the anchor point (0.7, 0.6) resolves to the grid point at (3, 1).

at the provided link there is an ASCII image that explains well how it is managed. For your specific question;

BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.mi_posicion_marker);
mMarkerMiPosicion = mMap.addMarker(
    new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude()))
    .title("I am here!").icon(icono).anchor(0.5f,0.5f));
like image 183
N Dorigatti Avatar answered Sep 29 '22 09:09

N Dorigatti