Моя программа расширяет только один маркер (Google Maps API) - PullRequest
0 голосов
/ 16 мая 2018

моя программа (использующая Google Maps API) расширяет только один маркер, и я не знаю почему, кто-то может мне помочь?Цикл создает 2 маркера, но только 1 показывает информацию.Я перепробовал все, что знаю, но не нашел ответа, цикл работает хорошо, потому что 2 маркера (созданы Сидни и Алис-Спрингс) EDIT = Теперь я обнаружил, что это работает, но проблема в том, что мне нужно сделать Zoom дляпродлите это, у кого-то есть решение?

  raph1">It is my first task</p>
    <!-- Map -->
        <div id="googleMap" onLoad="myMap()"></div>



    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB8BTio11xs3TIdsKPMG8g9iA7sS4NqAzE&callback=myMap"></script>



    </body>
</html>

    <!-- Script -->
         var cities = [
         {
            city: 'Alice Springs',
            icon: 'small-village.png',
            lat:  -23.69804199999999,
            long: 133.8807471,
            desc: "Alice Springs is the third-largest town in the Northern Territory of Australia.Popularly known as the Alice or simply Alice, Alice Springs is situated roughly in Australia's geographic centre.",
          },
          {
            city: 'Sidney',
            icon: 'big-city.png',
            lat:  -33.8688197,
            long: 151.20929550000005,
            desc: "Sidney is the state capital of New South Wales and the most populous city in Australia and Oceania.",
          }];
         function myMap() {
         //Center the map on Australia
         var omap= {
             center:new google.maps.LatLng(-25.274398,133.77513599999997),
             zoom:4,
                    };
         //Info LatLng
         markkers = [];
         var ne= {lat: -10.537626378223683,lng: 104.919462023010};
         var sw= {lat: -40.314544789915544,lng: 158.782164034453};

         //Center and resize

         google.maps.event.addDomListener(window, "resize", function() {
                                                                         var bound = new google.maps.LatLngBounds();
                                                                         for(var i in markkers) {
                                                                                                         bound.extend(markkers[i].getPosition());
                                                                                                      }
                                                                         map.fitBounds(bound);
                                                                        });
         var map=new google.maps.Map(document.getElementById("googleMap"),omap);
        //Inv Markers
            var inv1= new google.maps.Marker({
            position: ne,
            icon: 'inv.png',
            map: map,}); 

         var inv2= new google.maps.Marker({
            position: sw,
            icon: 'inv.png',
            map: map,}); 
         markkers.push(inv1)
         markkers.push(inv2)            
        //Markers
        var infoWindow = new google.maps.InfoWindow();
        var makercreate = function (info){

    var maker = new google.maps.Marker({
        map: map,
        position: new google.maps.LatLng(info.lat, info.long),
        title: info.city,
        icon: info.icon,
    });

    maker.content = '<div class="infoWindowContent">' + info.desc + '</div>';

    google.maps.event.addListener(maker, 'click', function(){
        infoWindow.setContent('<h2>' + maker.title + '</h2>' + maker.content);
        infoWindow.open(map, maker)});
    };
            for (i = 0; i < cities.length; i++){
            makercreate(cities[i]);
}

}

1 Ответ

0 голосов
/ 16 мая 2018
<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
</head> 
<body onLoad="myMap()">
  <div id="googleMap"  style="width: 100%; height: 800px;"></div>
<script>
     var cities = [
     {
        city: 'Alice Springs',
        //icon: 'small-village.png',
        lat:  -23.69804199999999,
        long: 133.8807471,
        desc: "Alice Springs is the third-largest town in the Northern Territory of Australia.Popularly known as the Alice or simply Alice, Alice Springs is situated roughly in Australia's geographic centre.",
      },
      {
        city: 'Sidney',
        //icon: 'big-city.png',
        lat:  -33.8688197,
        long: 151.20929550000005,
        desc: "Sidney is the state capital of New South Wales and the most populous city in Australia and Oceania.",
      }];
    function myMap() {
     var omap= {
         center:new google.maps.LatLng(-25.274398,133.77513599999997),
         zoom:4 };
     markkers = [];
     var ne= {lat: -10.537626378223683,lng: 104.919462023010};
     var sw= {lat: -40.314544789915544,lng: 158.782164034453};
     google.maps.event.addDomListener(window, "resize", function() {
             var bound = new google.maps.LatLngBounds();
             for(var i in markkers) { bound.extend(markkers[i].getPosition());}
             map.fitBounds(bound);
            });
     var map=new google.maps.Map(document.getElementById("googleMap"),omap);
        var inv1= new google.maps.Marker({
        position: ne,
        //icon: 'inv.png',
        map: map,}); 

     var inv2= new google.maps.Marker({
        position: sw,
        //icon: 'inv.png',
        map: map,}); 
     markkers.push(inv1)
     markkers.push(inv2)            
    var infoWindow = new google.maps.InfoWindow();
    var makercreate = function (info){
    var maker = new google.maps.Marker({
        map: map,
        position: new google.maps.LatLng(info.lat, info.long),
        title: info.city,
        icon: info.icon,
    });
    maker.content = '<div class="infoWindowContent">' + info.desc + '</div>';
    google.maps.event.addListener(maker, 'click', function(){
        infoWindow.setContent('<h2>' + maker.title + '</h2>' + maker.content);
        infoWindow.open(map, maker)});
    };
    for (i = 0; i < cities.length; i++){
        makercreate(cities[i]);
    }
    }
</script>
</body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...