簡體   English   中英

谷歌地圖地方 Javascript API

[英]Google Maps Places Javascript API

我正在做一個項目,我需要在同一頁面上顯示 2 個 Google 地圖位置。 當我只顯示 1 個位置時一切正常,只要我添加第二個,第一個停止顯示,它一次只顯示 1 個,如果我禁用第一個,它將顯示第二個。 如果有人可以幫助我,我是 Javascript 的初學者。 請注意,這只有一次我的 API 密鑰,當我擁有兩次時,我從谷歌地圖中收到錯誤消息。

<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>

為了幫助您了解如何實施 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>

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM