百度地圖PC端判斷用戶是否在配送范圍內(nèi)
最近接了個(gè)項(xiàng)目,其中有項(xiàng)目需求是這樣的:
在pc端設(shè)置商家的配送范圍,用戶在下單時(shí),根據(jù)用戶設(shè)置的配送地點(diǎn)判斷是否在可配送范圍內(nèi),并給用戶相應(yīng)的提示。
下面說下我的實(shí)現(xiàn)思路:
1.用百度地圖在PC端設(shè)置配送范圍,可拖拽選擇
2.根據(jù)用戶設(shè)置的配送地址判斷是否在配送范圍內(nèi)
一、百度地圖PC端獲取范圍
改動(dòng)百度地圖官網(wǎng)的demo,設(shè)置配送范圍。
思路:獲取多邊形的頂點(diǎn),以json的形式保存到數(shù)據(jù)庫(kù)。
百度API關(guān)于多邊形覆蓋物:
構(gòu)造函數(shù):
Polygon(points:Array<Point>[, opts:PolygonOptions]) 創(chuàng)建多邊形覆蓋物
方法:
setPath(path:Array<Point>) none 設(shè)置多邊型的點(diǎn)數(shù)組(自1.2新增)
getPath() Array<Point> 返回多邊型的點(diǎn)數(shù)組(自1.2新增)
實(shí)現(xiàn):

主要代碼:
//設(shè)置配送范圍
function setRange(_point, _ppoints){
var polygon = new BMap.Polygon(_ppoints, {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5}); //創(chuàng)建多邊形
map.addOverlay(polygon); //增加多邊形
polygon.enableEditing(); //允許編輯
polygon.addEventListener("lineupdate",function(e){
var rangeArr = polygon.getPath();
$("#distributeRange").val(JSON.stringify(rangeArr));
});
}
以上代碼主要是監(jiān)聽 lineupdate 事件,每一次拖拽百度地圖回調(diào)函數(shù)將返回的多邊形的頂點(diǎn),然后通過JSON.stringify方法轉(zhuǎn)為string類型存在一個(gè)標(biāo)簽里面,以待后續(xù)的表單提交操作。
二、判斷點(diǎn)是否在范圍內(nèi)
去網(wǎng)上看了一下,判斷點(diǎn)是否在配送范圍內(nèi)的方法很多,大概采用的是射線法。
但是有一些方法沒有考慮全面,導(dǎo)致有的情況判斷不夠準(zhǔn)確。
在百度地圖的GeoUtils里面找到了“判斷點(diǎn)是否多邊形內(nèi)”這個(gè)方法。
因?yàn)槲沂切枰诤蠖俗雠袛啵缓笾苯影裫s轉(zhuǎn)化成了java,測(cè)試百發(fā)百中,欣喜?。ê竺娓缴蠝y(cè)試方法)
/**
* 判斷點(diǎn)是否在多邊形內(nèi)
* @param point 檢測(cè)點(diǎn)
* @param pts 多邊形的頂點(diǎn)
* @return 點(diǎn)在多邊形內(nèi)返回true,否則返回false
*/
public static boolean IsPtInPoly(Point2D.Double point, List<Point2D.Double> pts){
int N = pts.size();
boolean boundOrVertex = true; //如果點(diǎn)位于多邊形的頂點(diǎn)或邊上,也算做點(diǎn)在多邊形內(nèi),直接返回true
int intersectCount = 0;//cross points count of x
double precision = 2e-10; //浮點(diǎn)類型計(jì)算時(shí)候與0比較時(shí)候的容差
Point2D.Double p1, p2;//neighbour bound vertices
Point2D.Double p = point; //當(dāng)前點(diǎn)
p1 = pts.get(0);//left vertex
for(int i = 1; i <= N; ++i){//check all rays
if(p.equals(p1)){
return boundOrVertex;//p is an vertex
}
p2 = pts.get(i % N);//right vertex
if(p.x < Math.min(p1.x, p2.x) || p.x > Math.max(p1.x, p2.x)){//ray is outside of our interests
p1 = p2;
continue;//next ray left point
}
if(p.x > Math.min(p1.x, p2.x) && p.x < Math.max(p1.x, p2.x)){//ray is crossing over by the algorithm (common part of)
if(p.y <= Math.max(p1.y, p2.y)){//x is before of ray
if(p1.x == p2.x && p.y >= Math.min(p1.y, p2.y)){//overlies on a horizontal ray
return boundOrVertex;
}
if(p1.y == p2.y){//ray is vertical
if(p1.y == p.y){//overlies on a vertical ray
return boundOrVertex;
}else{//before ray
++intersectCount;
}
}else{//cross point on the left side
double xinters = (p.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;//cross point of y
if(Math.abs(p.y - xinters) < precision){//overlies on a ray
return boundOrVertex;
}
if(p.y < xinters){//before ray
++intersectCount;
}
}
}
}else{//special case when ray is crossing through the vertex
if(p.x == p2.x && p.y <= p2.y){//p crossing over p2
Point2D.Double p3 = pts.get((i+1) % N); //next vertex
if(p.x >= Math.min(p1.x, p3.x) && p.x <= Math.max(p1.x, p3.x)){//p.x lies between p1.x & p3.x
++intersectCount;
}else{
intersectCount += 2;
}
}
}
p1 = p2;//next ray left point
}
if(intersectCount % 2 == 0){//偶數(shù)在多邊形外
return false;
} else { //奇數(shù)在多邊形內(nèi)
return true;
}
}
主要是判斷和這個(gè)方法的可行性。
為此寫了個(gè)測(cè)試方法。
思路:獲取一個(gè)多邊形的頂點(diǎn),然后隨機(jī)點(diǎn)一個(gè)點(diǎn)
1.調(diào)用百度地圖的方法,判斷該點(diǎn)是否在范圍內(nèi)
2.根據(jù)百度地圖獲取的那個(gè)店的經(jīng)緯度,自己程序判斷是否在范圍內(nèi)
調(diào)用百度地圖的方法:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GeoUtils示例</title>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2"></script>
<script type="text/javascript" src="http://api.map.baidu.com/library/GeoUtils/1.2/src/GeoUtils_min.js"></script>
<style type="text/css">
table {
font-size: 14px;
}
</style>
</head>
<body>
<div style="float:left;width:600px;height:500px;border:1px solid gray" id="container"></div>
<div style="float:left;width:300px;height:500px;border:1px solid gray" id="control">
<table style="width:100%;">
<tr>
<td colspan="2">判斷點(diǎn)是否在多邊形內(nèi):</td>
</tr>
<tr>
<td><input type="button" value="多邊形1" onclick="polygon1()" /></td>
</tr>
<tr>
<td><input type="button" value="多邊形2" onclick="polygon2()" /></td>
</tr>
<tr>
<td>經(jīng)度<input type="text" value="" id="lng"></td>
</tr>
<tr>
<td>緯度<input type="text" value="" id="lat"></td>
</tr>
<tr>
<td>結(jié)果:</td>
</tr>
<tr>
<td><p id="result" style="color:red"></p></td>
</tr>
<table>
</div>
</body>
</html>
<script type="text/javascript">
var map = new BMap.Map("container");
var pt = new BMap.Point(116.404, 39.915);
var mkr = new BMap.Marker(pt);
var ply; //多邊形
map.centerAndZoom(pt, 16);
map.enableScrollWheelZoom(); //開啟滾動(dòng)縮放
map.enableContinuousZoom(); //開啟縮放平滑
//初始化為多邊形1
polygon1();
//生成多邊形1
function polygon1() {
var pts = [];
var pt1 = new BMap.Point(116.395, 39.910);
var pt2 = new BMap.Point(116.394, 39.914);
var pt3 = new BMap.Point(116.403, 39.920);
var pt4 = new BMap.Point(116.402, 39.914);
var pt5 = new BMap.Point(116.410, 39.913);
pts.push(pt1);
pts.push(pt2);
pts.push(pt3);
pts.push(pt4);
pts.push(pt5);
ply = new BMap.Polygon(pts);
//演示:將面添加到地圖上
map.clearOverlays();
map.addOverlay(ply);
}
//生成多邊形2
function polygon2() {
var pts = [];
var pt1 = new BMap.Point(116.395, 39.910);
var pt2 = new BMap.Point(116.394, 39.914);
var pt3 = new BMap.Point(116.396, 39.919);
var pt4 = new BMap.Point(116.406, 39.920);
var pt5 = new BMap.Point(116.410, 39.913);
pts.push(pt1);
pts.push(pt2);
pts.push(pt3);
pts.push(pt4);
pts.push(pt5);
ply = new BMap.Polygon(pts);
//演示:將多邊形添加到地圖上
map.clearOverlays();
map.addOverlay(ply);
}
map.addEventListener("click", function (e) {
mkr.setPosition(e.point);
map.addOverlay(mkr);
//將點(diǎn)擊的點(diǎn)的坐標(biāo)顯示在頁(yè)面上
document.getElementById("lng").value = e.point.lng;
document.getElementById("lat").value = e.point.lat;
InOrOutPolygon(e.point.lng, e.point.lat);
});
function InOrOutPolygon(lng, lat){
var pt = new BMap.Point(lng, lat);
var result = BMapLib.GeoUtils.isPointInPolygon(pt, ply);
if (result == true) {
document.getElementById("result").innerHTML = "點(diǎn)在多邊形內(nèi)";
} else {
document.getElementById("result").innerHTML = "點(diǎn)在多邊形外";
}
}
</script>
界面如下:

在頁(yè)面上點(diǎn)擊一個(gè)點(diǎn)后,獲取了該點(diǎn)的坐標(biāo)(用于自己的方法測(cè)試),并調(diào)用了 InOrOutPolygon 來判斷了該店是否在此范圍內(nèi)。
后臺(tái)的測(cè)試方法:
// 測(cè)試一個(gè)點(diǎn)是否在多邊形內(nèi)
public static void main(String[] args) {
Point2D.Double point = new Point2D.Double(116.404072, 39.916605);
List<Point2D.Double> pts = new ArrayList<Point2D.Double>();
pts.add(new Point2D.Double(116.395, 39.910));
pts.add(new Point2D.Double(116.394, 39.914));
pts.add(new Point2D.Double(116.403, 39.920));
pts.add(new Point2D.Double(116.402, 39.914));
pts.add(new Point2D.Double(116.410, 39.913));
if(IsPtInPoly(point, pts)){
System.out.println("點(diǎn)在多邊形內(nèi)");
}else{
System.out.println("點(diǎn)在多邊形外");
}
}
經(jīng)過測(cè)試,結(jié)果滿意。
總結(jié),實(shí)現(xiàn)的過程最重要是保存那些頂點(diǎn),并根據(jù)那些保存的頂點(diǎn)(有一定的順序),來判斷一個(gè)點(diǎn)是否在這些頂點(diǎn)圍成的多邊形內(nèi)。
感覺百度地圖還是很好用的。API很全,而且都配有demo,非常利于我們開發(fā)者。
以上內(nèi)容是針對(duì)百度地圖PC端判斷用戶是否在配送范圍內(nèi)的相關(guān)知識(shí),希望對(duì)大家有所幫助。
相關(guān)文章
iOS實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
iOS App的設(shè)計(jì)模式開發(fā)中對(duì)State狀態(tài)模式的運(yùn)用
這篇文章主要介紹了iOS App的設(shè)計(jì)模式開發(fā)中對(duì)State狀態(tài)模式的運(yùn)用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
在iOS App中實(shí)現(xiàn)地理位置定位的基本方法解析
這篇文章主要介紹了在iOS App中實(shí)現(xiàn)地理位置定位的基本方法解析,包括獲取當(dāng)前位置和計(jì)算兩點(diǎn)間距離等基本功能的實(shí)現(xiàn),需要的朋友可以參考下2016-05-05
iOS 斷點(diǎn)上傳文件的實(shí)現(xiàn)方法
這項(xiàng)目開發(fā)中,有時(shí)候我們需要將本地的文件上傳到服務(wù)器,簡(jiǎn)單的幾張圖片還好,但是針對(duì)iPhone里面的視頻文件進(jìn)行上傳,為了用戶體驗(yàn),我們有必要實(shí)現(xiàn)斷點(diǎn)上傳。這篇文章主要介紹了iOS 斷點(diǎn)上傳文件的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-12-12
iOS開發(fā)TableView網(wǎng)絡(luò)請(qǐng)求及展示預(yù)加載實(shí)現(xiàn)示例
這篇文章主要為大家介紹了iOS開發(fā)TableView網(wǎng)絡(luò)請(qǐng)求及展示預(yù)加載實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
詳解iOS開發(fā)中UITableview cell 頂部空白的多種設(shè)置方法
這篇文章主要介紹了詳解iOS開發(fā)中UITableview cell 頂部空白的多種設(shè)置方法的相關(guān)資料,需要的朋友可以參考下2016-04-04
IOS正則表達(dá)式之驗(yàn)證密碼身份證手機(jī)號(hào)
這篇文章主要介紹了IOS正則表達(dá)式之驗(yàn)證密碼身份證手機(jī)號(hào)的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10
IOS 開發(fā)之ObjectiveC的變量類型的字符代表
這篇文章主要介紹了IOS 開發(fā)之ObjectiveC的變量類型的字符代表的相關(guān)資料,這里舉例說明如何使用Objective的變量類型的字符,幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08

