【问题标题】:google maps popup info box when clicking marker单击标记时谷歌地图弹出信息框
【发布时间】:2013-03-22 22:13:52
【问题描述】:

我正在使用这个谷歌地图脚本。如果我希望在单击标记时弹出信息框怎么办?

你可以在这里试试这个脚本:http://clark-kim.se/preview/googlemaps2.html

我无法发布所有代码,但至少这是脚本:

<script>
          function initialize() {
            var map = new google.maps.Map(document.getElementById('map-canvas'), {
              mapTypeId: google.maps.MapTypeId.ROADMAP,
            });
            var defaultBounds = new google.maps.LatLngBounds(
                new google.maps.LatLng(-33.8902, 151.1759),
                new google.maps.LatLng(-33.8474, 151.2631));
            map.fitBounds(defaultBounds);

            var input = document.getElementById('target');
            var searchBox = new google.maps.places.SearchBox(input);
            var markers = [];

            google.maps.event.addListener(searchBox, 'places_changed', function() {
              var places = searchBox.getPlaces();

              for (var i = 0, marker; marker = markers[i]; i++) {
                marker.setMap(null);
              }

              markers = [];
              var bounds = new google.maps.LatLngBounds();
              for (var i = 0, place; place = places[i]; i++) {
                var image = {
                  url: place.icon,
                  size: new google.maps.Size(71, 71),
                  origin: new google.maps.Point(0, 0),
                  anchor: new google.maps.Point(17, 34),
                  scaledSize: new google.maps.Size(25, 25)
                };

                var marker = new google.maps.Marker({
                  map: map,
                  icon: image,
                  title: place.name,
                  position: place.geometry.location
                });

                markers.push(marker);

                bounds.extend(place.geometry.location);
              }

              map.fitBounds(bounds);
            });

            google.maps.event.addListener(map, 'bounds_changed', function() {
              var bounds = map.getBounds();
              searchBox.setBounds(bounds);
            });
          }
          google.maps.event.addDomListener(window, 'load', initialize);
        </script>

【问题讨论】:

  • 任何答案有帮助吗?如果是,请勾选一个,以便该主题可以被视为已关闭,如果不是,请提供一些反馈或您自己的答案

标签: google-maps popup infobox


【解决方案1】:
      google.maps.event.addListener(marker, 'click', function() {
            infowindow.setContent("Your content here");
            infowindow.open(map, this);
        }); 

您必须先创建一个信息窗口,然后在每次单击时关闭它并在另一端打开它

【讨论】:

    【解决方案2】:

    您是否检查过 Google Maps JavaScript API v3 的 example?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      相关资源
      最近更新 更多