原文:http://www.open-open.com/code/view/1421032487812

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; /**
* 获取经纬度
*
* @author jueyue 返回格式:Map<String,Object> map map.put("status",
* reader.nextString());//状态 map.put("result", list);//查询结果
* list<map<String,String>>
* 密钥:f247cdb592eb43ebac6ccd27f796e2d2
*/
public class GetLatAndLngByBaidu { /**
* @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 {
GetLatAndLngByBaidu getLatAndLngByBaidu = new GetLatAndLngByBaidu();
Object[] o = getLatAndLngByBaidu.getCoordinate("成都市天府四街");
System.out.println(o[0]);//经度
System.out.println(o[1]);//纬度
} }

输出结果:104.04701

30.548397

调用以下路径根据经纬度获取地址,返回数据为JSON格式,

http://api.map.baidu.com/geocoder/v2/?ak=pmCgmADsAsD9rEXkqWNcTzjd&location=30.548397,104.04701&output=json&pois=1

返回结果:

{"status":0,"result":{"location":{"lng":104.04701001273,"lat":30.548396923974},"formatted_address":"四川省成都市武侯区天府四街","business":"","addressComponent":{"city":"成都市","district":"武侯区","province":"四川省","street":"天府四街","street_number":""},"pois":[{"addr":"四川省成都市武侯区天府四街","cp":"NavInfo","distance":"144","name":"保利·心语花园","poiType":"地产小区","point":{"x":104.04823889466,"y":30.54874673449},"tel":"","uid":"881965b85a8d8a977369d348","zip":""},{"addr":"四川省成都市武侯区天府四街","cp":"NavInfo","distance":"629","name":"逸都国际","poiType":"地产小区","point":{"x":104.05247889667,"y":30.547152788724},"tel":"","uid":"e49f869c4f156be9c6effb6b","zip":""},{"addr":"四川省成都市武侯区剑南大道","cp":"NavInfo","distance":"881","name":"世豪广场","poiType":"地产小区","point":{"x":104.05252381195,"y":30.553318490823},"tel":"","uid":"a2a7b0b167a0e1997adbd206","zip":""},{"addr":"四川省成都市武侯区德赛三街","cp":"NavInfo","distance":"989","name":"顾客停车场","poiType":"交通设施","point":{"x":104.04942465794,"y":30.555798655385},"tel":"","uid":"d135a6e15571911483a00cfe","zip":""}],"cityCode":75}}

最新文章

  1. ABP(现代ASP.NET样板开发框架)系列之1、ABP总体介绍
  2. [转载]js中return的用法
  3. MVC Code First 当实体类发生变化时,如何自动更新数据库表
  4. insertBefore的兼容性问题
  5. .hpp与.h的区别
  6. POJ-1947 Rebuilding Roads (树形DP+分组背包)
  7. kettle报错 ../deploy does not exist, please create it.
  8. C# try catch finally 执行
  9. 冒烟测试、α测试、Beta测试、性能测试
  10. Go Cookie 练习
  11. Linux 电子书共享下载--大家一起学习
  12. MySQL 出现 The table is full 的解决方法
  13. DDL中drop-alter table
  14. Jquery模拟多选框(checkbox)
  15. 消除TortoiseSVN 检出到(checkout)桌面上显示一堆问号
  16. mongodb查询后排序
  17. Nowcoder OI赛制测试2 F 假的数学题 - 斯特林公式 + 二分
  18. Android-Kotlin-空值处理&amp;字符串比较&amp;常量
  19. Python笔记(三):构建发布模块
  20. django(六):view和cbv

热门文章

  1. Android(java)学习笔记169:服务(service)之为什么使用服务
  2. uva12433 Rent a Car
  3. 看云&amp;gitbook 写帮助文档 | 专注于文档在线创作、协作和托管
  4. db2快速删除大表数据(亲测可用)
  5. webpack 之 缓存处理
  6. tornado框架基础11-tornado异步
  7. 一直被用错的6种SQL 错误用法
  8. Crossed Ladders 求街道宽度 (二分法)
  9. SQL 一次插入多条记录
  10. MindManager 设置默认Note字体大小