简体   繁体   English

如何通过点击谷歌地图上的标记获取数据

[英]How to fetch Data on click of a Marker on google Map

I am dynamically adding Markers to a Google Map on click of a button我点击一个按钮动态地将标记添加到谷歌地图

The following is the JSON I am sending it from backend .以下是我从后端发送的 JSON。

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

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

On click of a button i am calling the following code单击按钮时,我正在调用以下代码

My question is on click of a button how can i fetch the dealerId ??我的问题是单击按钮如何获取dealerId?

I have got a listener below , how can i fetch the dealer ID ??我在下面有一个听众,我如何获取经销商 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);

        });
    }
}
}

Could you please let me know how to fetch dealerId on click of a Marker ??你能告诉我如何通过点击标记来获取dealerId吗??

You can add dealerId as a property of the google.maps.Marker object (like you did with the InfoWindow contents):您可以将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
});

Then in the click function, this.dealerId will give you the value for that marker.然后在点击函数中, this.dealerId将为您提供该标记的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM