Last active
July 18, 2017 10:22
-
-
Save scamba/03100abe7ccdf68b1e28e42df5614498 to your computer and use it in GitHub Desktop.
Small library of functions to show how to get Google Maps location data from the coordinates (latitude and longitude).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
-- ============================================================================== | |
-- Author: Sergio Camba (https://github.com/scamba) | |
-- Technologies: PHP & Google Maps API | |
-- Create date: 18/07/2017 | |
-- Update date: 18/07/2017 | |
-- Description: Small library of functions to show how to get Google Maps | |
location data from the coordinates (latitude and longitude). | |
-- ============================================================================== | |
*/ | |
/** | |
* get_fullArray -> Get an array full of location data. | |
* | |
* @param $lat (float/string) -> latitude | |
* @param $lon (float/string) -> longitude | |
* @param $lang (string) -> language ( 'es', 'en', 'ru', etc ) | |
* @return array | |
*/ | |
function get_fullArray($lat, $lon, $lang) | |
{ | |
$url = "http://maps.googleapis.com/maps/api/geocode/json?language=" .$lang. "&sensor=false&latlng=" .$lat. "," .$lon; | |
return json_decode( file_get_contents( $url ) ); | |
} | |
/** | |
* get_parts -> Get each parts from array. | |
* | |
* @param $type (string) -> item type | |
* @return string | |
*/ | |
function get_parts( $type ) | |
{ | |
global $json; | |
switch ( strtolower( $type ) ) { | |
case 'calle': | |
$type = 'route'; | |
break; | |
case 'numero': | |
$type = 'street_number'; | |
break; | |
case 'localidad': | |
$type = 'administrative_area_level_4'; | |
break; | |
case 'comarca': | |
$type = 'administrative_area_level_3'; | |
break; | |
case 'provincia': | |
$type = 'administrative_area_level_2'; | |
break; | |
case 'comunidad': | |
$type = 'administrative_area_level_1'; | |
break; | |
case 'pais': | |
$type = 'country'; | |
break; | |
case 'codigo postal': | |
$type = 'postal_code'; | |
break; | |
case 'barrio': | |
$type = 'neighborhood'; | |
break; | |
case 'zona': | |
$type = 'sublocality'; | |
break; | |
case 'codigo comunidad': | |
$type = 'administrative_area_level_1'; | |
foreach ( $json->results as $result ) { | |
foreach( $result->address_components as $addressPart ) { | |
if( in_array( $type, $addressPart->types ) ) | |
return $addressPart->short_name; | |
} | |
} | |
break; | |
case 'codigo pais': | |
$type = 'country'; | |
foreach ( $json->results as $result ) { | |
foreach( $result->address_components as $addressPart ) { | |
if( in_array( $type, $addressPart->types ) ) | |
return $addressPart->short_name; | |
} | |
} | |
break; | |
case 'direccion completa': | |
foreach ( $json->results as $result ) { | |
return $result->formatted_address; | |
} | |
break; | |
} | |
$aux = "Not available"; | |
foreach ( $json->results as $result ) { | |
foreach( $result->address_components as $addressPart ) { | |
if( in_array( $type, $addressPart->types ) ) | |
$aux = $addressPart->long_name; | |
} | |
} | |
return $aux; | |
} | |
$coords = "40.420385, -3.701005"; // Change this to show your own lat/lon pair (keep format). | |
$aux = explode( ',', $coords ); | |
$lat = trim( $aux[0] ); | |
$lon = trim( $aux[1] ); | |
$lang = 'es'; | |
$json = get_fullArray( $lat, $lon, $lang ); | |
echo ' | |
<style> | |
table, th, td { | |
border: 1px solid black; | |
border-collapse: collapse; | |
} | |
th, td { | |
padding: 5px; | |
text-align: left; | |
} | |
</style> | |
<table style="max-width:800px;"> | |
<tr> | |
<th colspan="2">Datos de las coordenadas <b>[ '.$lat.', ' .$lon.' ]</b> para el idioma: <b>\''.$lang.'\'</b></th> | |
</tr> | |
<tr> | |
<td>Dirección completa</td> | |
<td>'.get_parts( "direccion completa" ).'</td> | |
</tr> | |
<tr> | |
<td>Calle</td> | |
<td>'.get_parts( "calle" ).'</td> | |
</tr> | |
<tr> | |
<td>Número</td> | |
<td>'.get_parts( "numero" ).'</td> | |
</tr> | |
<tr> | |
<td>Barrio</td> | |
<td>'.get_parts( "barrio" ).'</td> | |
</tr> | |
<tr> | |
<td>Zona</td> | |
<td>'.get_parts( "zona" ).'</td> | |
</tr> | |
<tr> | |
<td>Código postal</td> | |
<td>'.get_parts( "codigo postal" ).'</td> | |
</tr> | |
<tr> | |
<td>Localidad</td> | |
<td>'.get_parts( "localidad" ).'</td> | |
</tr> | |
<tr> | |
<td>Comarca</td> | |
<td>'.get_parts( "comarca" ).'</td> | |
</tr> | |
<tr> | |
<td>Provincia</td> | |
<td>'.get_parts( "provincia" ).'</td> | |
</tr> | |
<tr> | |
<td>Comunidad</td> | |
<td>'.get_parts( "comunidad" ).'</td> | |
</tr> | |
<tr> | |
<td>Código comunidad</td> | |
<td>'.get_parts( "codigo comunidad" ).'</td> | |
</tr> | |
<tr> | |
<td>País</td> | |
<td>'.get_parts( "pais" ).'</td> | |
</tr> | |
<tr> | |
<td>Código país</td> | |
<td>'.get_parts( "codigo pais" ).'</td> | |
</tr> | |
</table> | |
'; | |
//print_r( $json ); // Uncomment to check full results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment