问题描述
我目前正在做一个AJAX调用服务器,以获得一个可在谷歌地图上显示纬度/多头名单。我已经附加了点击事件,每个标记为好。诀窍是,我需要能够存储额外的数据一点点地标记知道什么ID(从数据库)我处理,所以我匹配它回数据库后。我使用的标题属性来显示一些友好的信息。阿贾克斯,标记的创建,并单击事件做工精细。什么是正确的方式来存储标记额外的数据?看到这里code:
$。阿贾克斯({
网址:/位置/ NearbyHotspots
数据: {
纬度:marker.position.lat()
LNG:marker.position.lng()
半径:10
},
数据类型:JSON,
键入:POST,
成功:功能(数据,状态,xhttp){
对于(VAR I = 0; I< data.length;我++){
VAR LOC =新google.maps.LatLng(数据[I] .Lat,数据[I]。长);
VAR newmarker =新google.maps.Marker({
位置:禄,
可拖动:假的,
地图:地图,
标题:数据[I] .Name点
});
//似乎这不工作
newmarker.hotspotid =数据[I] .ID;
google.maps.event.addListener(newmarker,咔嚓,函数(标记){
警报(mark.hotspotid);
});
}
},
错误:函数(jqXHR,textStatus,errorThrown){
警报(textStatus);
}
});
哈!我想到了。 这个做到了!
google.maps.event.addListener(newmarker,咔嚓,函数(标记){
警报(this.hotspotid);
});
I'm currently making an ajax call to the server to get a list of lat/longs to be displayed on a google map. I've attached a "click" event to each marker as well. The trick is that I need to be able to store a little bit of extra data to the marker to know what ID( from the database ) that I'm dealing with so I match it back to the db later. I'm using the Title property to display some friendly info. The AJAX, Marker creation, and click event work fine. What's the right way to store the extra data for the marker? See code here:
$.ajax({
url: "/location/NearbyHotspots",
data: {
lat: marker.position.lat(),
lng: marker.position.lng(),
radius: 10
},
datatype: "json",
type: "POST",
success: function (data, status, xhttp) {
for (var i = 0; i < data.length; i++) {
var loc = new google.maps.LatLng(data[i].Lat, data[i].Long);
var newmarker = new google.maps.Marker({
position: loc,
draggable: false,
map: map,
title: data[i].Name
});
// This doesn't seem to work
newmarker.hotspotid = data[i].ID;
google.maps.event.addListener(newmarker, "click", function(mark) {
alert(mark.hotspotid);
});
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});
HA! I figured it out. "this" did it!
google.maps.event.addListener(newmarker, "click", function(mark) {
alert(this.hotspotid);
});
这篇关于谷歌地图标记数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!