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

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.063832
30.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}}

 

  

获取某地的经纬度 && 通过经纬度获取相应的地理位置的更多相关文章

  1. 通过百度获取IP地址对应的经纬度

    /** * 获取指定IP对应的经纬度(为空返回当前机器经纬度) *  * @param ip * @return */ public static String[] getIPXY(String ip ...

  2. JAVA文件中获取路径及WEB应用程序获取路径方法

    JAVA文件中获取路径及WEB应用程序获取路径方法 1. 基本概念的理解 `绝对路径`:你应用上的文件或目录在硬盘上真正的路径,如:URL.物理路径 例如: c:/xyz/test.txt代表了tes ...

  3. IOS获取物理尺寸中7Plus中获取的是7的物理尺寸

    IOS获取物理尺寸中7Plus中获取的是7的物理尺寸: 在开发调试过程中我的7Plus手机获取[uiscreen mainscreen].bounds为750  .1334. 解决方案:在手机中的显示 ...

  4. ASP.NET 获取来源网站的网址,获取上一网页的网址,获取来源网页的URL,获取上一网页的URL

    ASP.NET 获取来源网站的网址,获取上一网页的网址,获取来源网页的URL, 获取上一网页的URL Uri Url = HttpContext.Current.Request.UrlReferrer ...

  5. jquery只获取自身文本节点,不获取子元素的

    jQuery.text()方法时候,会把子元素的文本也获取到,以下方法可获取自身文本节点,不包括子元素 <div id="demo">只获取我<a href=&q ...

  6. JAVA获取微信小程序openid和获取公众号openid,以及通过openid获取用户信息

    一,首先说明下这个微信的openid 为了识别用户,每个用户针对每个公众号会产生一个安全的OpenID,如果需要在多公众号.移动应用之间做用户共通,则需前往微信开放平台,将这些公众号和应用绑定到一个开 ...

  7. jquery不能是使用普通的for循环 因为普通的for循环通过下表获取对象 如果通过下表获取对象的话 会转成dom对象

    jquery不能是使用普通的for循环 因为普通的for循环通过下表获取对象 如果通过下表获取对象的话 会转成dom对象

  8. linux c 网络编程:用域名获取IP地址或者用IP获取域名 网络地址转换成整型 主机字符顺序与网络字节顺序的转换

    用域名获取IP地址或者用IP获取域名 #include<stdio.h> #include<sys/socket.h> #include<netdb.h> int ...

  9. js中获取时间new date()的用法 获取时间:

    获取时间: 1 var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获取当前年份(2位) 2 myDate.getF ...

  10. 微信公众号&小程序 -- 获取并解密用户数据(获取openId、unionId)

    本文转自https://my.oschina.net/u/3235888/blog/832895 前言 微信小程序API文档:https://mp.weixin.qq.com/debug/wxadoc ...

随机推荐

  1. JavaScript一些函数

    1.prompt()函数:有两个参数,一个是显示用户输入框上面的标签,另一个是输入框的初始值.用来接收用户输入的值,然后把它返回到代码中: 例如: <doctype html> <h ...

  2. 区间合并 --- Codeforces 558D : Gess Your Way Out ! II

    D. Guess Your Way Out! II Problem's Link: http://codeforces.com/problemset/problem/558/D Mean: 一棵满二叉 ...

  3. 数学 --- 高斯消元 POJ 1830

    开关问题 Problem's Link: http://poj.org/problem?id=1830 Mean: 略 analyse: 增广矩阵:con[i][j]:若操作j,i的状态改变则con[ ...

  4. .Net 自定义应用程序配置

    .Net 自定义应用程序配置 引言 几乎所有的应用程序都离不开配置,有时候我们会将配置信息存在数据库中(例如大家可能常会见到名为Config这样的表):更多时候,我们会将配置写在Web.config或 ...

  5. Unity实现滑页效果(UGUI)

    简介 项目需要...直接展示效果吧: 原理 使用UGUI提供的ScrollRect和ScrollBar组件实现基本滑动以及自己控制每次移动一页来达到滑页的效果. 实现过程 1.创建两个panel,上面 ...

  6. 【循序渐进学Python】12.Python 正则表达式简介

    正表达式就是一段匹配文本片段的模式,在Python 中 re 模块包含了对正则表达式(regular expression)的支持. 1. 正则表达式的基本概念 1. 通配符 点号( . )可以匹配换 ...

  7. 论httpclient上传带参数【commons-httpclient和apache httpclient区别】

    需要做一个httpclient上传,然后啪啪啪网上找资料 1.首先以前系统中用到的了commons-httpclient上传,找了资料后一顿乱改,然后测试 PostMethod filePost = ...

  8. POJ 1681---Painter's Problem(高斯消元)

    POJ   1681---Painter's Problem(高斯消元) Description There is a square wall which is made of n*n small s ...

  9. 设置php下载文件的超时时间

    使用curl 可以使用curl自己实现一个curl_file_get_contents函数 //CURLOPT_FOLLOWLOCATION TRUE 时将会根据服务器返回 HTTP 头中的 &quo ...

  10. 如何使用mybatis《三》

    在前边阐述了单独使用mybatis的方法,在实际开发过程中mybatis经常和spring一起使用,即mybatis和spring进行集成,现在我们来看如何集成. mybatis和spring进行集成 ...