September 21, 2016

How to get latitude & longitude from address or postcode string

function address_to_lat_lng($address){
$ret = array();
$address = urlencode($address);
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
//pree($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
//pree($response);
$response_a = json_decode($response);
$ret['lat'] = $response_a->results[0]->geometry->location->lat;
$ret['lng'] = $response_a->results[0]->geometry->location->lng;
return $ret;
}

Last updated: September 21, 2016