原文: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}}

Java根据百度API获得经纬度,然后根据经纬度在获得城市信息的更多相关文章

  1. java调用百度地图API依据地理位置中文获取经纬度

    百度地图api提供了非常多地图相关的免费接口,有利于地理位置相关的开发,百度地图api首页:http://developer.baidu.com/map/. 博主使用过依据地理依据地理位置中文获取经纬 ...

  2. java工具类(一)之服务端java实现根据地址从百度API获取经纬度

    服务端java实现根据地址从百度API获取经纬度 代码: package com.pb.baiduapi; import java.io.BufferedReader; import java.io. ...

  3. java根据地址获取百度API经纬度

    java根据地址获取百度API经纬度(详细文档) public void getLarLng(String address) throws Exception { String ak = " ...

  4. 调用百度API返回经纬度

    后台调用百度API接口生成: import java.io.BufferedReader; import java.io.IOException;import java.io.InputStreamR ...

  5. html5获取经纬度,百度api获取街区名,并使用JS保存进cookie

    引用js<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak= ...

  6. 百度地图Api之自定义标注:(获得标注的经纬度和中心经纬度即缩放度)

    百度地图Api之自定义标注:(获得标注的经纬度和中心经纬度即缩放度) <%@ Page Language="C#" AutoEventWireup="true&qu ...

  7. 【百度地图API】自行获取区域经纬度的工具

    原文:[百度地图API]自行获取区域经纬度的工具 摘要:上一章教大家如何建立自己的行政区域地图.这次为大家提供一个,可视化选择区域,并且能自动生成经纬度代码的工具.工具的源代码完全公开,并且做了详尽的 ...

  8. 百度api查询多个地址的经纬度的问题

    在使用百度api查询多个地址的经纬度的时候,由于百度api提供的经纬度查询方法是回调函数,并且后续操作必须等经纬度获取完成才能进行,问题就存在于怎么判断所有地点是否都回调完成了,问了之前的一个前端大佬 ...

  9. 百度api:根据经纬度获取地理位置信息

    调用百度api,根据经度和纬度获取地理位置信息,返回Json. C#代码: using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Syste ...

随机推荐

  1. Apache安装和文件配置

    Apache和Tomcat的区别是:静态文件和动态页面,C++和Java的区别. 对比.

  2. 5 秒创建 k8s 集群[转]

    据说 Google 的数据中心里运行着超过 20 亿个容器,而且 Google 十年前就开始使用容器技术. 最初,Google 开发了一个叫 Borg 的系统(现在命令为 Omega)来调度如此庞大数 ...

  3. 网页添加tittle前的图标logo

    在head标签中 <link rel="icon" href="~/favicon.ico" type="image/x-icon" ...

  4. 安卓获取数据demo出现的问题

    时间戳是long型的数据,但其他数据都是float型,但AsyncTask要求是统一数据类型.这样我就不能把时间戳放进AsyncTask里面进行处理,我就在doInBackground中获取时间戳然后 ...

  5. Python自动化测试框架——生成测试报告

    如何才能让用例自动运行完之后,生成一张直观可看易懂的测试报告呢? 小编使用的是unittest的一个扩展HTMLTestRunner 环境准备 使用之前,我们需要下载HTMLTestRunner.py ...

  6. python_面向对象笔记

    继承 什么是继承? 继承是一种新建类的方式,新建的类称为子类或派生类父类又称为基类.超类 子类可以“遗传”父类的属性,从而可以减少代码冗余 如何寻找继承关系?先抽象,再继承,继承描述的就是一种父子关系 ...

  7. Ducci Sequence解题报告

    A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... ,  ...

  8. com.alibaba.fastjson.JSONException: For input string: "8200-12-31"

    https://www.cnblogs.com/mengjinluohua/p/5544987.html https://samebug.io/exceptions/458113/com.alibab ...

  9. MD5散列算法的示例

    在很多地方,都用到了数据加密,比较多的就是MD5了,也比较安全,下面就贴上个示例,输入一串字符串,通过MD5加密 加密算法如下 public static string MD5_Encrypt(str ...

  10. PTA 02-线性结构3 Reversing Linked List (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/664 5-2 Reversing Linked List   (25分) Given a ...