Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌地图标记数据_Javascript_Jquery_Ajax_Google Maps - Fatal编程技术网

Javascript 谷歌地图标记数据

Javascript 谷歌地图标记数据,javascript,jquery,ajax,google-maps,Javascript,Jquery,Ajax,Google Maps,我目前正在对服务器进行ajax调用,以获取要在google地图上显示的lat/long列表。我还将“单击”事件附加到每个标记上。诀窍是,我需要能够存储一点额外的数据到标记,以了解我正在处理的ID(来自数据库),以便稍后将其匹配回db。我正在使用Title属性显示一些友好的信息。AJAX、标记创建和单击事件工作正常。为标记存储额外数据的正确方法是什么?请参见此处的代码: $.ajax({ url: "/location/NearbyHotspots", data: {

我目前正在对服务器进行ajax调用,以获取要在google地图上显示的lat/long列表。我还将“单击”事件附加到每个标记上。诀窍是,我需要能够存储一点额外的数据到标记,以了解我正在处理的ID(来自数据库),以便稍后将其匹配回db。我正在使用Title属性显示一些友好的信息。AJAX、标记创建和单击事件工作正常。为标记存储额外数据的正确方法是什么?请参见此处的代码:

$.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);
    }
});
$.ajax({
url:“/location/nearbyhospots”,
数据:{
lat:marker.position.lat(),
lng:marker.position.lng(),
半径:10
},
数据类型:“json”,
类型:“POST”,
成功:功能(数据、状态、xhttp){
对于(变量i=0;i
我认为您的方法是正确的,只是事件处理程序不正确。在你的处理程序中

function(mark) {
    alert(mark.hotspotid);
}
mark
参数不是您期望的标记,而是
MouseEvent
()

为了解决这个问题,您需要使用闭包来传递对标记的引用。循环使这一点变得复杂-您不能仅使用对
newmarker
的引用,因为它只会引用循环中的最后一个标记。有几种不同的方法可以解决此问题,但最简单的方法是在单独的函数中附加click事件:

success: function (data, status, xhttp) {
    // define a function to attach the click event
    function attachClickEvent(marker) {
        google.maps.event.addListener(marker, "click", function() {
            // the reference to the marker will be saved in the closure
            alert(marker.hotspotid);
        });
    }
    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
        });

        newmarker.hotspotid = data[i].ID;
        attachClickEvent(newmarker);
    }
},
success:函数(数据、状态、xhttp){
//定义用于附加单击事件的函数
函数附件ClickEvent(标记器){
google.maps.event.addListener(标记“单击”,函数(){
//对标记的引用将保存在闭包中
警报(标记热点);
});
}
对于(变量i=0;i
HA!我想出来了。“这个”成功了


顺便说一句-在这种情况下,不再需要使用自定义数据扩展marker对象,您可以使用数据[i].id将第二个参数传递给attachClickEvent函数。这应该是可接受的答案。它提供了一种干净的方法,不需要像OP自己的答案所建议的那样在处理程序中留下未使用的参数。
google.maps.event.addListener(newmarker, "click", function(mark) {
    alert(this.hotspotid);
});