当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Multiple markers on Phonegap plugin google maps api v2

问题描述:

I'm working on phonegap google maps plugin (v2)

but I only get one marker. I would like to show multiple markers. Can you help me? Thanks, here is my code:

<script type="text/javascript">

document.addEventListener("deviceready", function() {

var mapDiv = document.getElementById("map_canvas");

const TEST = new plugin.google.maps.LatLng(41.3772614,2.167013,15);

var map = plugin.google.maps.Map.getMap(mapDiv, {

'camera': {

'latLng': TEST,

'zoom': 17,

}

});

map.addEventListener(plugin.google.maps.event.MAP_READY, function() {

map.addMarker({

'position': TEST,

'title': "Aqui esta el test!",

'snippet': "Texto del snippet!",

}, function(marker) {

// marker.showInfoWindow(); // Show infowindow

});

});

});

</script>

网友答案:
    document.addEventListener("deviceready", function()  {
             var mapDiv = document.getElementById("map_canvas");
             var locatonList = [
                    {lat : 41.3772614 , long : 2.16701315, title : 'Title A' ,snippet : 'Snippet A'},
                    {lat : 41.7 , long : 2.53, title : 'Title B' ,snippet : 'Snippet B'},
                    {lat : 42 , long : 2.69, title : 'Title C' ,snippet : 'Snippet C'}
             ];

             const TEST = new plugin.google.maps.LatLng(locatonList[0].lat,locatonList[0].long);                             
             var map = plugin.google.maps.Map.getMap(mapDiv, {
                    'camera': {
                        'latLng': TEST,
                        'zoom': 17
                    }
             });

             map.addEventListener(plugin.google.maps.event.MAP_READY, function() {

                    for(var locIndex = 0;locIndex<locatonList.length;locIndex++){
                            var locObj = new plugin.google.maps.LatLng(locatonList[locIndex].lat,locatonList[locIndex].long);
                            map.addMarker({
                                   'position': locObj,
                                   'title': locatonList[locIndex].title,
                                   'snippet': locatonList[locIndex].snippet
                            }, function(marker) {
                                 // marker.showInfoWindow(); // Show infowindow
                            });
                    }
              });

     },false);
分享给朋友:
您可能感兴趣的文章:
随机阅读: