Google Map API V3:如何将自定义数据添加到标记 [英] Google Map API V3: How to add Custom data to markers
问题描述
我在页面上显示其他内容这取决于标记,因此当点击标记时,页面上的内容必须根据点击哪个标记而改变。我希望一旦标记被点击就能存储和检索自定义数据。
感谢
由于Google Marker是一个JavaScript对象,因此您可以添加自定义信息形式为 key:value
,其中key是一个有效的字符串。它们被称为对象属性,可以通过许多不同的方式处理。该值可以是任何合法的,如数字或字符串,还可以是函数,甚至是其他对象。三种简单的方法:在声明中,点符号和方括号
var markerA = new google.maps.Marker({
map:map,
position:new google.maps.LatLng(0,0),
customInfo:Marker A
});
var markerB = new google.maps.Marker({
map:map,
position:new google.maps.LatLng(-10,0)
} );
markerB.customInfo =标记B;
var markerC = new google.maps.Marker({
map:map,
position:new google.maps.LatLng(-20,0)
} );
markerC ['customInfo'] =标记C;
然后以类似的方式检索它:
google.maps.event.addListener(markerA,'click',function(){
alert(this.customInfo);
});
Is there a way I can add some custom information to my markers for later use. There are ways to have an info-window and a title, but what If I want to associate the marker with other information.
I have other stuff being displayed on the page that depends on the markers so when a marker is click the content on the page has to change depending upon which marker was clicked.I would like store and retrieve the custom data once a marker is lets say clicked etc.
Thanks
As a Google Marker is a JavaScript object, you may add custom information in the form key: value
, where key is a valid string. They are called object properties and can be approached in many different ways. The value can be anything legal, as simple as numbers or strings, and also functions, or even other objects. Three simple ways: in the declaration, dot notation and square brackets
var markerA = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(0, 0),
customInfo: "Marker A"
});
var markerB = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(-10, 0)
});
markerB.customInfo = "Marker B";
var markerC = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(-20, 0)
});
markerC['customInfo'] = "Marker C";
Then to retrieve it in a similar manner:
google.maps.event.addListener(markerA, 'click', function() {
alert(this.customInfo);
});
这篇关于Google Map API V3:如何将自定义数据添加到标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!