java调用百度定位接口_java调用百度定位api服务获取地理位置示例

本文展示了如何使用Java调用百度定位接口获取IP地址对应的地理位置信息。通过发送HTTP请求到百度API并解析返回的JSON数据,可以获取详细的地址信息。
摘要由CSDN通过智能技术生成

复制代码 代码如下:

package test;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.Reader;

import java.net.URL;

import java.nio.charset.Charset;

import org.json.JSONException;

import org.json.JSONObject;

/**

* java根据url获取json对象

* @author openks

* @since 2013-7-16

* 需要添加java-json.jar才能运行

*/

public class GetPlaceByIp {

private static String readAll(Reader rd) throws IOException {

StringBuilder sb = new StringBuilder();

int cp;

while ((cp = rd.read()) != -1) {

sb.append((char) cp);

}

return sb.toString();

}

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {

InputStream is = new URL(url).openStream();

try {

BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));

String jsonText = readAll(rd);

JSONObject json = new JSONObject(jsonText);

return json;

} finally {

is.close();

// System.out.println("同时 从这里也能看出 即便return了,仍然会执行finally的!");

}

}

public static void main(String[] args) throws IOException, JSONException {

//这里调用百度的ip定位api服务 详见 http://api.map.baidu.com/lbsapi/cloud/ip-location-api.htm

JSONObject json = readJsonFromUrl("http://api.map.baidu.com/location/ip?ak=F454f8a5efe5e577997931cc01de3974&ip=202.198.16.3");

System.out.println(json.toString());

System.out.println(((JSONObject) json.get("content")).get("address"));

}

}

时间: 2013-12-28

您可以使用百度地图API的地理编码服务实现该功能,具体步骤如下: 1. 创建百度开发者账号并申请地图API权限。 2. 在Java项目中引入百度地图APIJava SDK。 3. 调用地理编码服务API,传入地址参数即可获取对应的经纬度信息。 以下是Java代码示例: ``` import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.utils.CoordinateConverter; import com.baidu.mapapi.utils.CoordinateConverter.CoordType; import com.baidu.mapapi.utils.HttpUtil; public class GeoCoder { private static final String GEOCODER_URL = "http://api.map.baidu.com/geocoder/v2/"; private static final String AK = "your_ak"; // 替换成申请的AK public static LatLng getLatLng(String address) { String url = GEOCODER_URL + "?address=" + address + "&output=json&ak=" + AK; String response = HttpUtil.get(url, "utf-8"); JSONObject jsonObject = JSONObject.parseObject(response); int status = jsonObject.getInteger("status"); if (status == 0) { JSONObject result = jsonObject.getJSONObject("result"); JSONObject location = result.getJSONObject("location"); double lat = location.getDouble("lat"); double lng = location.getDouble("lng"); // 将百度地图坐标转换为GPS坐标 LatLng baiduLatLng = new LatLng(lat, lng); CoordinateConverter converter = new CoordinateConverter(); converter.from(CoordType.COMMON); converter.coord(baiduLatLng); LatLng gpsLatLng = converter.convert(); return gpsLatLng; } else { return null; } } } ``` 其中,AK是百度地图API的密钥,需要替换成您自己申请的密钥。该代码实现了将百度地图坐标转换为GPS坐标,如果不需要转换可以去掉相应的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值