Skip to content

Instantly share code, notes, and snippets.

@derekperkins
Created October 27, 2013 21:37
Show Gist options
  • Save derekperkins/7188166 to your computer and use it in GitHub Desktop.
Save derekperkins/7188166 to your computer and use it in GitHub Desktop.
MapQuest
<?php
function geocode($address) {
$apikey = "key=Fmjtd%7Cluub256a2u%2C2x%3Do5-9urwg6";
$url = "http://open.mapquestapi.com/geocoding/v1/address?";
$options = "&inFormat=kvp&outFormat=json&thumbMaps=false&maxResults=1";
$location = "&location=".urlencode($address);
$results = json_decode(file_get_contents($url.$apikey.$options.$location));
if(isset($results->results[0]->locations[0])) {
$place = $results->results[0]->locations[0]->latLng;
$place = [
"latitude" => $place->lat,
"longitude" => $place->lng
];
return $place;
}
}
function reverse_geocode($latitude, $longitude) {
$apikey = "key=Fmjtd%7Cluub256a2u%2C2x%3Do5-9urwg6";
$url = "http://open.mapquestapi.com/geocoding/v1/reverse?";
$options = "";
$location = "&location=".$latitude.",".$longitude;
$results = json_decode(file_get_contents($url.$apikey.$options.$location));
if(isset($results->results[0]->locations[0])) {
$place = $results->results[0]->locations[0];
$place = [
"city" => $place->adminArea5,
"county" => $place->adminArea4,
"state" => $place->adminArea3,
"postalCode" => $place->postalCode,
"country" => $place->adminArea1
];
return $place;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment