用百度地图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 startShowLocation = JsoupTool.getJson(showLocationUrl+startCityName, null);
		Map startResultLocation = (Map) startShowLocation.get("result");
		Map startLocation = (Map) startResultLocation.get("location");
		String startLng = startLocation.get("lng").toString();
		String startLat =  startLocation.get("lat").toString();
		
		Map showLocation = JsoupTool.getJson(showLocationUrl+endCityName, null);
		Map endResultLocation = (Map) showLocation.get("result");
		Map endLocation = (Map) endResultLocation.get("location");
		String endLng = endLocation.get("lng").toString();
		String endLat = endLocation.get("lat").toString();
		
		String showWaypointsDistanceUrl = waypointsDistanceUrl+startLng+","+startLat+";"+endLng+","+endLat;
		
		Map 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 startShowLocation = JsoupTool.getJson(showLocationUrl+startCityName, null);
		Map startResultLocation = (Map) startShowLocation.get("result");
		Map startLocation = (Map) startResultLocation.get("location");
		String startLng = startLocation.get("lng").toString();
		String startLat =  startLocation.get("lat").toString();
		
		Map showLocation = JsoupTool.getJson(showLocationUrl+endCityName, null);
		Map endResultLocation = (Map) showLocation.get("result");
		Map endLocation = (Map) endResultLocation.get("location");
		String endLng = endLocation.get("lng").toString();
		String endLat = endLocation.get("lat").toString();
		String showWaypointsDistanceUrl = waypointsDistanceUrl+startLng+","+startLat+";"+endLng+","+endLat;
		Map 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));
	}

}

你可能感兴趣的:(用百度地图api计算两个地方的距离)