0

谁能告诉我这段代码有什么问题?地图加载,但用户的点击操作不会打开 info_window 或将地图重新​​居中到点击的点。

每当用户单击地图时,我希望某个位置的地址作为 info_window 弹出。

<!DOCTYPE html>
<html>
  <head>
    <title>Accessing arguments in UI events</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
      #map-canvas {
        height: 60%; width: 60%
      }
    </style>

    <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry&key=MY_KEY&sensor=true">
    </script>

    <script type="text/javascript">
            var geocoder;
            var map;
            var info_win;
            var pos;
            var marker;
        function initialize() 
        {
            google.maps.visualRefresh = true;
            geocoder = new google.maps.Geocoder();
            var latlng = new google.maps.LatLng(-34.397, 150.644);
            var mapOptions = {
            zoom: 8,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

            google.maps.event.addListener(map, 'click', function(evt) {
                pos = evt.latLng;
                map.setCenter(pos);
            geocoder.geocode({'location': pos}, function(results, status)
                    {
                        if(status == google.maps.GeocoderStatus.OK)
                            {
                                marker = new google.maps.Marker
                                ({
                                    map: map,
                                    position: pos
                                });

                                info_win = new google.maps.InfoWindow
                                ({
                                    content: results[0].formatted_address,
                                });
                                info_win.open(map,marker);  
                            }
                        else
                            {
                            alert("Could not load");
                            }
                    });
            });
         }

  </script>

</head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>
4

1 回答 1

1

第一个问题:您正在将点击侦听器添加到初始化函数之外的地图中,因此在初始化地图之前添加了它:

    <script type="text/javascript">
            var geocoder;
            var map;
            var info_win;
            var pos;
            var marker;
        function initialize() 
        {
            google.maps.visualRefresh = true;
            geocoder = new google.maps.Geocoder();
            var latlng = new google.maps.LatLng(-34.397, 150.644);
            var mapOptions = {
            zoom: 8,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

        google.maps.event.addListener(map, 'click', function() {
            pos = map.getPosition();
            map.setCenter(pos);
            geocoder.geocode({'location': pos}, function(results, status)
                    {
                        marker = new google.maps.Marker
                        ({
                            map: map,
                            position: pos
                        });
                        info_win = new google.maps.InfoWindow
                        ({
                            content: results[0].formatted_address,
                        });
                    infowindow.open(map,marker);    
                    });
            });
         }

  </script>

之后你会发现google.maps.Map对象没有 getPosition 方法。它确实有一个 getCenter 方法,但您可能想改用click 事件的 latLng 属性

    google.maps.event.addListener(map, 'click', function(evt) {
        pos = evt.latLng;
        map.setCenter(pos);

并修复打开 info_win 以打开您创建的 google.maps.InfoWindow 的代码:

    info_win = new google.maps.InfoWindow
        ({
          content: results[0].formatted_address,
        });
    info_win.open(map,marker);    
于 2013-10-11T17:29:29.853 回答