2013-12-22 110 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:如果這個答案適合你,你能否把它標記爲已接受。謝謝。 –