2

我向您描述了我的需要,但如果您认为您对我的需要了解更多,请告诉我。

这是我的代码:

    <!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
      src="http://maps.googleapis.com/maps/api/js?key=*************************&sensor=true">
    </script>
    <script type="text/javascript">
      function initialize() {
        var myOptions = {
          center: new google.maps.LatLng(32.4279080, 53.6880460),
          zoom: 6,
          disableDefaultUI: true,
          disableDoubleClickZoom: true,
          draggable: false,
          scrollwheel: false,
          mapTypeId: google.maps.MapTypeId.ROADMAP   
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
            myOptions);

  /*google.maps.event.addListener(map, 'click', function() {
    alert('nothing');
  });*/
}
    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>
  </body>
</html>

编辑脚本::

function initialize() {
        var myOptions = {
          center: new google.maps.LatLng(32.4279080, 53.6880460),
          zoom: 6,
          disableDefaultUI: true,
          disableDoubleClickZoom: true,
          draggable: false,
          scrollwheel: false,
          mapTypeId: google.maps.MapTypeId.ROADMAP   
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
            myOptions);

 google.maps.event.addListener(map, 'click', function(event) {

geocoder.geocode({'latLng': event.latLng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (results[1]) {
          map.setZoom(11);
          marker = new google.maps.Marker({
              position: latlng,
              map: map
          });
          infowindow.setContent(results[1].formatted_address);
          infowindow.open(map, marker);
        }
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  });
}

我需要在城市圈上的鼠标点击事件中获取城市名称。那可能吗?

谢谢你。

4

1 回答 1

2

使用谷歌反向地理编码通过 latLng 了解该地点。以下功能有效。

 google.maps.event.addListener(map, 'click', function(event) {

geocoder.geocode({'latLng': event.latLng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (results[1]) {
          map.setZoom(11);
          marker = new google.maps.Marker({
              position: latlng,
              map: map
          });
          infowindow.setContent(results[1].formatted_address);
          infowindow.open(map, marker);
        }
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  });
于 2012-01-31T19:43:26.223 回答