import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* 高德地图根据地名获取经纬度,计算距离
* 这个需要客户申请个高德API上面的key,也可以自己申请(免费的,很快)
* */
public class GaodMap {
//高德地图申请的key值
public static final String MAP_KEY = "xxx";
public static void main(String[] args){
String start = "北京天安门";
String end = "上海市黄浦区西藏中路";
String startLonLat = getLonLat(start);
String endLonLat = getLonLat(end);
System.out.println("起始地:"+start+",经纬度:"+startLonLat);
System.out.println("终点:"+end+",经纬度:"+endLonLat);
Long dis = getDistance(startLonLat,endLonLat);
System.out.println("两点间距离:"+dis+"米");
}
/**
* 0.得到两个地址间距离
* */
public static long getDistanceByAdress(String start,String end){
String startLonLat = getLonLat(start);
String endLonLat = getLonLat(end);
long dis = getDistance(startLonLat,endLonLat);
return dis;
}
/**
* 1.地址转换为经纬度
* */
public static String getLonLat(String address){
//返回输入地址address的经纬度信息, 格式是 经度,纬度
String queryUrl = "http://restapi.amap.com/v3/geocode/geo?key=&address="+address;
String queryResult = getResponse(queryUrl); //高德接品返回的是JSON格式的字符串
JSONObject job = JSONObject.parseObject(queryResult);
JSONObject jobJSON = JSONObject.parseObject(job.get("geocodes").toString().substring(1, job.get("geocodes").toString().length()-1));
String DZ = jobJSON.get("location").toString();
System.out.println("经纬度:"+DZ);
return DZ;
}
// 将经纬度getLng, getLat 通过getAmapByLngAndLat方法转换地址
public static String getAmapByLngAndLat(String getLng, String getLat) throws Exception {
String url;
try {
url = "http://restapi.amap.com/v3/geocode/regeo?output=JSON&location=" + getLng + "," + getLat
+ "&key=MAP_KEY&radius=0&extensions=base";
String queryResult = getResponse(url); // 高德接品返回的是JSON格式的字符串
if (queryResult == null) {
return "-1";
}
// 将获取结果转为json 数据
JSONObject obj = JSONObject.parseObject(queryResult);
if (obj.get("status").toString().equals("1")) {
// 如果没有返回-1
JSONObject regeocode = obj.getJSONObject("regeocode");
if (regeocode.size() > 0) {
// 在regeocode中拿到 formatted_address 具体位置
String formatted = regeocode.get("formatted_address").toString();
return formatted;
} else {
System.out.println("未找到相匹配的地址!");
return "-1";
}
} else {
System.out.println("请求错误!");
return "-1";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "-1";
}
/**
* 2.经纬度算出两点间距离
* */
public static long getDistance(String startLonLat, String endLonLat){
//返回起始地startAddr与目的地endAddr之间的距离,单位:米
Long result = new Long(0);
String queryUrl = "http://restapi.amap.com/v3/distance?key=MAP_KEY&origins="+startLonLat+"&destination="+endLonLat;
String queryResult = getResponse(queryUrl);
JSONObject job = JSONObject.parseObject(queryResult);
JSONArray ja = job.getJSONArray("results");
JSONObject jobO = JSONObject.parseObject(ja.getString(0));
result = Long.parseLong(jobO.get("distance").toString());
System.out.println("距离2:"+result);
return result;
}
/**
* 3.发送请求
* */
public static String getResponse(String serverUrl){
//用JAVA发起http请求,并返回json格式的结果
StringBuffer result = new StringBuffer();
try {
URL url = new URL(serverUrl);
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while((line = in.readLine()) != null){
result.append(line);
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
}
java高德地图根据坐标与具体地址互转,计算两地距离
最新推荐文章于 2025-03-30 10:00:00 发布