17

I'm building an application where users provide the addresses for their listings. It's certainly not practical to ask a simple user to provide the latitude and longitude for each address he provides!

Can I provide addresses to Google Maps API instead? If so, how?

Thanks.

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
Isamtron
  • 613
  • 2
  • 8
  • 13
  • @Ben: Thanks for retagging, but I think the `google-maps-api-v3` tag could be important here, to differentiate between the v2 API. The v2 API is still very popular. – Daniel Vassallo Jun 20 '10 at 22:19
  • Oh, sorry. It seemed like there were about five roughly-equivalent tags it and I guess I accidentally removed some information. Thanks for the heads up! – Sophie Alpert Jun 21 '10 at 00:18

2 Answers2

29

Yes of course. That can be done very easily, using the Geocoding Services provided by the Google Maps JavaScript API. Consider the following example:

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps Geocoding Demo 1</title> 
   <script src="http://maps.google.com/maps/api/js?sensor=false" 
           type="text/javascript"></script> 
</head> 
<body> 
   <div id="map" style="width: 400px; height: 300px;"></div> 

   <script type="text/javascript"> 

   var address = 'London, UK';

   var map = new google.maps.Map(document.getElementById('map'), { 
       mapTypeId: google.maps.MapTypeId.TERRAIN,
       zoom: 6
   });

   var geocoder = new google.maps.Geocoder();

   geocoder.geocode({
      'address': address
   }, 
   function(results, status) {
      if(status == google.maps.GeocoderStatus.OK) {
         new google.maps.Marker({
            position: results[0].geometry.location,
            map: map
         });
         map.setCenter(results[0].geometry.location);
      }
   });

   </script> 
</body> 
</html>

Screenshot:

Google Maps v3 Geocoding Demo

You can simply substitute 'London, UK' from the address variable to any location that supports geocoding in Google Maps.

Michel Ayres
  • 5,891
  • 10
  • 63
  • 97
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • How can this be refined, often I provide a street name in the address and it still drops a pin locating to the city. – Mike Kormendy Jun 23 '15 at 14:14
  • The Geocoding API free plan quota allows only one request per day. This solution need payments (5 $ per 1000 requests): https://developers.google.com/maps/documentation/geocoding/usage-and-billing?hl=en_US – Vadym Apr 29 '19 at 22:43
0

Could be refined if you provide the address with the format : var address = '# Street, City'; var address = '127 Ledbury Rd, Notting Hill, London';

fasst
  • 1
  • 1