百度API官网 登入后点击控制台,选择应用管理、我的应用、创建应用,等应用创建成功后复制访问应用AK的值;
经纬度转换工具类
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
/**
* @Description : 百度地图地址解析
* @Author : Bruce Lee
* @CreateTime : 2024/1/12
*/
@Component
public class GetAddressUtil {
private static final String AK = "e5A5xSv2kfWsVexvXQsvhCCkqa99PGjq";
//由于接口请求有时候无法得到响应,故循环调用五次
public static Map<String,String> getMap(String address){
Map<String,String> map = null;
for(int i=0;i<5;i++){
map =getGeocoderLatitude(address);
if(map!= null){
break;
}
}
return map;
}
/**
* 返回输入地址的经纬度坐标
* key lng(经度),lat(纬度)
*/
public static Map<String,String> getGeocoderLatitude(String address){
BufferedReader in = null;
try {
//将地址转换成utf-8的16进制
address = URLEncoder.encode(address, "UTF-8");
URL tirc = new URL("http://api.map.baidu.com/geocoder?address="+ address +"&output=json&key="+ AK);
in = new BufferedReader(new InputStreamReader(tirc.openStream(),"UTF-8"));
String res;
StringBuilder sb = new StringBuilder("");
while((res = in.readLine())!=null){
sb.append(res.trim());
}
String str = sb.toString();
Map<String,String> map = null;
if(StringUtils.isNotEmpty(str)){
int lngStart = str.indexOf("lng\":");
int lngEnd = str.indexOf(",\"lat");
int latEnd = str.indexOf("},\"precise");
if(lngStart > 0 && lngEnd > 0 && latEnd > 0){
String lng = str.substring(lngStart+5, lngEnd);
String lat = str.substring(lngEnd+7, latEnd);
map = new HashMap<String,String>();
map.put("lng", lng);
map.put("lat", lat);
return map;
}
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
//经纬度转地址
public static String getAddressByLngAndLat(String lat,String lng){
String location = lat+","+lng;
String url="http://api.map.baidu.com/reverse_geocoding/v3/?ak="+AK+"&output=json&coordtype=bd09ll&location="+location;
String res=acquire(url);
//获取详细地址
String addressLocation= JSON.parseObject(res).getJSONObject("result").getString("formatted_address");
//获取省份
// String province="";
// if(res.contains("province")){
// province= JSON.parseObject(res).getJSONObject("result").getJSONObject("addressComponent").getString("province");
// }
// System.out.println(addressLocation);
return addressLocation;
}
//Http处理函数
public static String acquire(String url){
CloseableHttpClient httpClient= HttpClientBuilder.create().build();
HttpGet httpGet=new HttpGet(url);
CloseableHttpResponse response=null;
try {
response=httpClient.execute(httpGet);
HttpEntity responseEntity=response.getEntity();
if (responseEntity!=null){
return EntityUtils.toString(responseEntity);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(httpClient!=null){
httpClient.close();
}
if (response!=null){
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
参考地址
【1】https://blog.csdn.net/duan196_118/article/details/114595445
【2】百度地图API服务文档
标签:map,String,经纬度,API,org,apache,import,null,百度 From: https://www.cnblogs.com/ReturnOfTheKing/p/17960463