sungpeng Posted May 6, 2009 Share Posted May 6, 2009 Check is there a php code to convert postal code to longitude and latitude? Thank Quote Link to comment https://forums.phpfreaks.com/topic/157050-php-code-to-convert-postal-code-to-longitude-and-latitude/ Share on other sites More sharing options...
RussellReal Posted May 6, 2009 Share Posted May 6, 2009 I'm sure there is, but your best bet would probably be to use js and flash with mapquest API.. and lat/long will be too much of a guess or estimate than an actual pin point location Quote Link to comment https://forums.phpfreaks.com/topic/157050-php-code-to-convert-postal-code-to-longitude-and-latitude/#findComment-827298 Share on other sites More sharing options...
sungpeng Posted May 6, 2009 Author Share Posted May 6, 2009 <?php // three parts to the querystring: q is address, output is the format, key is the GAPI $key = 'your key'; $location = 'singapore' $url = 'http://maps.google.com/maps/geo?q='.urlencode($location).'&output=xml&key='.$key; sleep(1); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER,0); curl_setopt($ch, CURLOPT_VERBOSE,0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_FRESH_CONNECT,1); // I'm telling google maps im using Chrome because this process will be automated in future uses curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.3.154.9 Safari/525.19'); // capture anything in output so script wont break ob_start(); $xml = curl_exec($ch); ob_get_clean(); // check for errors $error = curl_error($ch); echo $xml; ?> Result below Queensland, Australia 200 geocode Queensland, Australia AUAustraliaQLD 145.774,-19.70 How to break apart the echo $xml so that one variable is longitude only, one variable is latitude only. Do you have any javascript to recommend also? Thank Quote Link to comment https://forums.phpfreaks.com/topic/157050-php-code-to-convert-postal-code-to-longitude-and-latitude/#findComment-827309 Share on other sites More sharing options...
RussellReal Posted May 6, 2009 Share Posted May 6, 2009 u should be able to use simplexml_load_string assuming google gives you XML as a return and then probably do $xml->latitude Quote Link to comment https://forums.phpfreaks.com/topic/157050-php-code-to-convert-postal-code-to-longitude-and-latitude/#findComment-827314 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.