在Android中查找两个GPS坐标之间的相似度可以通过计算它们之间的距离来实现。常用的计算距离的方法有欧氏距离、曼哈顿距离和哈夫曼距离。
Location location1 = new Location("");
location1.setLatitude(latitude1);
location1.setLongitude(longitude1);
Location location2 = new Location("");
location2.setLatitude(latitude2);
location2.setLongitude(longitude2);
float distance = location1.distanceTo(location2);
double distance = Math.abs(latitude1 - latitude2) + Math.abs(longitude1 - longitude2);
double earthRadius = 6371; // 地球半径,单位为千米
double latDistance = Math.toRadians(latitude2 - latitude1);
double lonDistance = Math.toRadians(longitude2 - longitude1);
double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
+ Math.cos(Math.toRadians(latitude1)) * Math.cos(Math.toRadians(latitude2))
* Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = earthRadius * c;
以上是三种常用的计算两个GPS坐标之间相似度的方法,根据具体需求选择适合的方法即可。
领取专属 10元无门槛券
手把手带您无忧上云