简体   繁体   English

在infowindow中使用街景的Google Map API无法正常工作

[英]google map api with street view in infowindow not working

in the link on bottom I put my google map, i have problem because when i'm clicking on marker, that take me only last data from localization string. 在底部的链接中,我放了谷歌地图,因为在单击标记时,我只能从本地化字符串中获取最后一个数据,因此出现了问题。

http://bit.ly/1tYVIdZ http://bit.ly/1tYVIdZ

can you help me ? 你能帮助我吗 ?

You only need 1 var infowindow. 您只需要1个var infowindow。 This should not be declared within the for(). 不应在for()中声明它。 notice: there can only be one id="content" (id is unique). 注意:只能有一个id =“ content”(id是唯一的)。

Here is how to think about it: 这是如何考虑的:

  • When the client clicks on a marker, temporarily you store that marker to var currentMarker; 当客户端单击一个标记时,您会暂时将该标记存储到var currentMarker中; then you open the infowindow. 然后打开信息窗口。 This triggers infowindow.domready . 这将触发infowindow.domready。

  • When the dom of the infowindow is ready, only then you create the new var pano, and you give it the position of currentMarker (both the position of the window & the coordinates of the panoramic view). 信息窗口的dom准备就绪后,才可以创建新的var pano,并为其指定currentMarker的位置(包括窗口位置和全景视图的坐标)。

(notice to other scripters: this requires 'marker.png' in the same folder as index.php) code: (注意其他脚本编写者:这需要在与index.php相同的文件夹中使用“ marker.png”)代码:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Complex icons</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>
    var LocationData = [
        [53.13449, 18.04292, "1. ul. Gajowa 99, 85-700 Bydgoszcz" ], 
        [53.03783, 18.61368, "2. ul. Polna 17, 87-100 Torun"],
        [54.37890, 18.59159, "3. ul. Partyzantów 70, 80-952 Gdansk" ],
        [50.23122, 18.99280, "4. ul. Rolna 7, 40-555 Katowice" ],
        [50.06358, 19.97002, "5. ul. Cystersów 21, 31-553 Krakow" ],
        [50.01563, 21.97702, "6. ul. Matuszczaka 5, 35-083 Rzeszów" ],
        [51.20651, 22.58249, "7. ul. Sierpinskiego 26, 20-448 Lublin" ],
        [53.78115, 20.50410, "8. ul. Towarowa 4, 10-417 Olsztyn" ],
        [53.40699, 14.59046, "9. ul.Gdanska 15a, 70-661 Szczecin" ],
        [54.18990, 16.15861, "10. ul. Bohaterów W-wy 3, 75-211 Koszalin" ],
        [52.22472, 20.93692, "11. ul. Sowinskiego 28, 01-105 Warszawa" ]
    ];
    function initialize() {
      var map = new google.maps.Map(document.getElementById('map-canvas'));
      var contentString = '<div id="content" style="width:300px;height:300px;"></div>';
      var bounds = new google.maps.LatLngBounds();
      var currentMarker = null;
      var infowindow = new google.maps.InfoWindow({
        content: contentString
      });

      google.maps.event.addListener(infowindow, 'domready', function() {
        var pano = new google.maps.StreetViewPanorama(document.getElementById("content"), {
          navigationControl: true,
          navigationControlOptions: {style: google.maps.NavigationControlStyle.ANDROID},
          enableCloseButton: false,
          addressControl: false,
          linksControl: true
        });
        // set the position of the infoWindow (the div points to the marker)
        infowindow.setPosition(currentMarker.getPosition());
        // set the position of the panoramic view to the right coordinates
        pano.setPosition(currentMarker.getPosition());
        pano.setVisible(true);
      });

      for (var i in LocationData) {
        var p = LocationData[i];
        var latlng = new google.maps.LatLng(p[0], p[1]);
        bounds.extend(latlng);
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            title: p[2],
            icon: 'marker.png'
        });
        google.maps.event.addListener(marker, 'click', function() {
          currentMarker = this;         // store the marker that was clicked upon
          infowindow.open(map, this);
        });
      }
      map.fitBounds(bounds);
    }
    google.maps.event.addDomListener(window, 'load', initialize);
  </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM