在谷歌地图API v2中显示街道地址,现在需要在v3中

时间:2013-03-19 23:05:23

标签: google-maps google-maps-api-3 google-maps-api-2

我正在使用google maps api v2在地图上的 var address 行显示地址,我想在api v3中使用它。有谁知道如何制作谷歌地图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>

1 个答案:

答案 0 :(得分: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