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

时间:2015-11-25 13:00:58

标签: php google-maps

我很难找到如何将输入的地址转换为PHP中的经度和纬度...无论如何转换它?

2 个答案:

答案 0 :(得分:1)

使用此

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>";

答案 1 :(得分:0)

检查一下:

$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'];
}