-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGoogleMapsGeo.php
More file actions
116 lines (116 loc) · 4.32 KB
/
Copy pathGoogleMapsGeo.php
File metadata and controls
116 lines (116 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Clase para trabajar con la geocodificación de Google Maps API
*
* @author Inazio Claver
*/
class GoogleMapsGeo{
/**
* Geocodifica una dirección
* @param string $direccion
* @return array con latitud, longitud y dirección, o FALSE si hubo algún error
*/
function geocode($direccion){
// Convierte en URL la dirección
$direccion = urlencode($direccion);
// POST a la API de geolocalización de Google Maps
$url = "http://maps.google.com/maps/api/geocode/json?address={$address}";
// Recojo la respuesta JSON
$respuestaJSON = file_get_contents($url);
// Decodifico el JSON
$respuesta = json_decode($respuestaJSON, true);
// El parámetro de Status de la respuesta debe ser OK para permitir el geoposicionamiento
if ($respuesta['status'] == 'OK'){
// Capturo los datos (solo los que me interesan)
$latitud = $respuesta['results'][0]['geometry']['location']['lat'];
$longitud = $resputa['results'][0]['geometry']['location']['lng'];
$direccionFormateada = $respuesta['results'][0]['formatted_address'];
// Verifico si los datos están completos
if ($latitud && $longitud && $direccionFormateada){
// Meto los datos en un array
$geoDatos = array($latitud, $longitud, $direccionFormateada);
return $geoDatos;
}
else{
return false;
}
}
else{
return false;
}
}
/**
* Convierte latitud y longitud a dirección comprensible
* @param double $lat Latitud
* @param double $long Longitud
* @return string con la dirección formateada, o FALSE si hubo algún error
*/
function inverseGeocoding($lat, $lng){
// Convierte a URL las direcciones
$lat = urlencode($lat);
$lng = urlencode($lng);
// POST a la API de geolocalización de Google Maps
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lng";
// Recojo la respuesta JSON
$respuestaJSON = file_get_contents($url);
// Decodifico la respuesta
$respuesta = json_decode($respuestaJSON, true);
// El parámetro de status de la respuesta debe ser OK para permitir la recogida de datos
if ($respuesta['status'] == 'OK'){
$direccionFormateada = $respuesta['results'][0]['formatted_address'];
if ($direccionFormateada){
return $direccionFormateada;
}
else{
return false;
}
}
else{
return false;
}
}
/**
* Calcula el tiempo de viaje entre dos direcciones
* @param string $dirInicio Dirección desde la que se parte
* @param string $dirFin Dirección a la que se llega
* @return array con tiempo de viaje en string y timestamp, direccion, latitud y longitud de dirección de inicio y dirección final, o FALSE si hubo algún error
*/
function travelTime($dirInicio, $dirFin){
// Convierte en URL las direcciones
$dirInicio = urlencode($dirInicio);
$dirFin = urlencode($dirFin);
// POST a la API de direcciones de Google Maps
$url = "https://maps.googleapis.com/maps/api/directions/json?origin=$dirInicio&destination=$dirFin";
// Recojo la respuesta JSON
$respuestaJSON = file_get_contents($url);
// Decodifico el JSON
$respuesta = json_decode($respuestaJSON, true);
// El parámetro de Status de la respuesta debe ser OK para permitir la recogide de datos
if ($respuesta['status'] == 'OK'){
// Tiempo (lenguaje humano)
$tiempoViaje = $respuesta['routes'][0]['legs'][0]['duration']['text'];
// Tiempo en timestamp
$tiempoViajeTimestamp = $respuesta['routes'][0]['legs'][0]['duration']['value'];
// Posición inicio
$direccionInicio = $respuesta['routes'][0]['legs'][0]['start_address'];
$latitudInicio = $respuesta['routes'][0]['legs'][0]['start_location']['lat'];
$longitudInicio = $respuesta['routes'][0]['legs'][0]['start_location']['lng'];
// Posición final
$direccionFinal = $respuesta['routes'][0]['legs'][0]['end_address'];
$latitudFin = $respuesta['routes'][0]['legs'][0]['end_location']['lat'];
$longitudFin = $respuesta['routes'][0]['legs'][0]['end_location']['lng'];
// Valido si los campos están completos
if ($tiempoViaje && $tiempoViajeTimestamp && $direccionInicio && $latitudInicio && $longitudInicio && $direccionFinal && $latitudFin && $longitudFin){
$geoDatos = array($tiempoViaje, $tiempoViajeTimestamp, $direccionInicio, $latitudInicio, $longitudInicio, $direccionFinal, $latitudFin, $longitudFin);
return $geoDatos;
}
else{
return false;
}
}
else{
return false;
}
}
}
?>