Google maps API multiple markers

后端 未结 1 880
轻奢々
轻奢々 2021-01-27 08:23

i am using following code to show google map and also create a marker on the map. Its working well. I just need to put multiple markers on the same map.



        
相关标签:
1条回答
  • 2021-01-27 08:36

    This is a basic question and several examples can be found in the web. You basically need an array to hold the various markers lat lng's. Then use a loop to place these latlng's on the map just like you do with one marker.

    var berlin = new google.maps.LatLng(52.520816, 13.410186);
    var neighborhoods = [
    new google.maps.LatLng(52.511467, 13.447179),
    new google.maps.LatLng(52.549061, 13.422975),
    new google.maps.LatLng(52.497622, 13.396110),
    new google.maps.LatLng(52.517683, 13.394393)
    ];
    var markers = [];
    var map;
    
    function initialize() {
    var mapOptions = {
    zoom: 12,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: berlin
    };
    map = new google.maps.Map(document.getElementById('map_canvas'),
    mapOptions);
    drop();
    }
    
    function drop() {
     for (var i = 0; i < neighborhoods.length; i++) 
      {
        markers.push(new google.maps.Marker({
        position: neighborhoods[i],
        map: map,
        }));
      }
    }
    
    0 讨论(0)
提交回复
热议问题