2012-08-23 44 views
26

在谷歌地圖中,​​標記圖像的中心底部通常是其必須標記點的緯度。如何對齊谷歌地圖上的標記圖標

想象我的標記圖標是一個圓,我想它的中心是在給定的點的緯度和經度...

代碼:

var image_hotspot = 'img/icons/bank_euro.png'; 
var marker = new google.maps.Marker({ 
     map: map, 
     position: placeLoc, 
     icon: image_hotspot 
    }); 

回答

34

你什麼需要的是創建一個對象MarkerImage,例如:

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)); 

其中第一個參數是一個圖像的URL,第二是圖像大小,第三是或igin點的圖像,第四個是標記應指向的圖像上的位置。 如果您的標記是一個圓圈,則將第四個參數設置爲圖像的中心。 並創建標記傳遞markerImage到icon屬性:

var marker = new google.maps.Marker({ 
    map: map, 
    position: placeLoc, 
    icon: markerImage 
}); 
+3

https://groups.google.com/forum/#!topic/google-maps-js- api-v3/D-7uLsch28c表示MarkerImage對象不贊成使用google.maps.Icon對象 –

8

從文檔:

轉換MarkerImage物件到類型圖標

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)); 

成爲

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