麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

獲取某地的經(jīng)緯度 && 通過經(jīng)緯度獲取相應(yīng)的地理位置

2019-11-15 00:55:29
字體:
供稿:網(wǎng)友
獲取某地的經(jīng)緯度 && 通過經(jīng)緯度獲取相應(yīng)的地理位置

  最近要通過一個經(jīng)緯度判斷該經(jīng)緯度是否位于某個地區(qū)內(nèi),所以通過網(wǎng)上查找資料,整合后出了下面的內(nèi)容。

1、通過地址獲取改地址的經(jīng)緯度

 /**     * @param addr     * 查詢的地址     * @return     * @throws IOException     */    public Object[] getCoordinate(String addr) throws IOException {         String lng = null;//經(jīng)度        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 = (URLConnection) 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(","));//經(jīng)度                        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};     } 

測試

 public static void main(String[] args) throws IOException {        GetLocation getLatAndLngByBaidu = new GetLocation();        Object[] o = getLatAndLngByBaidu.getCoordinate("成都市天府四街");                System.out.println(o[0]);//經(jīng)度        System.out.println(o[1]);//緯度    }

結(jié)果:

104.06383230.54855

2、通過某個經(jīng)緯度獲取該經(jīng)緯度的地址,返回的是一個json格式的對象(需要引入json.org.jar包的JSONObject類進行解析),這里只實現(xiàn)了通過經(jīng)緯度查詢出該經(jīng)緯度所在的市,其他信息再json中都有,只需通過解析即可得出,這里不給出其他信息的獲取方式。

public static void getCityFromLngAndlat(){//通過修改這里的location(經(jīng)緯度)參數(shù),即可得到相應(yīng)經(jīng)緯度的詳細(xì)信息String url2 = "http://api.map.baidu.com/geocoder/v2/?ak=pmCgmADsAsD9rEXkqWNcTzjd&location=22.75424,112.76535&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 = (URLConnection) myURL2.openConnection();// 不使用代理             if (httpsConn2 != null) {                 insr2 = new InputStreamReader( httpsConn2.getInputStream(), "UTF-8");                 br2 = new BufferedReader(insr2);                 String data2 = br2.readLine();                 try                {                                     //解析得到的json格式數(shù)據(jù)                    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();            }            if(br!=null){                br2.close();            }        }}

獲取到的json原始數(shù)據(jù)如下

{"status":0,"result":{"location":{"lng":112.76535000506,"lat":22.754240005123},"formatted_address":"廣東省佛山市高明區(qū)X492","business":"","addressComponent":{"city":"佛山市","country":"中國","direction":"","distance":"","district":"高明區(qū)","province":"廣東省","street":"X492","street_number":"","country_code":0},"pois":[{"addr":"佛山市高明區(qū)","cp":"NavInfo","direction":"東北","distance":"680","name":"皂幕山泉","poiType":"公司企業(yè)","point":{"x":112.76102978437,"y":22.750226120112},"tag":"公司企業(yè)","tel":"","uid":"ea4a3dc9e82219534e5547fc","z

  


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久成人精品视频 | 欧美极品欧美精品欧美视频 | 色播久久| 国产人成免费爽爽爽视频 | 国产亚洲精品久久午夜玫瑰园 | 欧洲精品久久 | 国产一区二区三区四区五区在线 | 在线成人一区 | 中文字幕激情视频 | 一本色道久久综合亚洲精品图片 | 黄网站免费入口 | 黄色aaa视频 | 国产毛片自拍 | 国产精品视频六区 | 国产精品99久久久久久久 | 久久网一区二区 | 中国洗澡偷拍在线播放 | 午夜国产在线 | 国产毛片网站 | 毛片在线免费视频 | 91成人亚洲 | 美女福利视频国产 | 成人午夜精品久久久久久久3d | 国产精品免费久久久久 | 久久免费视频一区二区三区 | 精品亚洲二区 | 色播视频在线播放 | 免费a观看| 九九视频精品在线观看 | 高清做爰免费无遮网站挡 | 日韩视频一区二区三区在线观看 | 成人在线观看一区二区 | 国产一区二区三区四区五区加勒比 | 一区二区久久精品66国产精品 | 最新国产毛片 | 亚洲片在线观看 | 久久人人爽人人爽人人片av高清 | 国产精品99一区二区 | 青草久久久久 | 激情小视频在线观看 | av在线视 |