Google Maps v3:检查点是否存在于多边形中 [英] Google Maps v3: check if point exists in polygon

查看:135
本文介绍了Google Maps v3:检查点是否存在于多边形中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来检查Google Maps v3(JavaScript)中多边形内是否存在点。我到处搜索过,到目前为止我发现的唯一解决方案是获取多边形的边界,但所显示的代码似乎只是创建一个矩形并不断扩大其表面积以包含所有相关点。顺便说一下,我不能只使用一个大方块即得到一个多边形边界的原因是我在地图上有多边形边界,并且它们不能展开进入彼此的领土。

编辑
从下面的回复中,我尝试使用我现有的多边形之一来实现示例代码,但它只是说这是我没有定义,我不明白为什么。



这是我的声明:

  myCoordinates = [
new google.maps.LatLng(0.457301,-0.597382),
new google.maps.LatLng(0.475153,-0.569916 ),
新google.maps.LatLng(0.494379,-0.563049),
新google.maps.LatLng(0.506738,-0.553436),
新google.maps.LatLng(0.520470, - 0.541077),
新google.maps.LatLng(0.531456,-0.536957),
新google.maps.LatLng(0.556174,-0.552063),
新google.maps.LatLng(0.536949, -0.596008),
new google.maps.LatLng (0.503991,-0.612488),
new google.maps.LatLng(0.473780,-0.612488)];

polyOptions = {
path:myCoordinates,
strokeColor:#FF0000,
strokeOpacity:0.8,
strokeWeight:2,
fillColor:#0000FF,
fillOpacity:0.6};

var rightShoulderFront = new google.maps.Polygon(polyOptions);
rightShoulderFront.setMap(map);

这里是我检查点的地方:

  var coordinate = selectedmarker.getPosition(); 
var isWithinPolygon = rightShoulderFront.containsLatLng(coordinate);
console.log(isWithinPolygon);

但它不断出现错误:Uncaught ReferenceError:rightShoulderFront is not defined

解决方案

解决此问题的一种算法是ray-casting。请参阅此处的说明。



您可以找到代码执行这适用于Google Maps JS API V3 此处



HTH。


I am looking to find a way of checking if a point exists inside a polygon in Google Maps v3 (JavaScript). I've searched everywhere and the only solutions I have found so far have been to do with getting the bounds of the polygon, but the code shown seems to just create a rectangle and keeps expanding its surface area to include all relevant points.

By the way, the reason I can't just use a big square i.e. getting a polygons bounds, is that I have bordering polygons on the map and they can not expand into each other's territory.

EDIT Following on from the reply below, I have tried implementing the example code using one of my existing polygons but it is just saying that it is not defined and I can't figure out why.

Here is my declaration:

myCoordinates = [
    new google.maps.LatLng(0.457301,-0.597382),
    new google.maps.LatLng(0.475153,-0.569916),
    new google.maps.LatLng(0.494379,-0.563049),
    new google.maps.LatLng(0.506738,-0.553436),
    new google.maps.LatLng(0.520470,-0.541077),
    new google.maps.LatLng(0.531456,-0.536957),
    new google.maps.LatLng(0.556174,-0.552063),
    new google.maps.LatLng(0.536949,-0.596008),
    new google.maps.LatLng(0.503991,-0.612488),
    new google.maps.LatLng(0.473780,-0.612488) ];

polyOptions = { 
    path: myCoordinates,
    strokeColor: "#FF0000",
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: "#0000FF",
    fillOpacity: 0.6 };

var rightShoulderFront = new google.maps.Polygon(polyOptions);
rightShoulderFront.setMap(map);

and here is where I am checking for the point:

var coordinate = selectedmarker.getPosition();
var isWithinPolygon = rightShoulderFront.containsLatLng(coordinate);
console.log(isWithinPolygon);

But it keeps coming up with the error: Uncaught ReferenceError: rightShoulderFront is not defined

解决方案

One algorithm to solve this is ray-casting. See an explanation here.

And you can find code implementing this for the Google Maps JS API V3 here.

HTH.

这篇关于Google Maps v3:检查点是否存在于多边形中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆