|
Server : Apache/2.4.62 System : FreeBSD fbsdweb2.web.rcn.net 14.1-RELEASE FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC amd64 User : www ( 80) PHP Version : 8.3.8 Disable Function : NONE Directory : /domains/owens.enteract/inc/libs/maps/ |
Upload File : |
<?php
/*
Set GOOGLE_API_KEY and set it in constants.
Google Geocoding API
http://code.google.com/apis/maps/documentation/geocoding/
GOOGLE RESPONSE CODES
200: Successful request (at least one placemark was returned).
400: Bad request (the server was unable to understand the request).
500: Server error (an unknown internal error occurred).
601: Missing query. This means the q parameter was not specified (or was an empty string).
602: Unknown address. This means that the request was performed but no placemarks were found.
603: Unavailable address. The given address could not be returned due to legal or contractual reasons.
610: An invalid Google Maps API key was specified.
620: Too many queries have been performed using the given API key.
STATUS CODES
* "OK" indicates that no errors occurred; the address was successfully parsed and at least one geocode was returned.
* "ZERO_RESULTS" indicates that the geocode was successful but returned no results. This may occur if the geocode was passed a non-existent address or a latlng in a remote location.
* "OVER_QUERY_LIMIT" indicates that you are over your quota.
* "REQUEST_DENIED" indicates that your request was denied, generally because of lack of a sensor parameter.
* "INVALID_REQUEST" generally indicates that the query (address or latlng) is missing.
*/
function geocodeAddress($theAddress){
$address = urlencode($theAddress);
//If you want an extended data set, change the output to "xml" instead of csv
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=" . $address . "&key=" . GOOGLE_API_KEY;
//Set up a CURL request, telling it not to spit back headers, and to throw out a user agent.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0); //Change this to a 1 to return headers
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$json = json_decode($data);
$status = $json->status;
if ($status === "OK"){
$theLat = $json->results[0]->geometry->location->lat;
$theLng = $json->results[0]->geometry->location->lng;
//Only works for US locations right now
foreach ($json->results[0]->address_components as $address) {
//Get state
if ($address->types[0] == "locality") {
$city = $address->short_name;
}else if ($address->types[0] == "administrative_area_level_1") {
$state = $address->short_name;
//Get country
}else if ($address->types[0] == "country") {
$country = $address->short_name;
}else if ($address->types[0] == "postal_code") {
$postal = $address->short_name;
}
}
//Clear invalid states/countries (US ONLY FOR NOW)
if ($country != "US" || !ValidationUtils::isValidUSState($state)){
$country = "";
$state = "";
$postal = "";
$status = "NON_US";
}
}
/*
echo $theLat . "<br />";
echo $theLng . "<br />";
echo $status . "<br />";
echo $country . "<br />";
echo $state . "<br />";
*/
@$obj->lat = $theLat;
$obj->long = $theLng;
$obj->status = $status;
$obj->country = $country;
$obj->state = $state;
$obj->city = $city;
$obj->postal = $postal;
return $obj;
}
?>