我正在尝试使用 containsLocation 选项计算2个多边形的公共字段,以使大量相同的小字段仅放置在公共字段中。我认为 containsLocation( this ,pole1)有问题。我尝试了许多其他方法,仍然没有工作。如果你已经知道如何计算复杂的多边形公共字段,你也可以在这里写下来。
问题
var x,y,countmarkers;countmarkers=0;
for(x=52.8;x<=54;x=x+0.15){
for(y=12.6;y<=19;y=y+0.25) {
var point = { lat: x, lng: y};
if (google.maps.geometry.poly.containsLocation(point, pole1)) {
if (google.maps.geometry.poly.containsLocation(point, pole2)){
var marker = new google.maps.Marker({
position: new google.maps.LatLng(x, y),
map: map
});
countmarkers++;
}
}
}
}
完整代码
var map;
function initialize() {
var mapOptions = {
mapTypeId: google.maps.MapTypeId.TERRAIN,
center: {lat: 52.597060, lng: 18.516048},
zoom: 6
};
var map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
var pole0;
pole0 = [
[
new google.maps.LatLng(54, 14),
new google.maps.LatLng(53, 14),
new google.maps.LatLng(53, 15),
new google.maps.LatLng(54, 15)],
[
new google.maps.LatLng(54, 15),
new google.maps.LatLng(53, 13),
new google.maps.LatLng(53, 17),
new google.maps.LatLng(54, 17)]
];
var pole2,pole1;
pole1 = new google.maps.Polygon({
//map: map,
paths: pole0[0],
strokeColor: '#000000',
strokeWeight: 4,
strokeOpacity: 1,
fillColor: '#ff0000',
fillOpacity: 0.5
});
pole2 = new google.maps.Polygon({
//map: map,
paths: pole0[1],
strokeColor: '#ffffff',
strokeWeight: 4,
strokeOpacity: 1,
fillColor: '#ff0000',
fillOpacity: 0.5
});
var x,y,countmarkers;countmarkers=0;
for(x=52.8;x<=54;x=x+0.15){
for(y=12.6;y<=19;y=y+0.25) {
var point = { lat: x, lng: y};
if (google.maps.geometry.poly.containsLocation(point, pole1)) {
if (google.maps.geometry.poly.containsLocation(point, pole2)){
var marker = new google.maps.Marker({
position: new google.maps.LatLng(x, y),
map: map
});
countmarkers++;
}
}
}
}
//if you comment "problem" part of code & "map: map" in polygon declarations,
//this listener will work well, placing markers only in polygon "pole1"
google.maps.event.addListener(map, 'click', function(e) {
if (google.maps.geometry.poly.containsLocation(e.latLng, pole1)) {
var marker = new google.maps.Marker({
position: e.latLng,
map: map
});
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
答案 0 :(得分:1)
var x,y,countmarkers=0;
for(x=52.8;x<=54;x=x+0.15){
for(y=12.6;y<=19;y=y+0.25) {
var point = new google.maps.LatLng(x,y);
if (google.maps.geometry.poly.containsLocation(point, pole1)) {
if (google.maps.geometry.poly.containsLocation(point, pole2)){
var marker = new google.maps.Marker({
position: point,
map: map
});
countmarkers++;
}
}
}
}