java-經緯度計算器 原

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang.StringUtils; public class LatitudeUtil { public static final String KEY_1 = "ChwEVlrmoje34iED20piImPc"; /** * 根據地址查座標 * * @param address * 地址,格式:深圳市羅湖區火車站 * * @return */ // @param key 申請ak(即獲取密鑰) ,若無百度賬號則首先需要註冊百度賬號。 public static Map<String, String> getGeocoderLatitude(String address) { BufferedReader in = null; // if(CommonUtil.NotEmpty(key)){ // return null; // } try { address = URLEncoder.encode(address, "UTF-8"); URL tirc = new URL("http://api.map.baidu.com/geocoder?address=" + address + "&output=json&key=" + KEY_1); in = new BufferedReader(new InputStreamReader(tirc.openStream(), "UTF-8")); String res; StringBuilder sb = new StringBuilder(""); while ((res = in.readLine()) != null) { sb.append(res.trim()); } String str = sb.toString(); Map<String, String> map = new HashMap<String, String>(); if (StringUtils.isNotEmpty(str)) { int lngStart = str.indexOf("lng\":"); int lngEnd = str.indexOf(",\"lat"); int latEnd = str.indexOf("},\"precise"); if (lngStart > 0 && lngEnd > 0 && latEnd > 0) { String lng = str.substring(lngStart + 5, lngEnd); String lat = str.substring(lngEnd + 7, latEnd); map.put("lng", lng); map.put("lat", lat); return map; } } } catch (Exception e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } /** * 計算地球上任意兩點(經緯度)距離 * * @param lon1 * 第一點經度 * @param lat1 * 第一點緯度 * @param lon2 * 第二點經度 * @param lat2 * 第二點緯度 * @return 返回距離單位:千米 */ public static double getDistatce(double lon1, double lat1, double lon2, double lat2) { double R = 6371; double distance = 0.0; double dLat = (lat2 - lat1) * Math.PI / 180; double dLon = (lon2 - lon1) * Math.PI / 180; double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon / 2) * Math.sin(dLon / 2); distance = (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))) * R; return distance; } /** * 計算地球上任意兩點(經緯度)距離 * * @param long1 * 第一點經度 * @param lat1 * 第一點緯度 * @param long2 * 第二點經度 * @param lat2 * 第二點緯度 * @return 返回距離單位:米 */ public static double Distance(double long1, double lat1, double long2, double lat2) { double a, b, R; R = 6378137; // 地球半徑 lat1 = lat1 * Math.PI / 180.0; lat2 = lat2 * Math.PI / 180.0; a = lat1 - lat2; b = (long1 - long2) * Math.PI / 180.0; double d; double sa2, sb2; sa2 = Math.sin(a / 2.0); sb2 = Math.sin(b / 2.0); d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2)); return d; } /** * 查找一定範圍內的經緯度值 傳入值: 經度緯度查找半徑(m) 返回值:最小經度、緯度,最大經度、緯度 113.957541,22.549392 * 朗峯大廈 */ public static Map<String, Double> getAround(Double lon, Double lat, Double raidus) { Double PI = 3.14159265; // 圓周率 Double EARTH_RADIUS = 6378137d; // 地球半徑 Double RAD = Math.PI / 180.0; // 弧度 Double longitude = lon; // 經度 Double latitude = lat; // 緯度 Double degree = (24901 * 1609) / 360.0; Double raidusMile = raidus; // 距離 Double dpmLat = 1 / degree; Double radiusLat = dpmLat * raidusMile; Double minLat = latitude - radiusLat; // 最小緯度 Double maxLat = latitude + radiusLat; // 最大緯度 Double mpdLng = degree * Math.cos(latitude * (PI / 180)); Double dpmLng = 1 / mpdLng; Double radiusLng = dpmLng * raidusMile; Double minLng = longitude - radiusLng; // 最小經度 Double maxLng = longitude + radiusLng; // 最大經度 Map<String, Double> m = new HashMap<String, Double>(); m.put("minLng", minLng); // 最小經度 m.put("minLat", minLat); // 最小緯度 m.put("maxLng", maxLng); // 最大經度 m.put("maxLat", maxLat); // 最大緯度 System.err.println(" 最小經度:" + minLng); System.err.println(" 最小緯度:" + minLat); System.err.println(" 最大經度:" + maxLng); System.err.println(" 最大緯度:" + maxLat); return m; } public static void main(String args[]) { /*Map<String, String> json = LatitudeUtil.getGeocoderLatitude(" 深圳羅湖火車站"); // Map<String, String> json = LatitudeUtil.getGeocoderLatitude("tl"); System.out.println("lng : " + json.get("lng")); System.out.println("lat : " + json.get("lat"));*/ double d1 = getDistatce(117.12744,36.68027, 117.0154019924088, 36.590417602779226); double d2 = Distance(117.12744,36.68027, 117.0154019924088, 36.590417602779226); System.out.println("d1 -> " + d1 + ", d2 -> " + d2); /*double f = 111.234; BigDecimal bg = new BigDecimal(f); double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(f1);*/ getAround(36.68027, 117.12744, 10000D); } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章