(PHP) get Latitude/Longitude from an address

Convert an address to geocode Latitude/Longitude positioning with PHP and Google Maps

  1. <?php
  2. $address = ’201 S. Division St., Ann Arbor, MI 48104′; // Google HQ
  3. $prepAddr = str_replace(‘ ‘,‘+’,$address);
  4.  
  5. $geocode=file_get_contents(‘http://maps.google.com/maps/api/geocode/json?address=’.$prepAddr.‘&sensor=false’);
  6.  
  7. $output= json_decode($geocode);
  8.  
  9. $lat = $output->results[0]->geometry->location->lat;
  10. $long = $output->results[0]->geometry->location->lng;
  11.  
  12. echo $address.‘<br>Lat: ‘.$lat.‘<br>Long: ‘.$long;
  13.  
  14. ?>
This entry was posted in PHP and tagged , , , , , , , , , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?

  • Advertisement

  • Categories