Nodral Posted October 28, 2011 Share Posted October 28, 2011 Hi All I've a huge database of addresses and I need to convert them to Latitude and Longtitude to enable me to do some geopositioning and distances etc. I've tried the following code, but just get a blank result <?php $address="high+street+leicester+england"; $url = 'http://maps.google.com/maps/geo?q='.$address.'&output=json&oe=utf8&sensor=false&key='.$api_key; $data = @file_get_contents($url); $jsondata = json_decode($data,true); if(is_array($jsondata )&& $jsondata ['Status']['code']==200){ $lat = $jsondata ['Placemark'][0]['Point']['coordinates'][0]; $lon = $jsondata ['Placemark'][0]['Point']['coordinates'][1]; } echo "hello".$lat . " " . $lon; in my php.ini ; Whether to allow the treatment of URLs (like http:// or ftp://) as files. allow_url_fopen = On ; Whether to allow include/require to open URLs (like http:// or ftp://) as files. allow_url_include = On Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/249998-latitude-and-longtitude/ Share on other sites More sharing options...
Adam Posted October 28, 2011 Share Posted October 28, 2011 Perhaps remove the error suppressing "@" before the file_get_contents() call? Quote Link to comment https://forums.phpfreaks.com/topic/249998-latitude-and-longtitude/#findComment-1283070 Share on other sites More sharing options...
Nodral Posted October 31, 2011 Author Share Posted October 31, 2011 Ok, so now I have this... <?php function getLatLong($address){ if (!is_string($address))die("All Addresses must be passed as a string"); $_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address)); $_result = false; if($_result = file_get_contents($_url)) { if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false; preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match); $_coords['lat'] = $_match[1]; $_coords['long'] = $_match[2]; } return $_coords; } $address="high+street+leicester+england"; $output=getLatLong($address); foreach($output as $value){ echo $value."<br>"; } and I get this error Warning: file_get_contents(http://maps.google.com/maps?output=js&q=high%2Bstreet%2Bleicester%2Bengland) [function.file-get-contents]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in D:\Documents\AI24\Web\visionnet\Geo\test.php on line 6 Warning: Invalid argument supplied for foreach() in D:\Documents\AI24\Web\visionnet\Geo\test.php on line 20 I understand the Foreach error, any ideas how I can sort this lat & long issue? Quote Link to comment https://forums.phpfreaks.com/topic/249998-latitude-and-longtitude/#findComment-1283623 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.