scott 6 years ago
parent
commit
ff0dc46454

+ 84 - 83
hotels_location/src/main/java/com/tripto/service/LocationService.java

@@ -1,83 +1,84 @@
-package com.tripto.service;
-
-import com.alibaba.fastjson.JSONObject;
-import com.tripto.Interface.LocationInterface;
-import com.tripto.util.LocationUtils;
-
-
-public class LocationService implements LocationInterface {
-
-	public JSONObject getLocationToAddress(String address) {
-		JSONObject json = LocationUtils.getGeocoderLatitude(address);
-		JSONObject retJson = new JSONObject();
-		if(json != null && json.containsKey("status") && "OK".equals(json.getString("status"))){
-			//成功
-			retJson.put("status", true);
-			retJson.put("lng", json.getJSONObject("result").getJSONObject("location").getString("lng"));
-			retJson.put("lat", json.getJSONObject("result").getJSONObject("location").getString("lat"));
-		}else{
-			//失败
-			retJson.put("status", false);
-			retJson.put("msg", "转换失败");
-		}
-		return retJson;
-	}
-
-	public JSONObject getLocationToIP(String ip) {
-		JSONObject json = LocationUtils.getLocationToIP(ip);
-		JSONObject retJson = new JSONObject();
-		if(json != null && json.containsKey("status") && "0".equals(json.getString("status"))){
-			//成功
-			retJson.put("status", true);
-			retJson.put("address", json.getJSONObject("content").getString("address"));
-			
-			retJson.put("province", json.getJSONObject("content").getJSONObject("address_detail").getString("province"));
-			retJson.put("city", json.getJSONObject("content").getJSONObject("address_detail").getString("city"));
-			retJson.put("district", json.getJSONObject("content").getJSONObject("address_detail").getString("district"));
-			
-			retJson.put("lng", json.getJSONObject("content").getJSONObject("point").getString("x"));
-			retJson.put("lat", json.getJSONObject("content").getJSONObject("point").getString("y"));
-		}else{
-			//失败
-			retJson.put("status", false);
-			retJson.put("msg", "转换失败");
-		}
-		return retJson;
-	}
-
-	public JSONObject getAddressToLocation(String lng, String lat) {
-		JSONObject json = LocationUtils.getAddressToLocation(lng, lat);
-		JSONObject retJson = new JSONObject();
-		if(json != null && json.containsKey("status") && "0".equals(json.getString("status"))){
-			//成功
-			retJson.put("status", true);
-			retJson.put("address", json.getJSONObject("result").getString("formatted_address"));
-			retJson.put("province", json.getJSONObject("result").getJSONObject("addressComponent").getString("province"));
-			retJson.put("city", json.getJSONObject("result").getJSONObject("addressComponent").getString("city"));
-			retJson.put("district", json.getJSONObject("result").getJSONObject("addressComponent").getString("district"));
-		}else{
-			//失败
-			retJson.put("status", false);
-			retJson.put("msg", "转换失败");
-		}
-		return retJson;
-	}
-
-	public JSONObject getBaiduCoordinateToGPS(String lng, String lat) {
-		JSONObject json = LocationUtils.getBaiduCoordinateToGPS(lng, lat);
-		JSONObject retJson = new JSONObject();
-		if(json != null && json.containsKey("status") && "0".equals(json.getString("status"))){
-			//成功
-			retJson.put("status", true);
-			JSONObject result = json.getJSONArray("result").getJSONObject(0);
-			retJson.put("lng", result.getString("x"));
-			retJson.put("lat", result.getString("y"));
-		}else{
-			//失败
-			retJson.put("status", false);
-			retJson.put("msg", "转换失败");
-		}
-		return retJson;
-	}
-
-}
+package com.tripto.service;
+
+import com.alibaba.fastjson.JSONObject;
+import com.tripto.Interface.LocationInterface;
+import com.tripto.util.LocationUtils;
+
+
+public class LocationService implements LocationInterface {
+
+	public JSONObject getLocationToAddress(String address) {
+		JSONObject json = LocationUtils.getGeocoderLatitude(address);
+		JSONObject retJson = new JSONObject();
+		if(json != null && json.containsKey("status") && "OK".equals(json.getString("status"))){
+			//成功
+			retJson.put("status", true);
+			retJson.put("lng", json.getJSONObject("result").getJSONObject("location").getString("lng"));
+			retJson.put("lat", json.getJSONObject("result").getJSONObject("location").getString("lat"));
+		}else{
+			//失败
+			retJson.put("status", false);
+			retJson.put("msg", "转换失败");
+		}
+		return retJson;
+	}
+
+	public JSONObject getLocationToIP(String ip) {
+		JSONObject json = LocationUtils.getLocationToIP(ip);
+		JSONObject retJson = new JSONObject();
+		if(json != null && json.containsKey("status") && "0".equals(json.getString("status"))){
+			//成功
+			retJson.put("status", true);
+			retJson.put("address", json.getJSONObject("content").getString("address"));
+			
+			retJson.put("province", json.getJSONObject("content").getJSONObject("address_detail").getString("province"));
+			retJson.put("city", json.getJSONObject("content").getJSONObject("address_detail").getString("city"));
+			retJson.put("district", json.getJSONObject("content").getJSONObject("address_detail").getString("district"));
+			
+			retJson.put("lng", json.getJSONObject("content").getJSONObject("point").getString("x"));
+			retJson.put("lat", json.getJSONObject("content").getJSONObject("point").getString("y"));
+		}else{
+			//失败
+			retJson.put("status", false);
+			retJson.put("msg", "转换失败");
+		}
+		return retJson;
+	}
+
+	public JSONObject getAddressToLocation(String lng, String lat) {
+		JSONObject json = LocationUtils.getAddressToLocation(lng, lat);
+		JSONObject retJson = new JSONObject();
+		if(json != null && json.containsKey("status") && "0".equals(json.getString("status"))){
+			//成功
+			retJson.put("status", true);
+			retJson.put("address", json.getJSONObject("result").getString("formatted_address"));
+			retJson.put("adcode", json.getJSONObject("result").getJSONObject("addressComponent").getString("adcode"));
+			retJson.put("province", json.getJSONObject("result").getJSONObject("addressComponent").getString("province"));
+			retJson.put("city", json.getJSONObject("result").getJSONObject("addressComponent").getString("city"));
+			retJson.put("district", json.getJSONObject("result").getJSONObject("addressComponent").getString("district"));
+		}else{
+			//失败
+			retJson.put("status", false);
+			retJson.put("msg", "转换失败");
+		}
+		return retJson;
+	}
+
+	public JSONObject getBaiduCoordinateToGPS(String lng, String lat) {
+		JSONObject json = LocationUtils.getBaiduCoordinateToGPS(lng, lat);
+		JSONObject retJson = new JSONObject();
+		if(json != null && json.containsKey("status") && "0".equals(json.getString("status"))){
+			//成功
+			retJson.put("status", true);
+			JSONObject result = json.getJSONArray("result").getJSONObject(0);
+			retJson.put("lng", result.getString("x"));
+			retJson.put("lat", result.getString("y"));
+		}else{
+			//失败
+			retJson.put("status", false);
+			retJson.put("msg", "转换失败");
+		}
+		return retJson;
+	}
+
+}

+ 132 - 132
hotels_location/src/main/java/com/tripto/util/LocationUtils.java

@@ -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"));
+    	
+	}
+    
+}