问题
I want to get the longitude and latitude coordinates using php from the given address ($street, $barangay, $city and $province).
回答1:
You can use url:
http://maps.googleapis.com/maps/api/geocode/json?address=YOUR_ADDRESS
It's free.
You will get data in json encoded form which contain lat & long
回答2:
You can use Google Maps Geocoding API, here: https://developers.google.com/maps/documentation/geocoding/intro
It's free for: - 2,500 free requests per day - 10 requests per second
In order to use Google Geocoding API, use this library (MIT license): http://geocoder-php.org/
回答3:
Here is an example of PHP code to get the latitude and longitude values from Google Maps API, based on the town, city or country location. Check this tutorial and official documentation.
<?php
$url = "http://maps.google.com/maps/api/geocode/json?address=West+Bridgford&sensor=false®ion=UK";
$response = file_get_contents($url);
$response = json_decode($response, true);
//print_r($response);
$lat = $response['results'][0]['geometry']['location']['lat'];
$long = $response['results'][0]['geometry']['location']['lng'];
echo "latitude: " . $lat . " longitude: " . $long;
?>
The http://maps.google.com/maps/api/geocode/json
URL have 3 parameters: address (your main location), region and sensor that indicates whether or not the request will come from a device with a location sensor.
You can also check this related SO question. The community suggested to use curl
instead of file_get_contents
.
$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=India";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo $lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo $long = $response_a->results[0]->geometry->location->lng;
来源:https://stackoverflow.com/questions/36696235/how-can-i-get-the-longitude-and-latitude-coordinates-of-a-place-using-php-for-ge