domingo, 16 de septiembre de 2018

PHP: Get Latitude/Longitude using Google Maps API


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$address = "Kathmandu, Nepal";
$url = "http://maps.google.com/maps/api/geocode/json?address=".urlencode($address);
<!--more-->
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$responseJson = curl_exec($ch);
curl_close($ch);
$response = json_decode($responseJson);
if ($response->status == 'OK') {
    $latitude = $response->results[0]->geometry->location->lat;
    $longitude = $response->results[0]->geometry->location->lng;
    echo 'Latitude: ' . $latitude;     
    echo '<br />';     
    echo 'Longitude: ' . $longitude;
} else {
    echo $response->status;
    var_dump($response);
    exit;
}

No hay comentarios:

Publicar un comentario