|
@@ -1,132 +1,132 @@
|
|
|
-package com.tripto.util;
|
|
|
-
|
|
|
-import java.net.URLEncoder;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-
|
|
|
-public class LocationUtils {
|
|
|
-
|
|
|
-
|
|
|
- //GeocodingURL KEY
|
|
|
- public static String KEY_1 = "CmWSW6MuBOCTzGATbfzlRYRxK0cBR1S7";
|
|
|
-
|
|
|
- //IpURL KEY
|
|
|
- public static String KEY_2 = "Ibsh0oEGVDe09iVcqzEMDuoEk8IX0Rvz";
|
|
|
-
|
|
|
- //GPS坐标转换百度坐标 KEY
|
|
|
- public static String KEY_3 = "VTqVA5weGzrbm8u8WmGCjOR2A9kz1R5G";
|
|
|
-
|
|
|
- //IpURL
|
|
|
- private static final String IpURL = "http://api.map.baidu.com/location/ip";
|
|
|
-
|
|
|
- //GeocodingURL
|
|
|
- private static final String GeocodingURL = "http://api.map.baidu.com/geocoder/v2/";
|
|
|
-
|
|
|
- //经纬度转地址
|
|
|
- private static final String geocodingReverseURL ="http://api.map.baidu.com/geocoder/v2/";
|
|
|
-
|
|
|
-
|
|
|
- //GPS坐标转换百度坐标URL
|
|
|
- private static final String gpsBaiduURL = "http://api.map.baidu.com/geoconv/v1/";
|
|
|
-
|
|
|
- /*static{
|
|
|
- KEY_1 = PropertyUtil.getValue("KEY_1", "location.properties");
|
|
|
- KEY_2 = PropertyUtil.getValue("KEY_2", "location.properties");
|
|
|
- KEY_3 = PropertyUtil.getValue("KEY_3", "location.properties");
|
|
|
- }*/
|
|
|
-
|
|
|
- /**
|
|
|
- * 通过地址获取经纬度
|
|
|
- * @param address
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static JSONObject getGeocoderLatitude(String address){
|
|
|
- try {
|
|
|
- address = URLEncoder.encode(address, "UTF-8");
|
|
|
- String url = GeocodingURL+"?address="+ address +"&output=json&ak="+ KEY_1;
|
|
|
- System.out.println(url);
|
|
|
- String ret = HttpUtil.submitGet(url);
|
|
|
- return JSONObject.parseObject(ret);
|
|
|
- } catch (Exception e) {
|
|
|
- // TODO: handle exception
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * IP地址转地址与经纬度
|
|
|
- * @param ip
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static JSONObject getLocationToIP(String ip){
|
|
|
- try {
|
|
|
- String url = IpURL+"?ip="+ ip +"&output=json&coor=bd09ll&ak="+ KEY_2;
|
|
|
- String ret = HttpUtil.submitGet(url);
|
|
|
- return JSONObject.parseObject(ret);
|
|
|
- } catch (Exception e) {
|
|
|
- // TODO: handle exception
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * GPS坐标转百度坐标
|
|
|
- * @param lng 经度
|
|
|
- * @param lat 维度
|
|
|
- * @return {"status":0,"result":[{"x":116.30305,"y":39.8972}]}
|
|
|
- */
|
|
|
- public static JSONObject getBaiduCoordinateToGPS(String lng,String lat){
|
|
|
- try {
|
|
|
- String url = gpsBaiduURL+"?coords="+ lng +",+"+lat+"&output=json&coor=bd09ll&ak="+ KEY_3;
|
|
|
- String ret = HttpUtil.submitGet(url);
|
|
|
- return JSONObject.parseObject(ret);
|
|
|
- } catch (Exception e) {
|
|
|
- // TODO: handle exception
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 通过IP转地址
|
|
|
- * @param lng
|
|
|
- * @param lat
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static JSONObject getAddressToLocation(String lng,String lat){
|
|
|
- try {
|
|
|
- String url = geocodingReverseURL+"?location="+ lng +",+"+lat+"&output=json&ak="+ KEY_1;
|
|
|
- String ret = HttpUtil.submitGet(url);
|
|
|
- return JSONObject.parseObject(ret);
|
|
|
- } catch (Exception e) {
|
|
|
- // TODO: handle exception
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- //116.471534,
|
|
|
- //System.out.println(getLocationToIP("114.242.2.96"));
|
|
|
- //System.out.println(getGeocoderLatitude("北京市大兴区黄村镇兴丰北大街西侧1幢4层423室"));
|
|
|
- System.out.println(getAddressToLocation("39.963175","116.471534"));
|
|
|
- /*String zfc = "{\"address\":\"CN|北京|北京|None|UNICOM|0|0\",\"content\":{\"address\":\"北京市\",\"address_detail\":{\"city\":\"北京市\",\"city_code\":131,\"district\":\"\",\"province\":\"北京市\",\"street\":\"\",\"street_number\":\"\"},\"point\":{\"x\":\"116.40387397\",\"y\":\"39.91488908\"}},\"status\":0}";
|
|
|
- JSONObject json = JSONObject.fromObject(zfc);
|
|
|
- System.out.println(json.getJSONObject("content").getJSONObject("point"));*/
|
|
|
-
|
|
|
- //http://127.0.0.1:8080/gt_location_service/Inlet?type=address&address=%E5%8C%97%E4%BA%AC%E5%B8%82%E6%B5%B7%E6%B7%80%E5%8C%BA%E6%97%B1%E6%B2%B3%E8%B7%AF%E8%BE%85%E8%B7%AF
|
|
|
- //http://127.0.0.1:8080/gt_location_service/Inlet?type=ip&ip=114.242.2.96
|
|
|
- //http://api.map.baidu.com/geoconv/v1/?coords=116.29055555555556,39.889722222222225&output=json&coor=bd09ll&ak=VTqVA5weGzrbm8u8WmGCjOR2A9kz1R5G
|
|
|
-
|
|
|
- //http://api.map.baidu.com/geocoder/v2/?location=39.983424,116.322987&output=json&ak=CmWSW6MuBOCTzGATbfzlRYRxK0cBR1S7
|
|
|
-
|
|
|
- //System.out.println(getAddressToLocation("36.5420920000", "107.6870310000"));
|
|
|
- //System.out.println(getBaiduCoordinateToGPS("116.29055555555556","39.889722222222225"));
|
|
|
- //System.out.println(getLocationToIP("182.92.235.119"));
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+package com.tripto.util;
|
|
|
+
|
|
|
+import java.net.URLEncoder;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+
|
|
|
+public class LocationUtils {
|
|
|
+
|
|
|
+
|
|
|
+ //GeocodingURL KEY
|
|
|
+ public static String KEY_1 = "CmWSW6MuBOCTzGATbfzlRYRxK0cBR1S7";
|
|
|
+
|
|
|
+ //IpURL KEY
|
|
|
+ public static String KEY_2 = "Ibsh0oEGVDe09iVcqzEMDuoEk8IX0Rvz";
|
|
|
+
|
|
|
+ //GPS坐标转换百度坐标 KEY
|
|
|
+ public static String KEY_3 = "VTqVA5weGzrbm8u8WmGCjOR2A9kz1R5G";
|
|
|
+
|
|
|
+ //IpURL
|
|
|
+ private static final String IpURL = "http://api.map.baidu.com/location/ip";
|
|
|
+
|
|
|
+ //GeocodingURL
|
|
|
+ private static final String GeocodingURL = "http://api.map.baidu.com/geocoder/v2/";
|
|
|
+
|
|
|
+ //经纬度转地址
|
|
|
+ private static final String geocodingReverseURL ="http://api.map.baidu.com/geocoder/v2/";
|
|
|
+
|
|
|
+
|
|
|
+ //GPS坐标转换百度坐标URL
|
|
|
+ private static final String gpsBaiduURL = "http://api.map.baidu.com/geoconv/v1/";
|
|
|
+
|
|
|
+ /*static{
|
|
|
+ KEY_1 = PropertyUtil.getValue("KEY_1", "location.properties");
|
|
|
+ KEY_2 = PropertyUtil.getValue("KEY_2", "location.properties");
|
|
|
+ KEY_3 = PropertyUtil.getValue("KEY_3", "location.properties");
|
|
|
+ }*/
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过地址获取经纬度
|
|
|
+ * @param address
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject getGeocoderLatitude(String address){
|
|
|
+ try {
|
|
|
+ address = URLEncoder.encode(address, "UTF-8");
|
|
|
+ String url = GeocodingURL+"?address="+ address +"&output=json&ak="+ KEY_1;
|
|
|
+ System.out.println(url);
|
|
|
+ String ret = HttpUtil.submitGet(url);
|
|
|
+ return JSONObject.parseObject(ret);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // TODO: handle exception
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * IP地址转地址与经纬度
|
|
|
+ * @param ip
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject getLocationToIP(String ip){
|
|
|
+ try {
|
|
|
+ String url = IpURL+"?ip="+ ip +"&output=json&coor=bd09ll&ak="+ KEY_2;
|
|
|
+ String ret = HttpUtil.submitGet(url);
|
|
|
+ return JSONObject.parseObject(ret);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // TODO: handle exception
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * GPS坐标转百度坐标
|
|
|
+ * @param lng 经度
|
|
|
+ * @param lat 维度
|
|
|
+ * @return {"status":0,"result":[{"x":116.30305,"y":39.8972}]}
|
|
|
+ */
|
|
|
+ public static JSONObject getBaiduCoordinateToGPS(String lng,String lat){
|
|
|
+ try {
|
|
|
+ String url = gpsBaiduURL+"?coords="+ lng +",+"+lat+"&output=json&coor=bd09ll&ak="+ KEY_3;
|
|
|
+ String ret = HttpUtil.submitGet(url);
|
|
|
+ return JSONObject.parseObject(ret);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // TODO: handle exception
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过IP转地址
|
|
|
+ * @param lng
|
|
|
+ * @param lat
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject getAddressToLocation(String lng,String lat){
|
|
|
+ try {
|
|
|
+ String url = geocodingReverseURL+"?location="+ lng +",+"+lat+"&output=json&ak="+ KEY_1;
|
|
|
+ String ret = HttpUtil.submitGet(url);
|
|
|
+ return JSONObject.parseObject(ret);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // TODO: handle exception
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ //116.471534,
|
|
|
+ //System.out.println(getLocationToIP("114.242.2.96"));
|
|
|
+ //System.out.println(getGeocoderLatitude("北京市大兴区黄村镇兴丰北大街西侧1幢4层423室"));
|
|
|
+ System.out.println(getAddressToLocation("39.963175","116.471534"));
|
|
|
+ /*String zfc = "{\"address\":\"CN|北京|北京|None|UNICOM|0|0\",\"content\":{\"address\":\"北京市\",\"address_detail\":{\"city\":\"北京市\",\"city_code\":131,\"district\":\"\",\"province\":\"北京市\",\"street\":\"\",\"street_number\":\"\"},\"point\":{\"x\":\"116.40387397\",\"y\":\"39.91488908\"}},\"status\":0}";
|
|
|
+ JSONObject json = JSONObject.fromObject(zfc);
|
|
|
+ System.out.println(json.getJSONObject("content").getJSONObject("point"));*/
|
|
|
+
|
|
|
+ //http://127.0.0.1:8080/gt_location_service/Inlet?type=address&address=%E5%8C%97%E4%BA%AC%E5%B8%82%E6%B5%B7%E6%B7%80%E5%8C%BA%E6%97%B1%E6%B2%B3%E8%B7%AF%E8%BE%85%E8%B7%AF
|
|
|
+ //http://127.0.0.1:8080/gt_location_service/Inlet?type=ip&ip=114.242.2.96
|
|
|
+ //http://api.map.baidu.com/geoconv/v1/?coords=116.29055555555556,39.889722222222225&output=json&coor=bd09ll&ak=VTqVA5weGzrbm8u8WmGCjOR2A9kz1R5G
|
|
|
+
|
|
|
+ //http://api.map.baidu.com/geocoder/v2/?location=39.983424,116.322987&output=json&ak=CmWSW6MuBOCTzGATbfzlRYRxK0cBR1S7
|
|
|
+
|
|
|
+ //System.out.println(getAddressToLocation("36.5420920000", "107.6870310000"));
|
|
|
+ //System.out.println(getBaiduCoordinateToGPS("116.29055555555556","39.889722222222225"));
|
|
|
+ //System.out.println(getLocationToIP("182.92.235.119"));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|