Tuesday 14 August 2018

How to get Latitude and Longitude from Country or city using PHP

How to get latitude and longitude from address in php

You can use country name for get Latitude and Longitude and also use full address instead of city or country
$address = 'india';
Or also you can use CIty name for get Latitude and Longitude
$address = 'Junagadh';
Or you can also use city, state and Country togther.
$address = 'Junagadh+Gujarat+India';
There are two method for get Latitude and Longitude
First is :
$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$address."&sensor=false";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);

// If Status Code is ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED or INVALID_REQUEST
if ($response['status'] != 'OK') {
 return null;
}

//print_r($response);
//print_r($response['results'][0]['geometry']['location']);

$latLng = $response['results'][0]['geometry']['location'];

$lat = $latLng['lat'];
$lng = $latLng['lng']; 
OR
Second method is:
$geocode_stats = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=india&sensor=false");

$output_deals = json_decode($geocode_stats);

$latLng = $output_deals->results[0]->geometry->location;

$lat = $latLng->lat;
$lng = $latLng->lng; 

0 comments:

Post a Comment