2013-12-22 2 views
0

좋아요. 저는 시도하고 진실한 수식 중 하나를 Geofence에 사용하려고합니다. 코드에서 두 번째 경고는 사실의 결과 여야합니다 ... 그렇지만 항상 거짓이라고 말합니다. 어떤 아이디어? 여기 Javascript로 지오 펜싱

var points = [{ 
    x: 35.586680, 
    y: -80.874079 
}, { 
    x: 35.586646, 
    y: -80.872840 
}, { 
    x: 35.585852, 
    y: -80.872732 
}, { 
    x: 35.585673, 
    y: -80.873918 
}]; 


function isPointInPoly(poly, pt) { 
    for (var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y)) && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y)/(poly[j].y - poly[i].y) + poly[i].x) && (c = !c); 
    return c; 
} 

jQuery(window).ready(function() { 
    jQuery("#btnInit").click(initiate_geolocation); 
    jQuery("#checkit").click(e); 
}); 

function initiate_geolocation() { 
    navigator.geolocation.getCurrentPosition(handle_geolocation_query); 
} 

function handle_geolocation_query(position) { 
    alert('Lat: ' + position.coords.latitude + ' ' + 
     'Lon: ' + position.coords.longitude); 
    alert(isPointInPoly(points, { 
     X: 35.586488, 
     Y: -80.873660 
    })); 
    alert(isPointInPoly(points, { 
     X: position.coords.latitude, 
     y: position.coords.longitude 
    })); 
} 

당신은 '잘못된'개체에서 전송 JSFiddle 링크

http://jsfiddle.net/D2RL3/

+0

그것은 진정한 경고는'반환 – Cilan

답변

0

입니다. 당신은 개체 속성에 대한 자본 XY 대신 xy을 사용 :

alert(isPointInPoly(points, { 
     X: 35.586488, 
     Y: -80.873660 
    })); 
    alert(isPointInPoly(points, { 
     X: position.coords.latitude, 
     y: position.coords.longitude 
    })); 

변수 이름은 대소 문자를 구분 자바 스크립트. 이 코드는 x 작은 y 객체 속성이어야한다 : c` true로 변경 될 때 '

alert(isPointInPoly(points, { 
     x: 35.586488, 
     y: -80.873660 
    })); 
    alert(isPointInPoly(points, { 
     x: position.coords.latitude, 
     y: position.coords.longitude 
    })); 
+1

뇌 방귀 c' ..... 감사 ANTO 말 때문에! – ddpishere

+0

@ddpishere :이 답변을 사용할 수있는 경우 허용으로 표시해주세요. 감사. –

관련 문제