Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align the icon of a marker in google map

In google map usually the center bottom of the image of the marker is the lat lng of a point that it has to mark.

Imagine my marker icon is a circle, I would like the center of it to be at the lat-lng of the given point...

Code:

var image_hotspot = 'img/icons/bank_euro.png'; var marker = new google.maps.Marker({       map: map,       position: placeLoc,       icon: image_hotspot     }); 
like image 810
illinois Avatar asked Aug 23 '12 13:08

illinois


People also ask

How do I get a marker position on Google Maps?

You can add a simple marker to the map at a desired location by instantiating the marker class and specifying the position to be marked using latlng, as shown below.

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.

How do I change my marker position?

Call the changeMarkerPosition() function and pass the marker object in it. The setPosition() will change the marker position on google map based on the specified latitude and longitude.

How do you customize a marker in maps?

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.


2 Answers

What You need is to create a MarkerImage object, for example:

var markerImage = new google.maps.MarkerImage('img/icons/bank_euro.png',                 new google.maps.Size(30, 30),                 new google.maps.Point(0, 0),                 new google.maps.Point(15, 15)); 

Where first parameter is an image url, second is image size, third is origin point of image, and fourth is position on the image where marker should point. If your marker is a circle then set fourth parameter to center of the image. And create your marker passing markerImage to icon property:

var marker = new google.maps.Marker({   map: map,   position: placeLoc,   icon: markerImage }); 
like image 169
WojtekT Avatar answered Oct 03 '22 14:10

WojtekT


From the docs:

Converting MarkerImage objects to type Icon

var image = new google.maps.MarkerImage(     place.icon,     new google.maps.Size(71, 71),     new google.maps.Point(0, 0),     new google.maps.Point(17, 34),     new google.maps.Size(25, 25)); 

becomes

var image = {   url: place.icon,   size: new google.maps.Size(71, 71),   origin: new google.maps.Point(0, 0),   anchor: new google.maps.Point(17, 34),   scaledSize: new google.maps.Size(25, 25) }; 

https://developers.google.com/maps/documentation/javascript/markers

like image 37
Sabino Velasquez Avatar answered Oct 03 '22 14:10

Sabino Velasquez