Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android google map marker placing

In my Android application I need to place marker at exact position on map. I pin a marker on map with 51.507351, -0.127758 (London) location. I used the following code to do the work.

googleMap.addMarker(new MarkerOptions().position(
        new LatLng(51.507351, -0.127758)).icon(
        BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(),
                        R.drawable.q_icon))));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
        new LatLng(51.507351, -0.127758), 20));

And this my marker drawable image:

enter image description here

Now my problem is, the "Q" symbol is placed at the location of 51.507351, -0.127758. I need to place marker at the position where bottom arrow starts.

Please see the picture so that you can understand my question.

How can I do this? please help me.

enter image description here

like image 729
M.A.Murali Avatar asked Sep 07 '15 11:09

M.A.Murali


People also ask

How do I add a marker on Google Maps Android?

For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.

How do I move a marker smoothly on Google Maps?

The transition() and moveMarker() are used to move marker smoothly on click on the Google map.


3 Answers

The logic behind how the markers are anchored is something like this for a 4px-by-2px image:

        0,0      0.5,0.0        1,0
        *-----+-----+-----+-----*
        |     |     |     |     |
        |     |     |     |     |
  0,0.5 +-----+-----+-----+-----+ 1,0.5
        |     |     |   X |     |           (U, V) = (0.7, 0.6)
        |     |     |     |     |
        *-----+-----+-----+-----*
        0,1      0.5,1.0        1,1

Also take into consideration that based on your bitmap resource, it could be positioned a little different than you would expect, because they actually approximate to the nearest snap position. So in the example above, your anchor points will snap to this position:

 *-----+-----+-----+-----*
 |     |     |     |     |
 |     |     |     |     |
 +-----+-----+-----X-----+   (X, Y) = (3, 1)
 |     |     |     |     |
 |     |     |     |     |
 *-----+-----+-----+-----*

Documentation

like image 144
Ionut Negru Avatar answered Oct 19 '22 04:10

Ionut Negru


Use MarkerOption's anchor method: https://developers.google.com/android/reference/com/google/android/gms/maps/model/MarkerOptions.html#anchor(float,%20float)

For your case:

MarkerOptions marker = new MarkerOptions()
.anchor(0.5f, 1.0f)
// Rest of the properties follow
like image 21
jlhonora Avatar answered Oct 19 '22 05:10

jlhonora


Try this one:

googleMap.addMarker(new MarkerOptions().position(
                new LatLng(51.507351, -0.127758)).icon(
                BitmapDescriptorFactory.fromBitmap(BitmapFactory
                        .decodeResource(getResources(),
                                R.drawable.q_icon))).anchor(0.5f, 1f));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
    new LatLng(51.507351, -0.127758), 20));
like image 1
Vitalii Obideiko Avatar answered Oct 19 '22 03:10

Vitalii Obideiko