0

我正在使用下面的 google maps api v2 在地图上的var 地址行中显示地址,我想在 api v3 中使用它。有谁知道如何将其制作为 google maps api v3?

<script type="text/javascript">
    //<![CDATA[
   var geocoder;
   var map;
   var address = "425 West 53rd St,New York,NY,";
   // On page load, call this function
   function load()
   {
      // Create new map object
      map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
      // Create new geocoding object
      geocoder = new GClientGeocoder();
      // Retrieve location information, pass it to addToMap()
      geocoder.getLocations(address, addToMap);
   }
   // This function adds the point to the map
   function addToMap(response)
 {
   // Retrieve the object
   place = response.Placemark[0];
   // Retrieve the latitude and longitude
   point = new GLatLng(place.Point.coordinates[1],
                       place.Point.coordinates[0]);
   // Center the map on this point
   map.setCenter(point, 15);
   // Create a marker
    marker = new GMarker(point);
    // Add the marker to map
    map.addOverlay(marker);
   }    //]]>
    </script>

<script src="http://maps.google.com/maps?file=api&v=2&key=xxxxxxxx" type="text/javascript"></script><div id="map"></div>
4

1 回答 1

2

文档中的简单示例,使用您的地址而不是“输入字段”

  var geocoder;
  var map;
  var address = "425 West 53rd St,New York,NY,";
  function initialize() {
    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'), mapOptions);
    codeAddress();
  }

  function codeAddress() {
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

jsfiddle

于 2013-03-20T02:33:35.670 回答