Skip to content

Instantly share code, notes, and snippets.

@Tom-Millard
Created October 16, 2014 14:16
Show Gist options
  • Save Tom-Millard/e87bca03be3333eb7493 to your computer and use it in GitHub Desktop.
Save Tom-Millard/e87bca03be3333eb7493 to your computer and use it in GitHub Desktop.
Get the country based on latlong
// watch out for google api cut off
class googleMaps {
private $latlong = "";
private $json = "";
private $log = array();
private $break = false;
public function __construct($latlong) {
$this->latlong = $latlong;
try {
$this->json = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=$this->latlong&sensor=false");
$this->json = json_decode($this->json);
$this->log[] = "CONNECTION - CONNECTION MADE";
} catch(Exception $e){
$this->json = "";
$this->log[] = "CONNECTION - CONNECTION FAIL";
$this->break = true;
}
}
public function returnJsonDecode(){
return $this->json;
}
public function returnLog(){
return implode("<br />", $this->log);
}
public function parseAddressComponents(){
if($this->break)
return $this->returnLog();
$country = array();
$loop = $this->json->results;
foreach($loop as $sections){
foreach($sections->address_components as $loc){
if(in_array("country", $loc->types)){
$country[$loc->short_name] = $loc->long_name;
}
}
}
return $country;
}
public function returnBreak(){
return $this->break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment