java实现gps定位路线_javaweb实现百度GPS定位接口(经纬度)

这篇博客介绍了如何使用Java实现调用百度地图的GPS定位接口,通过经纬度获取详细地址。首先需要在百度地图开放平台注册并获取AK,然后在`BaiduWebAPI`类中使用HttpClient发送GET请求,结合正则表达式处理返回的JSON数据,提取出城市和区县信息。
摘要由CSDN通过智能技术生成

百度web GPS定位(经纬度)

注册账号及配置地址

主类 BaiduWebAPI

import java.util.Map;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import org.apache.log4j.Logger;

import com.webber.cm.common.util.HttpClient;

import com.webber.cm.common.util.JsonUtil;

public class BaiduWebAPI {

static Logger logger = Logger.getLogger(BaiduWebAPI.class);

// 配置地址:http://lbsyun.baidu.com/apiconsole/key

private static final String APP_ID = "18**********";

private static final String AK = "XGXnh8tB7e*******************";

public static void main(String[] args) {

//BaiduWebAPI.ipLocation("127.0.0.1");

BaiduWebAPI.gpsLocation("116.840213","39.196272");

}

// GPS接口

public static String gpsLocation(String lng, String lat) {

String result = null;

try {

String url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=MY_AK&output=json&coordtype=wgs84ll&location=LAT_VALUE,LNG_VALUE";

url = url.replace("MY_AK", AK).replace("LNG_VALUE", lng).replace("LAT_VALUE", lat);

String reqResult = HttpClient.doGet(url);

System.out.println(reqResult);

Map map = JsonUtil.parseJSON2Map(reqResult);

Map ac = (Map) ((Map) map.get("result")).get("addressComponent");

result = ac.get("city").toString() + ac.get("district").toString();

} catch (Exception e) {

logger.error("GPS接口异常:", e);

}

logger.info("GPS接口:{lng:" + lng + ",lat:" + lat + ",result:" + result + "}");

return result;

}

// IP接口

public static String ipLocation(String ip) {

if(BaiduWebAPI.isLan(ip)) {

return "内网IP";

}

String result = null;

try {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值