-1
我正在爲我公司的供應商創建覆蓋圖,然後需要檢查我們的客戶中有多少人生活在整體覆蓋範圍之外。供應商的覆蓋區域始終是一個圓圈。將圓圈組合爲多邊形而沒有與公共區域相交
我使用this解決方案將我們供應商的覆蓋區域合併爲一個多邊形,以便我可以使用containsLocation function來比較客戶的位置。問題是containsLocation函數將一些重疊的覆蓋區域標識爲在覆蓋範圍之外。以下是一個示例,其中該引腳應該保持隱形,因爲它位於2個供應商的覆蓋範圍內。 Overlapping Vendor Coverage Example
function updateMap(){
var radius = $('#radius').val();
$.ajax({
url: "",
dataType: "json",
type: "POST",
data: $('#dataForm').serialize()
}).done(function(result){
for(var i=0; i<result.length; i++){
center = {lat: parseFloat(result[i].Latitude), lng: parseFloat(result[i].Longitude)};
shapeArray.push(drawCircle(center, (radius), 1));
}
coverage = new google.maps.Polygon({
paths: shapeArray,
strokeColor: "#ff0000",
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: "#ff0000",
fillOpacity: 0.35,
map: map
});
});
}
function drawCircle(point, radius, dir)
{
var d2r = Math.PI/180; // degrees to radians
var r2d = 180/Math.PI; // radians to degrees
var earthsradius = 3963; // 3963 is the radius of the earth in miles
var points = 32;
// find the raidus in lat/lon
var rlat = (radius/earthsradius) * r2d;
var rlng = rlat/Math.cos(point.lat * d2r);
var extp = new Array();
if (dir==1) {var start=0;var end=points+1} // one extra here makes sure we connect the
else{var start=points+1;var end=0}
for (var i=start; (dir==1 ? i < end : i > end); i=i+dir)
{
var theta = Math.PI * (i/(points/2));
ey = point.lng + (rlng * Math.cos(theta)); // center a + radius x * cos(theta)
ex = point.lat + (rlat * Math.sin(theta)); // center b + radius y * sin(theta)
extp.push(new google.maps.LatLng(ex, ey));
}
return extp;
}
function showUncovered(el){
if(!el.checked){
for(var i=0; i<markerArray.length; i++){
markerArray[i].setVisible(false);
}
return;
}
for(var i=0;i<markerArray.length;i++){
var pos = markerArray[i].position;
var isAffected = false;
if(!google.maps.geometry.poly.containsLocation(pos,coverage)){
markerArray[i].setVisible(true);
}
}
}
你說「引腳應該保持不可見」,但你的代碼基本上是說'if containsLocation(){setVisible(true)}' - 不應該是另一種方式嗎? 'markerArray [i] .setVisible(false);'將其隱藏起來 – duncan
我的歉意。地圖最終需要能夠在覆蓋區域內部和外部的引腳之間來回翻轉,並且我必須在我陳述的情況下錯誤地複製/粘貼我的代碼。代碼已更新。 –
這是我從指出的例子中分出的一個小提琴,它將控制器中的containsLocation的值。請注意,單擊多邊形圓相交的位置時,它將返回false。 http://jsfiddle.net/qrvvctja/ –