Skip to content

Instantly share code, notes, and snippets.

@michelmelo
Created September 26, 2017 14:49
Show Gist options
  • Save michelmelo/002922f96848edb9eb08d3482edade4c to your computer and use it in GitHub Desktop.
Save michelmelo/002922f96848edb9eb08d3482edade4c to your computer and use it in GitHub Desktop.
<?php
/**
* gMaps Class
*
* Pega as informações de latitude, longitude e zoom de um endereço usando a API do Google Maps
*
* @author Michel MElo
*/
class gMaps {
private $mapsKey;
function __construct($key = null) {
if (!is_null($key)) {
$this->mapsKey = $key;
}
}
function carregaUrl($url) {
if (function_exists('curl_init')) {
$cURL = curl_init($url);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_FOLLOWLOCATION, true);
$resultado = curl_exec($cURL);
curl_close($cURL);
} else {
$resultado = file_get_contents($url);
}
if (!$resultado) {
trigger_error('Não foi possível carregar o endereço: <strong>' . $url . '</strong>');
} else {
return $resultado;
}
}
function geoLocal($endereco) {
$url = "https://maps.googleapis.com/maps/api/geocode/json?key={$this->mapsKey}&address=" . urlencode($endereco);
$data = json_decode($this->carregaUrl($url));
if ($data->status === 'OK') {
return $data->results[0]->geometry->location;
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment