如何在谷歌地图上点击标记获取数据

时间:2015-05-04 08:04:19

标签: google-maps

我点击按钮动态地将标记添加到Google地图

以下是我从后端发送的JSON。

[
    {
        "longititude": "78.486671",
        "latitude": "17.385044",
        "address": "xxxx",
        "dealerId": "1"
    },

    {
        "longititude": "78.43471",
        "latitude": "17.367044",
        "address": "xxxxSS",
        "dealerId": "2"
    }
]

点击按钮我正在调用以下代码

我的问题是点击按钮如何获取dealerId?

我下面有一个听众,我怎样才能获得经销商ID?

 google.maps.event.addListener(global_markers[i], 'click', function() {

                 infowindow.setContent(this['infowindow']);
            infowindow.open(map, this);

        });

function initializeCalllater(lator,lonor,response) {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(lator, lonor);
    var myOptions = {
        zoom: 10,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    addMarker(response);
}


function addMarker(markers) {
    if(markers.length>0)
    {
        var infowindow = new google.maps.InfoWindow({});
        var global_markers = [];    

    for (var i = 0; i < markers.length; i++) {

        // obtain the attribues of each marker
        var lat = parseFloat(markers[i].latitude);
        var lng = parseFloat(markers[i].longititude);
        var trailhead_name = markers[i].address;
        var dealerId = markers[i].dealerID;

        var myLatlng = new google.maps.LatLng(lat, lng);

        var contentString = "<html><body><div><p><h2>" + trailhead_name + "</h2></p></div></body></html>";

        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: "Coordinates: " + lat + " , " + lng + " | Trailhead name: " + trailhead_name
        });
        marker['infowindow'] = contentString;
        global_markers[i] = marker;
        $(".howmanyfound").text(markers.length + ' Found');
        google.maps.event.addListener(global_markers[i], 'click', function() {
                 infowindow.setContent(this['infowindow']);
            infowindow.open(map, this);

        });
    }
}
}

请您告诉我如何通过点击Marker来获取dealerId?

1 个答案:

答案 0 :(得分:0)

您可以将dealerId添加为google.maps.Marker对象的属性(就像您使用InfoWindow内容一样):

var marker = new google.maps.Marker({
    position: myLatlng,
    map: map,
    title: "Coordinates: " + lat + " , " + lng + " | Trailhead name: " + trailhead_name,
    dealerId: dealerId
});

然后在click函数中,this.dealerId将为您提供该标记的值。