1

How can i get exact Latitude and longitude of user in php, without using IP, Any hope with google api?

  • 4
    I'm voting to close this question as off-topic because this about PHP and it has anything to do with Magento. – Rajeev K Tomy Jan 6 '17 at 5:40
8

By using the district name you can get the exact latitude and longitude. Please follow the below code:

$address = $_POST['district'];//here we can get the name of the district
$geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=false');
// We convert the JSON to an array
$geo = json_decode($geo, true);
// If everything is cool
if ($geo['status'] == 'OK') {
    // We set our values
    $latitude = $geo['results'][0]['geometry']['location']['lat'];
    $longitude = $geo['results'][0]['geometry']['location']['lng'];
    $resultjson = ['success' => 'OK', 'latitude' => $latitude, 'longtitude' => $longitude];
    echo json_encode($resultjson);
}
| improve this answer | |
4

You can get latitude and longitude by address

$address = '1600 Amphitheatre Parkway, Mountain View, CA 94043, USA';
$coordinates = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=true');
$coordinates = json_decode($coordinates);
$latitude = $coordinates->results[0]->geometry->location->lat;
$longitude = $coordinates->results[0]->geometry->location->lng;
| improve this answer | |
3

please try this code, hope it's helpful for you

<?php
// Address
$address = 'BTM 2nd Stage, Bengaluru, Karnataka 560076';
// Get JSON results from this request
$geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false');

// Convert the JSON to an array

$geo = json_decode($geo, true);

if ($geo['status'] == 'OK') {
  // Get Lat & Long
  $latitude = $geo['results'][0]['geometry']['location']['lat'];
  $longitude = $geo['results'][0]['geometry']['location']['lng'];
}
?>

For more info, please have a look following URLs:- Get Latitude and Longitude from Address using Google Maps API and PHP

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.