如何获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)


看标题就会明白,两个经纬度之间真实距离这个通常的地图API有自带方法,直接调用即可获得结果,通常结果都是以米为单位。最近在作android版上的GoogleMap,找了半天API发现没有此类方法,看来只能本身实现了,接下来我就把如何计算两点之间(经纬度)的真实距离的算法写下来,原则上在各类地图版本上都通用,方便你们使用。java


Google Map API:https://developers.google.com/maps/documentation/android/android



【本文适用于android,ios等各类平台下的地图经纬度测距】ios


本身实现距离算法:git

/**
	 * 计算两点之间距离
	 * @param start
	 * @param end
	 * @return 米
	 */
	public double getDistance(LatLng start,LatLng end){
		double lat1 = (Math.PI/180)*start.latitude;
		double lat2 = (Math.PI/180)*end.latitude;
		
		double lon1 = (Math.PI/180)*start.longitude;
		double lon2 = (Math.PI/180)*end.longitude;
		
//      double Lat1r = (Math.PI/180)*(gp1.getLatitudeE6()/1E6);
//      double Lat2r = (Math.PI/180)*(gp2.getLatitudeE6()/1E6);
//      double Lon1r = (Math.PI/180)*(gp1.getLongitudeE6()/1E6);
//      double Lon2r = (Math.PI/180)*(gp2.getLongitudeE6()/1E6);
		
		//地球半径
		double R = 6371;
		
		//两点间距离 km,若是想要米的话,结果*1000就能够了
		double d =  Math.acos(Math.sin(lat1)*Math.sin(lat2)+Math.cos(lat1)*Math.cos(lat2)*Math.cos(lon2-lon1))*R;
		
		return d*1000;
	}


举例:(我使用的百度地图的经纬度数据)算法

LatLng start = new LatLng(39.95676, 116.401394);
		LatLng end = new LatLng(36.63014,114.499574);
		getDistance(start, end);

log日志结果为:402.21321(km)工具



惧怕不许确的话,能够打开百度地图首页,使用测距工具:google


看图应该知道,应该没什么问题吧。spa