2013-12-22 52 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

当'C'被更改为true它提醒真实的,因为你说的'回报C' – Cilan

回答

0

您在 '错误' 的对象发送。您使用的资本XY代替xy为对象的属性:

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

JavaScript变量名是区分大小写。此代码应与小xy对象属性:

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

脑屁.....感谢安托! – ddpishere

+0

@ddpishere:如果这个答案适合你,你能否把它标记为已接受。谢谢。 –