简体   繁体   English

谷歌地图API v3,无法获得点击标记

[英]Google maps API v3, can't get clicked marker

I'm getting from my webAPI an array of json objects of users. 我从我的webAPI获取了一组用户的json对象。 I can display the marker for each user correctly in their location but I cannot get to work when I click in each of them to get the clicked item, I always get the information from the last in the list. 我可以在他们的位置正确显示每个用户的标记,但是当我点击每个用户获取被点击的项目时我无法工作,我总是从列表中的最后一个获取信息。

This is the code I use to put them on the map: 这是我用来将它们放在地图上的代码:

var markers = [];

for ( var i = 0; i < size; i++) {
    var zIndex = membersList[i].type; 
    var latLng = new google.maps.LatLng(membersList[i].latitude,
            membersList[i].longitude);
    var marker = new google.maps.Marker({
        'position' : latLng,
        'shadow' : null,
        'zIndex' : zIndex,
        'title'  : membersList[i].username,
        'id'     : i,
        'icon' : markerImage[membersList[i].type]
    });

    google.maps.event.addListener(marker, 'click', function()
    {
        console.log(marker.id);
        var clicked = membersList[marker.id];
        var mType = ['', 'Couple', 'Male', 'Female'];
        var textType = mType[clicked.type];
        var userName = clicked.username;
        $(this).simpledialog2({
            mode: 'button',
            headerText: "OPTIONS",
            headerClose: true,
            buttonPrompt:  userName+ ' ('+textType+')',
            buttons : {
                'PROFILE': {
                    click: function () {
                        toUser = userName;
                        loadPage('profile');
                    },
                    icon: "info"
                },
                'MESSAGE': {
                    click: function () { 
                        toUser = userName;
                        loadPage('compose');
                    },
                    icon: "star",
                }
            }
        });
    });

    markers.push(marker);
}

markerCluster.addMarkers(markers);

By the way I'm using markerClusterer to display the markers grouped on the map in some zoom levels. 顺便说一下,我使用markerClusterer以一些缩放级别显示在地图上分组的标记。

It´s never too late, so, if I understood correctly, this is the aproach y would take. 这永远不会太晚,所以,如果我理解正确的话,这是你需要的方法。

When intantiating the markers, add them to an array after their individual setup is completed. 在对标记进行实例化时,在完成各自的设置后将它们添加到数组中。

var MySiteWithMap = { 
    markersArray : [], //Markers array
    map : {}, //Map object instance
    init : function() {

       var mapCanvas = document.getElementById("map");
       var mapOptions = {
         center : new google.maps.LatLng(-26.816667, -65.216667),
         zoom : 11
       };
       this.map = new google.maps.Map(mapCanvas,mapOptions);

       /*
       The following is used to get the Lat and Lng where the user clicks. 
       It may be useful to store the last coordinate where the user clicked.
       */
       this.map.addListener("click", function(event){
          var lat = event.latLng.lat();
          var lng = event.latLng.lng();
          console.log("Lat: "  + lat + "  Lng: "  +  lng);
       });

       MySiteWithMap.initMarkers();

    },
    initMarkers : function(url){
         $.get(url).done(function(result){
            var lat;
            var lng;
            $.each(result,function(index,element){
               lat = element.Latitud;
               lng = element.Longitud;

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

               var marker = new google.maps.Marker({
                   position : position,
                   map : _this.map,
                   id : element.id //Supposing it's a primary key from the DB or some other unique id.
               });
               marker.addListener('click',function(){
                   console.log(this.id)
               });

               Mapas.markersArray[marker.id] = marker; 
        })
    }
}

By doing so, you have an array of Marker objects. 通过这样做,您有一个Marker对象数组。 If you want to get a reference to an specific marker instance, an option would be to get the id of the clicked marker and look for it on the "markersArray". 如果要获取对特定标记实例的引用,则可以选择获取所单击标记的id并在“markersArray”上查找它。

If you want to get the position (or any other properties) of a marker, you can do something like this: 如果您想获得标记的位置(或任何其他属性),您可以执行以下操作:

console.log(MySiteWithMap.markersArray[123].position)

Hope this helps! 希望这可以帮助!

you are using marker which is defined outside of you click function. 您使用的是在您点击功能之外定义的标记。

what you need to look at is the google maps api, and find out how to get the clicked marker id 您需要查看的是google maps api,并了解如何获取点击的标记ID

marker is incrementing through the loop and will ALWAYS be the last marker created 标记在循环中递增,并且始终是最后创建的标记

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

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