如何使用Java测试具有纬度/经度和半径的区域内的点? [英] How to test if a point inside area with lat/lon and radius using Java?

查看:33
本文介绍了如何使用Java测试具有纬度/经度和半径的区域内的点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个具有以下签名的方法

I've to write a method that has the following signature

public class Position {
double longitude;
double latitude;
}

boolean isInsideTheArea(Position center, double radius, Position point);

所以如果 point 位于以 center 为中心,radius 为半径的 area 内以英里为单位,这应该返回true,否则返回false.

So if point is inside the area that has the center as its center and radius as its radius in miles, this should return true, false otherwise.

推荐答案

使用 Haversine 公式来计算之间的距离centerpoint.如果距离大于radius,返回false;否则返回 true.

Use the Haversine formula to compute the distance between center and point. If that distance is greater than radius, return false; otherwise return true.

伪代码:

def haversineDistance(a, b):
    # snip...
    return foo

def isInsideTheArea (center, radius, point):
    return haversineDistance(center, point) <= radius

这篇关于如何使用Java测试具有纬度/经度和半径的区域内的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆