Jump to content

Latitude and Longtitude


Nodral

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.