将自定义图标添加到Google地图

时间:2009-11-14 12:11:21

标签: javascript google-maps

我需要在某些Google Maps javascript中添加自定义图标。

以下代码供您参考:

function populateMap() {
 var map = new GMap2(document.getElementById("map"));
 map.setCenter(new GLatLng(55.915832522285235, -3.5911989212036133), 10);
 map.setUIToDefault();



 var points = new Array(5);

  points[1] = [55.992602,-3.92968,'<div class="infoContainer"><h2><a href="#">Mayfield Drive, Longcroft, Bonnybridge<\/a><\/h2><p class="address">{locations_address}<\/p><\/div>'];



  points[2] = [55.9471382,-3.9729174,'<div class="infoContainer"><h2><a href="#">Oakwood Cumbernauld<\/a><\/h2><p class="address">{locations_address}<\/p><\/div>'];



  points[3] = [55.933873,-3.12406,'<div class="infoContainer"><h2><a href="#">Wauchope Square, Edinburgh<\/a><\/h2><p class="address">{locations_address}<\/p><\/div>'];



  points[4] = [55.843728,-3.95258,'<div class="infoContainer"><h2><a href="#">Gibb Street, Chapelhall<\/a><\/h2><p class="address">{locations_address}<\/p><\/div>'];



  points[5] = [55.854559,-4.000543,'<div class="infoContainer"><h2><a href="#">Paddock Street, Sikeside<\/a><\/h2><p class="address">{locations_address}<\/p><\/div>'];




 for(var i=1; i < points.length; i++) {
  var point = new GLatLng(points[i][0],points[i][1]);
  var windowInfo = points[i][2];
  var marker = createMarker(point,windowInfo);
  map.addOverlay(marker);
 }
}

    function createMarker(point, overlayText) {
 var marker = new GMarker(point);
 GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(overlayText);});
 return marker;
}

    function addLoadEvent(func) {
 var oldonload = window.onload;
 if (typeof window.onload != 'function') {
  window.onload = func;
 } else {
  window.onload = function() {
   if (oldonload) {
    oldonload();
   }
   func();
  }
 }
}

addLoadEvent(populateMap);

2 个答案:

答案 0 :(得分:2)

谷歌地图API页面似乎都没有工作,但这可能有助于任何有类似问题的机构

// Create our "tiny" marker icon
var centreicon = new GIcon();
centreicon.constructor
centreicon.image = "http://maps.google.com/mapfiles/kml/pal3/icon31.png ";
centreicon.shadow = "http://maps.google.com/mapfiles/kml/pal3/icon31s.png ";
centreicon.iconSize = new GSize(12, 20);
centreicon.shadowSize = new GSize(22, 20);
centreicon.iconAnchor = new GPoint(6, 20);
centreicon.infoWindowAnchor = new GPoint(5, 1);

.......你的代码创建地图然后

           var marker = new GMarker(point, centreicon);

           map.addOverlay(marker);

答案 1 :(得分:0)