2013-01-10 2 views
3

latLng이 다각형 Maps.area을 통과하면 해결하려고하는이 코드가 여기에 있습니다.점이 다각형 안에 있는지 알아보기

Maps.ui.contains = function(latLng){ 
    //poly.getBounds gets the 'box' around the polygon 
    if(!Maps.ui.getBounds().contains(latLng)) 
     return false; 
    //So we dont need to check t/f, we either set it or we dont 
    var inPolygon = false; 
    var count = 0; 

    Maps.area.getPaths().forEach(function(el0, index0){ 
     var last = el0.getLength() - 1; 
     el0.forEach(function(el1, index1){ 
      count += Maps.ui.ray_intersect_segment(latLng, el1, el0.getAt(last)); 
      last = index1; 
     }); 
    }); 

    if(Maps.area.getPaths().getLength()%2 == 0) 
     return count%2==0; 
    else 
     return count%2!=0; 


} 

var eps = 0.0001; 
var inf = 1e600; 
Maps.ui.ray_intersect_segment = function(point, i1, i2){ 
    var p = point; 
    var segment = (i1.lng() > i2.lng())?[i2, i1]:[i1, i2]; 

    p = (p.lng() == segment[0].lng() || p.lng() == segment[1].lng())?new google.maps.LatLng(p.lng() + eps):p; 

    if(p.lng() < segment[0].lng() || p.lng() > segment[1].lng() || p.lat() > [segment[0].lat(), segment[1].lng()].max()) 
     return 0; 
    if(p.lat() < [segment[0].lat(), segment[1].lat()].min()) 
     return 1; 

    var a = (segment[0].lat() != segment[1].lat())?(segment[1].lng() - segment[0].lng())/(segment[1].lat() - segment[0].lat()):inf; 
    var b = (segment[0].lat() != p.lat()) ? (p.lng() - segment[0].lng())/(p.lat() - segment[0].lat()):inf; 

    return (b > a)?1:0; 
} 

Maps.ui.getBounds = function() { 
    //Lets make a box 
    var bounds = new google.maps.LatLngBounds(); 
    //Get all the points lines of the polly 
    var paths = Maps.area.getPaths(); 
    for (var p = 0; p < paths.getLength(); p++) 
     //To store each path 
     var path = paths.getAt(p); 
     //Now lets expand the box 
     for (var i = 0; i < path.getLength(); i++) 
      //Add each point of the line to the 'box' making it bigger each time 
      bounds.extend(path.getAt(i)); 
    //Reaturn the bounds, this has a contains method so we can check if the latLng is in it. 
    return bounds; 
} 

Array.prototype.max = function() { 
    return Math.max.apply(null, this) 
} 

Array.prototype.min = function() { 
    return Math.min.apply(null, this) 
} 

그러나 나는 그것을 해결할 수 없습니다. 간단한 삼각형하거나 완벽하게 작동 광장,하지만 우리는 이런 식으로 뭔가에 도착하면 우리는 count도해야합니다 여부를 알아낼 수 없습니다 또는 홀수

enter image description here

+0

폴리곤은 항상 볼록합니까? – cppguy

답변

2

Google지도 API v3의 spherical geometry 라이브러리는 poly.contains있다. LatLng 및 다각형을 가져 와서 점이 다각형에 있는지 여부를 알려줍니다.

containsLocation(point:LatLng, polygon:Polygon) 
0

이가 꽤 있기 때문에 작동하지 않습니다 지리 정보 시스템에 대한 표준 문제 문제를 해결하기위한 몇 가지 "표준"알고리즘이 있습니다. 아래의 링크는 그 중 몇 가지를 설명하고 예제를 제공합니다. 폴리곤이 폴과 자오선과 같은 극한의 위도/경도 경계에 이르는 경우와 같이 에지의 경우 알고리즘이 파괴되는 경향이 있습니다.

Polygon Algorithms

관련 문제