Google Maps Places Javascript API - PullRequest
       64

Google Maps Places Javascript API

2 голосов
/ 26 января 2020

Я работаю над проектом, который мне нужен, чтобы отобразить 2 места Google Maps на одной странице. Все работает нормально, когда я отображаю только 1 место, как только я добавляю второе, первое перестает отображаться, оно будет показывать только 1 за раз, если я отключу первое, оно покажет второе. Я новичок в Javascript, если кто-то может мне помочь. Обратите внимание, что у меня есть API-ключ только один раз, когда он у меня был дважды, я получил сообщение об ошибке с Google Maps.

<HTML>
  <div id="map1" style="height:500px;">
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&libraries=places&callback=initMap"></script>
    <script>
      function initMap() {
        var map1 = new google.maps.Map(document.getElementById('map1'), {
          center: {
            lat: 40.0527839,
            lng: -74.163964
          },
          zoom: 15
        });

        var infowindow = new google.maps.InfoWindow();
        var service = new google.maps.places.PlacesService(map1);

        service.getDetails({
          placeId: 'ChIJ-Wn_paSDwYkREENXsJ--cng'
        }, function(place, status) {
          if (status === google.maps.places.PlacesServiceStatus.OK) {
            var marker = new google.maps.Marker({
              map: map1,
              position: place.geometry.location
            });
            map1.setCenter(place.geometry.location);
            map1.addListener('tilesloaded', function() {

              infowindow.setContent('<div> <strong>' + place.name + ' ' +
                place.formatted_address + '</strong></div>' +
                place.opening_hours.weekday_text[6] + '<BR>' +
                place.opening_hours.weekday_text[0] + '<BR>' +
                place.opening_hours.weekday_text[1] + '<BR>' +
                place.opening_hours.weekday_text[2] + '<BR>' +
                place.opening_hours.weekday_text[3] + '<BR>' +
                place.opening_hours.weekday_text[4] + '<BR>' +
                place.opening_hours.weekday_text[5]);
            });
            infowindow.open(map1, marker);
          }
        });
      }
    </script>
  </div>
  <div id="map" style="height:500px;">

    <script>
      function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          center: {
            lat: -33.866,
            lng: 151.196
          },
          zoom: 15
        });

        var infowindow = new google.maps.InfoWindow();
        var service = new google.maps.places.PlacesService(map);

        service.getDetails({
          placeId: 'ChIJ5zCkBzpFwokRlW13makMfEM'
        }, function(place, status) {
          if (status === google.maps.places.PlacesServiceStatus.OK) {
            var marker = new google.maps.Marker({
              map: map,
              position: place.geometry.location
            });
            map.setCenter(place.geometry.location);
            map.addListener('tilesloaded', function() {

              infowindow.setContent('<div><strong>' + place.name + ' ' +
                place.formatted_address + '</strong></div>' +
                place.opening_hours.weekday_text[6] + '<BR>' +
                place.opening_hours.weekday_text[0] + '<BR>' +
                place.opening_hours.weekday_text[1] + '<BR>' +
                place.opening_hours.weekday_text[2] + '<BR>' +
                place.opening_hours.weekday_text[3] + '<BR>' +
                place.opening_hours.weekday_text[4] + '<BR>' +
                place.opening_hours.weekday_text[5]);
            });
            infowindow.open(map, marker);
          }
        });
      }
    </script>
  </div>
</html>

1 Ответ

0 голосов
/ 28 января 2020

Чтобы помочь вам понять, как реализовать совет xomena, пожалуйста, проверьте этот рабочий jsfiddle .

Полный код ниже:

<div id="map1" style="height:500px;"></div>
<div id="map2" style="height:500px;"></div>
<script>
  function initMap() {
    var map1 = new google.maps.Map(document.getElementById('map1'), {
      center: {
        lat: 40.0527839,
        lng: -74.163964
      },
      zoom: 15
    });

    var infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map1);

    service.getDetails({
      placeId: 'ChIJ-Wn_paSDwYkREENXsJ--cng'
    }, function(place, status) {
      if (status === google.maps.places.PlacesServiceStatus.OK) {
        var marker = new google.maps.Marker({
          map: map1,
          position: place.geometry.location
        });
        map1.setCenter(place.geometry.location);
        map1.addListener('tilesloaded', function() {

          infowindow.setContent('<div> <strong>' + place.name + ' ' +
            place.formatted_address + '</strong></div>' +
            place.opening_hours.weekday_text[6] + '<BR>' +
            place.opening_hours.weekday_text[0] + '<BR>' +
            place.opening_hours.weekday_text[1] + '<BR>' +
            place.opening_hours.weekday_text[2] + '<BR>' +
            place.opening_hours.weekday_text[3] + '<BR>' +
            place.opening_hours.weekday_text[4] + '<BR>' +
            place.opening_hours.weekday_text[5]);
        });
        infowindow.open(map1, marker);
      }
    });

    var map2 = new google.maps.Map(document.getElementById('map2'), {
      center: {
        lat: -33.866,
        lng: 151.196
      },
      zoom: 15
    });

    var infowindow2 = new google.maps.InfoWindow();
    var service2 = new google.maps.places.PlacesService(map2);

    service2.getDetails({
      placeId: 'ChIJ5zCkBzpFwokRlW13makMfEM'
    }, function(place, status) {
      if (status === google.maps.places.PlacesServiceStatus.OK) {
        var marker2 = new google.maps.Marker({
          map: map2,
          position: place.geometry.location
        });
        map2.setCenter(place.geometry.location);
        map2.addListener('tilesloaded', function() {

          infowindow2.setContent('<div><strong>' + place.name + ' ' +
            place.formatted_address + '</strong></div>' +
            place.opening_hours.weekday_text[6] + '<BR>' +
            place.opening_hours.weekday_text[0] + '<BR>' +
            place.opening_hours.weekday_text[1] + '<BR>' +
            place.opening_hours.weekday_text[2] + '<BR>' +
            place.opening_hours.weekday_text[3] + '<BR>' +
            place.opening_hours.weekday_text[4] + '<BR>' +
            place.opening_hours.weekday_text[5]);
        });
        infowindow2.open(map2, marker2);
      }
    });

  }

</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initMap"></script>

Надеюсь, это поможет!

...