Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I provide addresses instead of latitude and longitude to Google Maps?

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.

like image 720
Isamtron Avatar asked Jun 20 '10 03:06

Isamtron


People also ask

Can I add my address to Google Maps?

You can publicly add or edit addresses in Maps. Add mailing addresses, fix where packages would be delivered, or adjust pin locations.


1 Answers

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.

like image 78
Daniel Vassallo Avatar answered Sep 27 '22 22:09

Daniel Vassallo