0

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

您可以在这里尝试脚本: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>
4

2 回答 2

0

你检查过这个例子吗?谷歌地图 JavaScript API v3

于 2015-03-12T04:13:34.993 回答
0
      google.maps.event.addListener(marker, 'click', function() {
            infowindow.setContent("Your content here");
            infowindow.open(map, this);
        }); 

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

于 2013-03-22T22:30:37.557 回答