Posted By

mmstudio on 01/25/11


Tagged

php google maps long address lat


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

mmcachran
nickian
luizlopes
manteca
adkatrit
Priestd09


Geocoding, from address to coordinates lat long


 / Published in: PHP
 

  1. // ------------------------------------------
  2. // converts a string with a stret address
  3. // into a couple of lat, long coordinates.
  4. // ------------------------------------------
  5. public function getLatLong($address){
  6. if (!is_string($address))die("All Addresses must be passed as a string");
  7. $_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address));
  8. $_result = false;
  9. if($_result = file_get_contents($_url)) {
  10. if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false;
  11. preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match);
  12. $_coords['lat'] = $_match[1];
  13. $_coords['long'] = $_match[2];
  14. }
  15. return $_coords;
  16. }

Report this snippet  

You need to login to post a comment.