将标记添加到Google地图v3

时间:2013-12-11 17:06:46

标签: javascript google-maps google-maps-api-3

我正在尝试从预定义变量addrstartnameaddrEndName向Google地图添加标记。它在v2中很有用,但是在v3中没有显示。

function addMarker2Map(point,markerID,imageUrl) {
  marker = new google.maps.Marker(point, {
    draggable: false,
    autoPan: false,
    title: (markerID.toLowerCase().indexOf("start") != -1) ? 'Start Point: ' + addrStartName: 'End Point: ' + addrEndName
  });
    marker.id = markerID;
    marker.setMap(map);
    marker.setIcon(imageUrl);
  if (markerID.indexOf("tart") != -1) {
    dirMarkerFrom = marker;
    google.maps.event.addListener(marker, 'dragend', function(point) {
      findAddrLocations(point, geocoderGetLocsFrom, geocoderNotFound);
    });
   } else {
   dirMarkerTo = marker;
     google.maps.event.addListener(marker, 'dragend', function(point) {findAddrLocations(point, geocoderGetLocsTo, geocoderNotFound);});
   }
 google.maps.event.addListener(marker, 'click', function (point) {
   if (this.id.toLowerCase().indexOf("start") != -1) {
       this.openInfoWindow("<div style='width:200px'><img src='images/google_pointer_start.png' alt='Start Address' title='Start Address' align='middle' /> <strong>Start Address:</strong><br />" + addrStartName + "</div>");
       } else {
      this.openInfoWindow("<div style='width:200px'><img src='images/google_pointer_finish.png' alt='End Address' title='End Address' align='middle'/><strong>End Address:</strong><br />" + addrEndName + "</div>");
   }
 });
}

我昨天在marker.setIcon(imageUrl);附近遇到了错误,但今天工作正常。

此处也可以看到调用此函数的示例

   google.maps.event.removeListener(mapClick); 
    if (point != undefined){
   addMarker2Map(point,markerID,imageUrl)
                   }

提前感谢您的任何帮助

1 个答案:

答案 0 :(得分:1)

google.maps.Marker的构造函数不正确。

只需要一个参数,MarkerOptions object

marker = new google.maps.Marker(point,{draggable:false, autoPan:false, title:       (markerID.toLowerCase().indexOf("start") != -1)?'Start Point: '+addrStartName:'End Point:     '+addrEndName});

应该是:

marker = new google.maps.Marker({position: point, draggable:false, autoPan:false, title:       (markerID.toLowerCase().indexOf("start") != -1)?'Start Point: '+addrStartName:'End Point:     '+addrEndName});