1. 程式人生 > >java高德地圖根據座標與具體地址互轉,計算兩地距離

java高德地圖根據座標與具體地址互轉,計算兩地距離

package com.shineyoo.common.utils;

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();
      }

}