Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ContainesLocation与loop&;公共字段_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript ContainesLocation与loop&;公共字段

Javascript ContainesLocation与loop&;公共字段,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我正在尝试使用containsLocation选项计算2个多边形的公共区域,以使大量相同的小区域仅放置在公共区域中。我认为ContainesLocation(这个,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) {

我正在尝试使用containsLocation选项计算2个多边形的公共区域,以使大量相同的小区域仅放置在公共区域中。我认为ContainesLocation(这个,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 x,y,countmarkers;countmarkers=0;
对于(x=52.8;x
var x,y,countmarkers=0;

对于(x=52.8;x
Point
应该是一个
google.maps.LatLng
实例不
{lat:x,lng:y}
谢谢你的回答。仍然不起作用。你试过
if(pole1.containsLatLng(Point)和&pole2.containsLatLng(Point)了吗{…
?Engerlost works给我的建议。哦,天哪。我忘了包括几何体库。无论如何谢谢你的帮助:)我编辑了代码。评论中正确的建议是使用
google.maps.LatLng
而不是
LatLngLiteral
(containsLocation
不会接受).这是唯一的问题。
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);
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++;
                }
            }
        }
    }