测试使用,之后持续更新
1.百度地图开放平台注册成为个人开发人员
https://lbsyun.baidu.com/apiconsole/key#/home
2.控制台—->申请密钥(AK)
3.申请密钥
申请服务端密钥名称随便起
白名单: 0.0.0.0/0
其他按需设置
申请浏览器端密钥
白名单: *
其他按需设置
4.导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
5.java代码
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* @author Katie Qu
* @date 2021/4/25
*/
public class BaiduMapGeocoderUtil {
/**
* 百度地图 Api调用相关的百度AK
*/
public final static String BAIDU_MAP_AK = "S3b9buXfNthnsH1GOF2gI2HIOufDTWhs";
//测试使用
public static void main(String[] args) {
getAddressInfoByLngAndLat("118.767413","32.041544");
}
/**
* 根据经纬度调用百度API获取 地理位置信息,根据经纬度
* @param longitude 经度
* @param latitude 纬度
* @return
*/
public static JSONObject getAddressInfoByLngAndLat(String longitude,String latitude){
JSONObject obj = new JSONObject();
String location=latitude+","+longitude;
//百度url coordtype :bd09ll(百度经纬度坐标)、bd09mc(百度米制坐标)、gcj02ll(国测局经纬度坐标,仅限中国)、wgs84ll( GPS经纬度)
String url ="http://api.map.baidu.com/reverse_geocoding/v3/?ak="+BAIDU_MAP_AK+"&output=json&coordtype=wgs84ll&location="+location;
try {
String json = loadJSON(url);
obj = JSONObject.parseObject(json);
System.out.println(obj.toString());
// status:0 成功
String success="0";
String status = String.valueOf(obj.get("status"));
if(success.equals(status)){
String result = String.valueOf(obj.get("result"));
JSONObject resultObj = JSONObject.parseObject(result);
String addressComponent = String.valueOf(resultObj.get("addressComponent"));
//JSON字符串转换成Java对象
// AddressComponent addressComponentInfo = JSONObject.parseObject(addressComponent, AddressComponent.class);
System.out.println("addressComponentInfo:"+addressComponent);
}
} catch (Exception e) {
System.out.println("未找到相匹配的经纬度,请检查地址!");
}
return obj;
}
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 (MalformedURLException e) {} catch (IOException e) {}
return json.toString();
}
}
原创文章,作者:wure,如若转载,请注明出处:https://blog.ytso.com/277072.html