just do it

php百度地图api判断地址是否在多边形区域内

百度官方案例

根据用户地址判断是否在五环内
1.获取用户地址经纬度
2.获取五环多边行各个顶点经纬度数组
3.判断用户地址是否在多边形内

《php百度地图api判断地址是否在多边形区域内》
/**
* @desc 根据地址获取经纬度
* @param string $addr 地址
* @param string $city 地址所在城市
* @param string $ak 百度api密钥

*/

function GetLN($addr = ”,$city = ”,$ak = ”){

$addr = urlencode($addr);
$api_url = “http://api.map.baidu.com/geocoder/v2/?address=$addr&city=$city&output=json&ak=$ak”;
$json = file_get_contents($api_url);
$json_arr = json_decode($json);
return $json_arr;
}

/**
* @desc 根据两点间的经纬度计算距离
* @param float $lat 纬度值
* @param float $lng 经度值
*/
function getDistance($lat1, $lng1, $lat2, $lng2)
{
$earthRadius = 6367000; //approximate radius of earth in meters

/*
Convert these degrees to radians
to work with the formula
*/

$lat1 = ($lat1 * pi() ) / 180;
$lng1 = ($lng1 * pi() ) / 180;

$lat2 = ($lat2 * pi() ) / 180;
$lng2 = ($lng2 * pi() ) / 180;

/*
Using the
Haversine formula

http://en.wikipedia.org/wiki/Haversine_formula

calculate the distance
*/

$calcLongitude = $lng2 – $lng1;
$calcLatitude = $lat2 – $lat1;
$stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
$stepTwo = 2 * asin(min(1, sqrt($stepOne)));
$calculatedDistance = $earthRadius * $stepTwo;

return round($calculatedDistance);
}
// 判断点 是否在多边形 内
function isPointInPolygon($polygon,$lnglat){
$count = count($polygon);
$px = $lnglat[‘lat’];
$py = $lnglat[‘lng’];
$flag = FALSE;
for ($i = 0, $j = $count – 1; $i < $count; $j = $i, $i++) {
$sy = $polygon[$i][‘lng’];
$sx = $polygon[$i][‘lat’];
$ty = $polygon[$j][‘lng’];
$tx = $polygon[$j][‘lat’];
if ($px == $sx && $py == $sy || $px == $tx && $py == $ty)
return TRUE;
if ($sy < $py && $ty >= $py || $sy >= $py && $ty < $py) { $x = $sx + ($py – $sy) * ($tx – $sx) / ($ty – $sy); if ($x == $px) return TRUE; if ($x > $px)
$flag = !$flag;
}
}
return $flag;
}

$addr = GetLN(‘朝阳区霄云路鹏润大厦B座29层’,”,$ak);

$polygon = array(
array(
“lng” =>116.497492,
“lat” =>40.026915
),
array(
“lng” =>116.288222,
“lat” =>40.030451
),
array(
“lng” =>116.223832,
“lat” =>39.997737
),
array(
“lng” =>116.211184,
“lat” =>39.859634
),
array(
“lng” =>116.283623,
“lat” =>39.773621
),
array(
“lng” =>116.420453,
“lat” =>39.762973
),
array(
“lng” =>116.565332,
“lat” =>39.853431
),
array(
“lng” =>116.554983,
“lat” =>39.945539
),
array(
“lng” =>116.499791,
“lat” =>40.025147
)
);
$lnglat = array(
“lng” =>116.47340460859,
“lat” =>39.96493228002

);

$tt = isPointInPolygon($polygon,$lnglat);

点赞