fahidjavid
12/12/2018 - 7:03 AM

Getting the Google Maps Latitude and Longitude of an Address

Getting the Google Maps Latitude and Longitude of an Address

<?php

$address = 'Visalia, CA, USA'; // any address you want latitude and longitude of

$address_encoded = urlencode($address);
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=". $address_encoded ."&key=YOUR_API_KEY";
$result = file_get_contents($url);
$geocode = json_decode($result, true); // returing json of all information about given address
$location = $geocode['results'][0]['geometry']['location'];

$location_lat = $location['lat'];
$location_lng = $location['lng'];

// google maps documentaiotn referrence https://developers.google.com/maps/documentation/geocoding/start