2010-04-01 157 views
66

我有一个脚本,循环和添加标记一次一个。添加ID到谷歌地图标记

我试图让当前标记有一个信息窗口,并且只在地图上5个标记在时间(4无信息窗口和1)

我将如何添加一个ID给每个标记,以便我可以根据需要删除和关闭信息窗口。

这是我使用的设置标记功能:

function codeAddress(address, contentString) { 

var infowindow = new google.maps.InfoWindow({ 
    content: contentString 
}); 

if (geocoder) { 

    geocoder.geocode({ 'address': address}, function(results, status) { 

    if (status == google.maps.GeocoderStatus.OK) { 

     map.setCenter(results[0].geometry.location); 

     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 

     infowindow.open(map,marker); 

     } else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
    } 

}

+0

我不会nk可以为标记添加一个id。我想你可以给每个标记一个自定义标题,并找到与标题匹配的div标签。虽然不是理想的解决方案。 – 2010-04-02 00:42:24

+0

有没有人回答你的问题? – CrazyEnigma 2010-07-22 16:36:44

回答

166

JavaScript是一种动态语言。你可以将它添加到对象本身。

var marker = new google.maps.Marker(markerOptions); 
marker.metadata = {type: "point", id: 1}; 

而且,因为所有V3对象扩展MVCObject()。您可以使用:

marker.setValues({type: "point", id: 1}); 
// or 
marker.set("type", "point"); 
marker.set("id", 1); 
var val = marker.get("id"); 
+8

我很好奇,一旦你给出了标记ID,你将如何去访问地图画布外的这些标记。 $( '#1')的doSomething();例如? – willdanceforfun 2013-03-07 08:31:35

+0

如果'marker.set( “ID”,1);'不工作,使用'marker.set( '身份证',1);'。它适用于我的情况。 – 2013-06-05 09:39:31

+0

我收到消息:marker.get不是函数。 – Rodrigo 2013-11-21 22:55:11

1

为什么不使用存储每个标记对象,并引用一个ID的缓存?

var markerCache= {}; 
var idGen= 0; 

function codeAddress(addr, contentStr){ 
    // create marker 
    // store 
    markerCache[idGen++]= marker; 
} 

编辑:当然,这依赖于一个数字索引系统,它不提供数组的长度属性。当然,您可以为Object对象创建一个原型,并为此创建一个长度等等。 OTOH,为每个地址生成一个唯一的ID值(MD5等)可能是要走的路。

3

我有一个简单的Location类,我用它来处理所有与标记相关的事情。我会在下面粘贴我的代码,以便让你发挥作用。

最后一行是实际创建标记对象的内容。它通过循环我的位置,这是这个样子的一些JSON:

{"locationID":"98","name":"Bergqvist Järn","note":null,"type":"retail","address":"Smidesvägen 3","zipcode":"69633","city":"Askersund","country":"Sverige","phone":"0583-120 35","fax":null,"email":null,"url":"www.bergqvist-jb.com","lat":"58.891079","lng":"14.917371","contact":null,"rating":"0","distance":"45.666885421019"} 

下面是代码:

如果你看一下我的位置类的target()方法,你会看到我不断参考infowindow的,并可以简单地open()close()他们因为一个参考。

见现场演示:http://ww1.arbesko.com/en/locator/(在瑞典城市类型,如斯德哥尔摩,然后回车)

var Location = function() { 
    var self = this, 
     args = arguments; 

    self.init.apply(self, args); 
}; 

Location.prototype = { 
    init: function(location, map) { 
     var self = this; 

     for (f in location) { self[f] = location[f]; } 

     self.map = map; 
     self.id = self.locationID; 

     var ratings = ['bronze', 'silver', 'gold'], 
      random = Math.floor(3*Math.random()); 

     self.rating_class = 'blue'; 

     // this is the marker point 
     self.point = new google.maps.LatLng(parseFloat(self.lat), parseFloat(self.lng)); 
     locator.bounds.extend(self.point); 

     // Create the marker for placement on the map 
     self.marker = new google.maps.Marker({ 
      position: self.point, 
      title: self.name, 
      icon: new google.maps.MarkerImage('/wp-content/themes/arbesko/img/locator/'+self.rating_class+'SmallMarker.png'), 
      shadow: new google.maps.MarkerImage(
             '/wp-content/themes/arbesko/img/locator/smallMarkerShadow.png', 
             new google.maps.Size(52, 18), 
             new google.maps.Point(0, 0), 
             new google.maps.Point(19, 14) 
            ) 
     }); 

     google.maps.event.addListener(self.marker, 'click', function() { 
      self.target('map'); 
     }); 

     google.maps.event.addListener(self.marker, 'mouseover', function() { 
      self.sidebarItem().mouseover(); 
     }); 

     google.maps.event.addListener(self.marker, 'mouseout', function() { 
      self.sidebarItem().mouseout(); 
     }); 

     var infocontent = Array(
      '<div class="locationInfo">', 
       '<span class="locName br">'+self.name+'</span>', 
       '<span class="locAddress br">', 
        self.address+'<br/>'+self.zipcode+' '+self.city+' '+self.country, 
       '</span>', 
       '<span class="locContact br">' 
     ); 

     if (self.phone) { 
      infocontent.push('<span class="item br locPhone">'+self.phone+'</span>'); 
     } 

     if (self.url) { 
      infocontent.push('<span class="item br locURL"><a href="http://'+self.url+'">'+self.url+'</a></span>'); 
     } 

     if (self.email) { 
      infocontent.push('<span class="item br locEmail"><a href="mailto:'+self.email+'">Email</a></span>'); 
     } 

     // Add in the lat/long 
     infocontent.push('</span>'); 

     infocontent.push('<span class="item br locPosition"><strong>Lat:</strong> '+self.lat+'<br/><strong>Lng:</strong> '+self.lng+'</span>'); 

     // Create the infowindow for placement on the map, when a marker is clicked 
     self.infowindow = new google.maps.InfoWindow({ 
      content: infocontent.join(""), 
      position: self.point, 
      pixelOffset: new google.maps.Size(0, -15) // Offset the infowindow by 15px to the top 
     }); 

    }, 

    // Append the marker to the map 
    addToMap: function() { 
     var self = this; 

     self.marker.setMap(self.map); 
    }, 

    // Creates a sidebar module for the item, connected to the marker, etc.. 
    sidebarItem: function() { 
     var self = this; 

     if (self.sidebar) { 
      return self.sidebar; 
     } 

     var li = $('<li/>').attr({ 'class': 'location', 'id': 'location-'+self.id }), 
      name = $('<span/>').attr('class', 'locationName').html(self.name).appendTo(li), 
      address = $('<span/>').attr('class', 'locationAddress').html(self.address+' <br/> '+self.zipcode+' '+self.city+' '+self.country).appendTo(li); 

     li.addClass(self.rating_class); 

     li.bind('click', function(event) { 
      self.target(); 
     }); 

     self.sidebar = li; 

     return li; 
    }, 

    // This will "target" the store. Center the map and zoom on it, as well as 
    target: function(type) { 
     var self = this; 

     if (locator.targeted) { 
      locator.targeted.infowindow.close(); 
     } 

     locator.targeted = this; 

     if (type != 'map') { 
      self.map.panTo(self.point); 
      self.map.setZoom(14); 
     }; 

     // Open the infowinfow 
     self.infowindow.open(self.map); 
    } 
}; 

for (var i=0; i < locations.length; i++) { 
    var location = new Location(locations[i], self.map); 
    self.locations.push(location); 

    // Add the sidebar item 
    self.location_ul.append(location.sidebarItem()); 

    // Add the map! 
    location.addToMap(); 
}; 
+0

可以在这里找到http://www.geotweetmap.com/json_test。php – Nick 2010-04-03 21:59:02

-2

标记已经拥有唯一的ID

marker.__gm_id 
+0

我将使用这些唯一的解决方案。 – clime 2013-08-15 17:27:59

+0

不适用于我。未定义 – John 2015-01-29 18:16:53

7

只是添加另一种解决方案是对我的作品。你可以简单地将其追加在标记选项:

var marker = new google.maps.Marker({ 
    map: map, 
    position: position, 

    // Custom Attributes/Data/Key-Values 
    store_id: id, 
    store_address: address, 
    store_type: type 
}); 

然后检索它们:

marker.get('store_id'); 
marker.get('store_address'); 
marker.get('store_type');