如何在php中将地址转换为Lng-Lat


How to convert address into Lng Lat in php

我很难找到如何在PHP中将输入的地址转换为经度和纬度。。。有没有办法转换它?

使用此

function getLatndLogtd($address) {
    $address = urlencode($address);
    $url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false";
    // Make the HTTP request
    $data = @file_get_contents($url);
    // Parse the json response
    $jsondata = json_decode($data,true);
    // If the json data is invalid, return empty array
    if (!check_status($jsondata))   return array();
    $LatLng = array(
        'lat' => $jsondata["results"][0]["geometry"]["location"]["lat"],
        'lng' => $jsondata["results"][0]["geometry"]["location"]["lng"],
    );
    return $LatLng;
}
/* 
* Check if the json data from Google Geo is valid 
*/
function check_status($jsondata) {
    if ($jsondata["status"] == "OK") return true;
    return false;
}
$address = 'address value';
$arrLogDet = getLatndLogtd($address);
echo "<pre>";
print_r($arrLogDet);
echo "</pre>";

检查此项:

$address is your input address
$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'];
}