Search code examples
google-mapsgoogle-maps-api-3google-maps-api-2

Showing street address in google maps API v2, need it in v3 now


I'm using google maps api v2 below to show address in var address line on the map, and I want to have this in api v3. Does anyone know how to make this as 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>

Solution

  • Simple example from documentation, with your address instead of the "input field"

      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