import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; /** * @author xrj * @date 2020/6/8 */ public class GpsUtil { // public static void main(String[] args) { // System.out.println(reverseInt(376419585468736L)+""); // } public static long reverseInt(long i){ long s,j=0; s=i; while (s!=0){ j = j*10 + s%10; s = s/10; } return j; } /** * https://lbs.amap.com/console/show/picker?spm=a2c4g.11186623.2.25.1efd56f7y6ENFy * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Object[] o = getCoordinate("成都市天府四街"); //经度 System.out.println(o[0]); //纬度 System.out.println(o[1]); getCityFromLngAndlat("104.052288,30.546999"); } /** * @param addr * 查询的地址 * @return * @throws IOException */ public static Object[] getCoordinate(String addr) throws IOException { //经度 String lng = null; //纬度 String lat = null; String address = null; try { address = java.net.URLEncoder.encode(addr, "UTF-8"); }catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } String key = "f247cdb592eb43ebac6ccd27f796e2d2"; String url = String .format("http://api.map.baidu.com/geocoder?address=%s&output=json&key=%s", address, key); URL myURL = null; URLConnection httpsConn = null; try { myURL = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); } InputStreamReader insr = null; BufferedReader br = null; try { httpsConn = myURL.openConnection(); // 不使用代理 if (httpsConn != null) { insr = new InputStreamReader( httpsConn.getInputStream(), "UTF-8"); br = new BufferedReader(insr); String data = null; int count = 1; while((data= br.readLine())!=null){ if(count==5){ lng = (String)data.subSequence(data.indexOf(":")+1, data.indexOf(",")); //经度 count++; }else if(count==6){ lat = data.substring(data.indexOf(":")+1); //纬度 count++; }else{ count++; } } } } catch (IOException e) { e.printStackTrace(); } finally { if(insr!=null){ insr.close(); } if(br!=null){ br.close(); } } return new Object[]{lng,lat}; } /** * 22.75424,112.76535 * @param str * @throws IOException */ public static void getCityFromLngAndlat(String str) throws IOException { //通过修改这里的location(经纬度)参数,即可得到相应经纬度的详细信息 //f247cdb592eb43ebac6ccd27f796e2d2 //pmCgmADsAsD9rEXkqWNcTzjd String url2 = "http://api.map.baidu.com/geocoder/v2/?ak=pmCgmADsAsD9rEXkqWNcTzjd&location="+str+"&output=json&pois=1 "; URL myURL2 = null; URLConnection httpsConn2 = null; try { myURL2 = new URL(url2); } catch (MalformedURLException e) { e.printStackTrace(); } InputStreamReader insr2 = null; BufferedReader br2 = null; try { httpsConn2 = myURL2.openConnection(); // 不使用代理 if (httpsConn2 != null) { insr2 = new InputStreamReader( httpsConn2.getInputStream(), "UTF-8"); br2 = new BufferedReader(insr2); String data2 = br2.readLine(); System.out.println("data2:"+data2); try { //解析得到的json格式数据 JSONObject dataJson = new JSONObject(data2); JSONObject result = dataJson.getJSONObject("result"); JSONObject addressComponent = result.getJSONObject("addressComponent"); String city = addressComponent.getString("city"); System.out.println("city = " + city); } catch (JSONException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); } finally { if(insr2!=null){ insr2.close(); } } }
博主强烈推荐:https://blog.csdn.net/persistencegoing/article/details/84376427
希望大家关注我一波,防止以后迷路,有需要的可以加群讨论互相学习java ,学习路线探讨,经验分享与java求职
群号:721 515 304