博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取某地的经纬度 && 通过经纬度获取相应的地理位置
阅读量:4941 次
发布时间:2019-06-11

本文共 5584 字,大约阅读时间需要 18 分钟。

  最近要通过一个经纬度判断该经纬度是否位于某个地区内,所以通过网上查找资料,整合后出了下面的内容。

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","zip":""},{"addr":"四九二县道井头圩附近","cp":"NavInfo","direction":"东北","distance":"690","name":"乡下酒楼","poiType":"美食","point":{"x":112.76288029372,"y":22.748959622733},"tag":"美食;中餐厅","tel":"","uid":"b292b3aa9bf25c28be48422f","zip":""},{"addr":"四九二县道井头村附近","cp":"NavInfo","direction":"东北","distance":"724","name":"雄英酒楼","poiType":"美食","point":{"x":112.76211673404,"y":22.74900128402},"tag":"美食;中餐厅","tel":"","uid":"abe37f62d7e119b6cd04efd2","zip":""},{"addr":"四九二县道井头村附近","cp":"NavInfo","direction":"东北","distance":"730","name":"名峰酒楼","poiType":"美食","point":{"x":112.75995181776,"y":22.750776042908},"tag":"美食;中餐厅","tel":"","uid":"2fb890627b93c3a7c91b95e2","zip":""},{"addr":"杨和镇","cp":"NavInfo","direction":"东北","distance":"757","name":"山村菜馆","poiType":"美食","point":{"x":112.76165859823,"y":22.748934625955},"tag":"美食;中餐厅","tel":"","uid":"637df60ac6f49c7241afe68b","zip":""}],"poiRegions":[],"sematic_description":"皂幕山泉东北680米","cityCode":138}}

 

 

  

 

转载于:https://www.cnblogs.com/0201zcr/p/4658803.html

你可能感兴趣的文章
理解Backtracking
查看>>
T3 光
查看>>
搭建交叉调试环境 arm-linux-gdb配合gdbserver
查看>>
使用Jsoup 抓取页面的数据
查看>>
使用命令批量对文件中出现的字符串进行替换
查看>>
C#获取URL参数值
查看>>
Struts 框架 之 文件上传下载案例
查看>>
【重走Android之路】【路线篇(二)】知识点归纳
查看>>
graphviz入门
查看>>
tomcat 系统架构与设计模式 第二部分 设计模式 转
查看>>
scanf中的%[^\n]%*c格式
查看>>
启动Eclipse报Initializing Java Tooling错误解决方法
查看>>
用jquery来实现类似“网易新闻”横向标题滑动的移动端页面
查看>>
(原)基于物品的协同过滤ItemCF的mapreduce实现
查看>>
CSS可以和不可以继承的属性
查看>>
eclipse每次当我按ctrl+鼠标点击代码,自动关闭,产生原因及解决办法!!
查看>>
hbase
查看>>
用PHP将Unicode 转化为UTF-8
查看>>
HDOJ1002 A+B Problem II
查看>>
ADB server didn't ACK(adb不能开启
查看>>