假设我有一个GPS坐标说A(在经度 - 纬度十进制形式,然后我想创建一个30米半径的区域(一个圆圈),这样我就可以捕获进入该区域的任何设备(带GPS坐标),怎么做?谢谢
答案 0 :(得分:2)
看看这个javascript示例,但它很容易在任何其他语言上实现。
主要代码:
var earthRadiusKm = 3437.74677 * 1.1508 * 1.6093470878864446;
function GeoArea(centerLat, centerLng, distanceKm, northPoint, southPoint, eastPoint, westPoint) {
this.northPoint = northPoint;
this.southPoint = southPoint;
this.eastPoint = eastPoint;
this.westPoint = westPoint;
this.inArea = function (lat, lng) {
var inBox = southPoint.lat <= lat && lat <= northPoint.lat && westPoint.lng <= lng && lng <= eastPoint.lng;
if (inBox) {
var distanceFromCenterKm = calcDistance(centerLat, centerLng, lat, lng);
return distanceFromCenterKm <= distanceKm;
} else {
return false;
}
}
}
function GeoPoint(lat, lng) {
this.lat = lat;
this.lng = lng;
}
function toDegrees(radians) {
return radians / (Math.PI / 180);
}
function toRadians(degrees) {
return Math.PI / 180 * degrees;
}
function calcDistance(latA, lngA, latB, lngB) {
var rLatA = toRadians(latA);
var rLatB = toRadians(latB);
var rHalfDeltaLat = toRadians((latB - latA) / 2);
var rHalfDeltaLng = toRadians((lngB - lngA) / 2);
return 2 * earthRadiusKm * Math.asin(Math.sqrt(Math.pow(Math.sin(rHalfDeltaLat), 2) + Math.cos(rLatA) * Math.cos(rLatB) * Math.pow(Math.sin(rHalfDeltaLng), 2)));
}
function findPoint(lat, lng, bearing, distance) {
var rLat = toRadians(lat);
var rLng = toRadians(lng);
var rBearing = toRadians(bearing);
var rAngDist = distance / earthRadiusKm;
var rLatB = Math.asin(Math.sin(rLat) * Math.cos(rAngDist) + Math.cos(rLat) * Math.sin(rAngDist) * Math.cos(rBearing));
var rLngB = rLng + Math.atan2(Math.sin(rBearing) * Math.sin(rAngDist) * Math.cos(rLat), Math.cos(rAngDist) - Math.sin(rLat) * Math.sin(rLatB));
var pLat = toDegrees(rLatB);
var pLng = toDegrees(rLngB);
return new GeoPoint(pLat, pLng);
}
function calcArea(lat, lng, distanceKm) {
var northPoint = findPoint(lat, lng, 0, distanceKm);
var eastPoint = findPoint(lat, lng, 90, distanceKm);
var southPoint = findPoint(lat, lng, 180, distanceKm);
var westPoint = findPoint(lat, lng, 270, distanceKm);
return new GeoArea(lat, lng, distanceKm, northPoint, southPoint, eastPoint, westPoint);
}
用法示例:
// calculate area with center in lat:55.742793 lng:37.615401
// and distance in 23 km
var area = calcArea(55.742793, 37.615401, 23);
//returns true
area.inArea(55.714735, 37.629547);
//returns false
area.inArea(55.693842, 38.015442);