国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

獲取某地的經緯度 && 通過經緯度獲取相應的地理位置

2019-11-15 00:55:29
字體:
來源:轉載
供稿:網友
獲取某地的經緯度 && 通過經緯度獲取相應的地理位置

  最近要通過一個經緯度判斷該經緯度是否位于某個地區內,所以通過網上查找資料,整合后出了下面的內容。

1、通過地址獲取改地址的經緯度

 /**     * @param addr     * 查詢的地址     * @return     * @throws IOException     */    public 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 = (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(","));//經度                        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]);//經度        System.out.println(o[1]);//緯度    }

結果:

104.06383230.54855

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

public static void getCityFromLngAndlat(){//通過修改這里的location(經緯度)參數,即可得到相應經緯度的詳細信息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格式數據                    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原始數據如下

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

  


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 驻马店市| 得荣县| 丰台区| 临洮县| 牙克石市| 松滋市| 南部县| 北票市| 南丰县| 灯塔市| 贺州市| 大渡口区| 虞城县| 宣汉县| 正安县| 壤塘县| 南溪县| 仙游县| 浮梁县| 南京市| 仙居县| 长岭县| 阿瓦提县| 黄大仙区| 荔浦县| 乾安县| 宁津县| 青河县| 上饶县| 青龙| 桦甸市| 阳朔县| 江城| 太仆寺旗| 繁昌县| 大厂| 汤原县| 甘肃省| 平阳县| 谢通门县| 阜南县|