快捷搜索: 王者荣耀 脱发

用百度地图api计算两个地方的距离

/**
 * @author 距离计算
 *
 */
public class DistanceUtil {
	private static final String showLocationUrl = "http://api.map.baidu.com/geocoder/v2/?output=json&ak=RguGdBfvanKG10lrLHtUAtka&address=";
	private static final String waypointsDistanceUrl = "http://api.map.baidu.com/telematics/v3/distance?output=json&ak=RguGdBfvanKG10lrLHtUAtka&waypoints=";
	
	@SuppressWarnings("unchecked")
	public static Double twoCitysDistance(String startCityName, String endCityName){
		Double distance = 0.00;
		
		Map<String, Object> startShowLocation = JsoupTool.getJson(showLocationUrl+startCityName, null);
		Map<String, Object> startResultLocation = (Map<String, Object>) startShowLocation.get("result");
		Map<String, Object> startLocation = (Map<String, Object>) startResultLocation.get("location");
		String startLng = startLocation.get("lng").toString();
		String startLat =  startLocation.get("lat").toString();
		
		Map<String, Object> showLocation = JsoupTool.getJson(showLocationUrl+endCityName, null);
		Map<String, Object> endResultLocation = (Map<String, Object>) showLocation.get("result");
		Map<String, Object> endLocation = (Map<String, Object>) endResultLocation.get("location");
		String endLng = endLocation.get("lng").toString();
		String endLat = endLocation.get("lat").toString();
		
		String showWaypointsDistanceUrl = waypointsDistanceUrl+startLng+","+startLat+";"+endLng+","+endLat;
		
		Map<String, Object> waypointsDistance = JsoupTool.getJson(showWaypointsDistanceUrl, null);
		String distanceStr = waypointsDistance.get("results").toString();
		distanceStr = distanceStr.replaceAll("\[", "").replaceAll("\]", "");
		String[] distanceArray = distanceStr.split(","); 
		distance = Double.valueOf(distanceArray[0]);
		return distance;
	}
	
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		String startCityName = "河南省郑州市市辖区";
		String endCityName = "河南省许昌市市辖区";
		System.out.println(showLocationUrl+startCityName);
		Map<String, Object> startShowLocation = JsoupTool.getJson(showLocationUrl+startCityName, null);
		Map<String, Object> startResultLocation = (Map<String, Object>) startShowLocation.get("result");
		Map<String, Object> startLocation = (Map<String, Object>) startResultLocation.get("location");
		String startLng = startLocation.get("lng").toString();
		String startLat =  startLocation.get("lat").toString();
		
		Map<String, Object> showLocation = JsoupTool.getJson(showLocationUrl+endCityName, null);
		Map<String, Object> endResultLocation = (Map<String, Object>) showLocation.get("result");
		Map<String, Object> endLocation = (Map<String, Object>) endResultLocation.get("location");
		String endLng = endLocation.get("lng").toString();
		String endLat = endLocation.get("lat").toString();
		String showWaypointsDistanceUrl = waypointsDistanceUrl+startLng+","+startLat+";"+endLng+","+endLat;
		Map<String, Object> waypointsDistance = JsoupTool.getJson(showWaypointsDistanceUrl, null);
		String distanceStr = waypointsDistance.get("results").toString();
		distanceStr = distanceStr.replaceAll("\[", "").replaceAll("\]", "");
		
		String[] distanceArray = distanceStr.split(","); 
		Double double1 = Double.valueOf(distanceArray[0]);
		
		System.out.println(new java.text.DecimalFormat("#").format(double1/1000/90));
	}

}

经验分享 程序员 微信小程序 职场和发展