准备工作
1、申请百度ak(v1版本的key) 百度地图开放平台链接:http://lbsyun.baidu.com/
2、百度地图web服务API链接:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
百度地图有json和xml两种形式存储数据
3、导入jar包:此处通过json形式,需导入jar包
json所需要的jar包如下:
json-lib-2.3-jdk15.jar
json所依赖的jar如下:
commons-beanutils-1.8.0.jar
commons-collections-3.2.1.jar
commons-lang-2.4.jar
commons-logging-1.1.jar
ezmorph-1.0.6.jar
下载jar包链接:http://download.csdn.net/download/ghjzzhg/10014271
采用maven导入jar包:
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils-core</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.3</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
代码:
package cn.zx.ghjmaven.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
/**
* @author 作者 :
* @createDate 创建时间:
* 类说明 -- 百度地图经纬度和地址相互转换的工具类
*/
public class BaiduMapUtils {
private static final String AK = "XXXXXXXX";
/**
* 百度地图通过经纬度来获取地址,传入参数纬度lat、经度lng
* @param lat
* @param lng
* @return
*/
public static String getDetailAddress(String lat, String lng) {
JSONObject obj = getLocationInfo(lat, lng).getJSONObject("result");
return obj.getString("formatted_address");
}
/**
* 获取City
* @param lat
* @param lng
* @return
*/
public static String getCity(String lat, String lng) {
JSONObject obj = getLocationInfo(lat, lng).getJSONObject("result").getJSONObject("addressComponent");
return obj.getString("city");
}
public static JSONObject getLocationInfo(String lat, String lng) {
String url = "http://api.map.baidu.com/geocoding/v3/?callback=renderReverse&location="+lat+","+lng+"&output=json&pois=0&ak="+AK;
JSONObject obj = JSONObject.fromObject(loadJSON(url));
System.out.println(obj);
return obj;
}
/**
* 百度地图通过地址来获取经纬度,传入参数address
* @param address
* @return
* Todo
*/
public static Map<String,Double> getLngAndLat(String address){
Map<String,Double> map=new HashMap<String, Double>();
String url = "http://api.map.baidu.com/geocoding/v3/?address="+address+"&output=json&ak="+AK;
String json = loadJSON(url);
JSONObject obj = JSONObject.parseObject(json);
if(obj.get("status").toString().equals("0")){
double lng=obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
double lat=obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
map.put("lng", lng);
map.put("lat", lat);
System.out.println("经度:" + lng + "--- 纬度:" + lat);
}else{
System.out.println("未找到相匹配的经纬度!");
}
return map;
}
public static String loadJSON (String url) {
StringBuilder json = new StringBuilder();
try {
URL oracle = new URL(url);
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream(),"UTF-8"));
String inputLine = null;
while ( (inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (Exception e) {
}
return json.toString();
}
}
转自https://blog.csdn.net/ghjzzhg/article/details/78193739#commentBox