pom/xml
<!-- Java计算两个经纬度间的距离最简单的方式 --> <!-- <dependency> <groupId>org.gavaghan</groupId> <artifactId>geodesy</artifactId> <version>1.1.3</version> </dependency> -->
(微信小程序map组件 传的经纬度可以直接用)
计算2个 坐标之间的距离
计算2个经纬度之间的距离
public static void main(String s[]) throws Exception{ GlobalCoordinates source = new GlobalCoordinates(34.17168950710855, 112.84804344177246); GlobalCoordinates target = new GlobalCoordinates(34.168635904722734, 112.86040306091309); double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere); double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84); System.out.println("Sphere坐标系计算结果:"+meter1 + "米"); System.out.println("WGS84坐标系计算结果:"+meter2 + "米"); } public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid) { //创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离 GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo); return geoCurve.getEllipsoidalDistance(); }
package com.java456.util; import java.util.Map; import org.gavaghan.geodesy.Ellipsoid; import org.gavaghan.geodesy.GeodeticCalculator; import org.gavaghan.geodesy.GeodeticCurve; import org.gavaghan.geodesy.GlobalCoordinates; public class PositionUtil { public static void main(String s[]) throws Exception { // 微信小程序的经纬度可以直接传来用 GlobalCoordinates source = new GlobalCoordinates(34.17168950710855, 112.84804344177246); GlobalCoordinates target = new GlobalCoordinates(34.168635904722734, 112.86040306091309); double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere); double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84); System.out.println("Sphere坐标系计算结果:" + meter1 + "米"); System.out.println("WGS84坐标系计算结果:" + meter2 + "米"); } /** * @param p1_lat * @param p1_lon * @param p2_lat * @param p2_lon * @return 微信小程序的经纬度可以直接传来用 */ public static double getJulv(Double p1_lat,Double p1_lon,Double p2_lat,Double p2_lon) { GlobalCoordinates from = new GlobalCoordinates(p1_lat, p1_lon); GlobalCoordinates to = new GlobalCoordinates(p2_lat, p2_lon); double meter1 = getDistanceMeter(from, to, Ellipsoid.Sphere); return meter1; } public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid) { // 创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离 GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo); return geoCurve.getEllipsoidalDistance(); } }
站长微信:xiaomao0055
站长QQ:14496453