Geocoding, from address to coordinates lat long


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.