最近要通過一個經(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
新聞熱點
疑難解答