百度地图获取定位信息服务接口

package com.ld.service.consult.controller;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 百度地图服务器端工具类
 * 
 * @author zhaieryuan
 * @date 2017年12月20日 上午9:57:24jar包下载
 */
public class MapUtils {

	private static String url = "http://api.map.baidu.com/geocoder/v2/";

	/**
	 * 带ur参数
	 * 
	 * @param url
	 * @param param
	 * @return
	 * @throws IOException
	 * @throws HttpException
	 * @author zhaieryuan
	 * @date 2017年12月20日 下午12:41:00
	 */
	public static JSONObject httpGet(String url, Map<String, Object> param) throws IOException, HttpException {
		JSONObject result = new JSONObject();
		String urlParam = "";
		HttpClient httpClient = new HttpClient();
		httpClient.getHostConfiguration().setHost("api.map.baidu.com", 80, "http");
		for (Map.Entry<String, Object> item : param.entrySet()) {
			urlParam += item.getKey() + "=" + URLEncoder.encode(item.getValue().toString(), "UTF-8") + "&";
		}
		url = url + "?" + urlParam;
		GetMethod getMethod = new GetMethod(url);
		getMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		getMethod.getParams().setContentCharset("utf-8");
		httpClient.executeMethod(getMethod);
		String respStr = getMethod.getResponseBodyAsString();
		JSONObject mapData = JSONObject.parseObject(respStr);
		result.put("locationDetails", mapData.getJSONObject("result"));
		result.put("mapData", mapData.getJSONObject("result"));
		result.put("lng", mapData.getJSONObject("result").getJSONObject("location").get("lng"));
		result.put("lat", mapData.getJSONObject("result").getJSONObject("location").get("lat"));
		return result;
	}

	/**
	 * 不带ur参数
	 * 
	 * @param param
	 * @return
	 * @throws IOException
	 * @throws HttpException
	 * @author zhaieryuan
	 * @date 2017年12月20日 下午12:40:51
	 */
	public static JSONObject httpGet(Map<String, Object> param) throws IOException, HttpException {
		Object JSONObject = null;
		JSONObject result = new JSONObject();
		String urlParam = "";
		HttpClient httpClient = new HttpClient();
		httpClient.getHostConfiguration().setHost("api.map.baidu.com", 80, "http");
		for (Map.Entry<String, Object> item : param.entrySet()) {
			urlParam += item.getKey() + "=" + URLEncoder.encode(item.getValue().toString(), "UTF-8") + "&";
		}
		url = url + "?" + urlParam;
		GetMethod getMethod = new GetMethod(url);
		getMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		getMethod.getParams().setContentCharset("utf-8");
		httpClient.executeMethod(getMethod);
		String respStr = getMethod.getResponseBodyAsString();
		JSONObject mapData = ((JSON) JSONObject).parseObject(respStr);
		result.put("locationDetails", mapData.getJSONObject("result"));
		result.put("mapData", mapData.getJSONObject("result"));
		result.put("lng", mapData.getJSONObject("result").getJSONObject("location").get("lng"));
		result.put("lat", mapData.getJSONObject("result").getJSONObject("location").get("lat"));
		return result;
	}

	/**
	 * demon
	 * 
	 * @param args
	 * @throws Exception
	 * @author zhaieryuan
	 * @date 2017年12月20日 下午12:38:01
	 */
	public static void main(String[] args) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ak", "STai60SY2FKuIWcK7o4TgtsgGtBFcBVP");
		param.put("output", "json");
		param.put("address", "南京香山美墅");
		try {
			JSONObject data = httpGet("http://api.map.baidu.com/geocoder/v2/", param);
			// SONObject data = httpGet("http://api.map.baidu.com/geocoder/v2/", param);
			System.out.println(data.get("lng") + "-" + data.get("lat"));
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("未找到相匹配的经纬度!");
		}
	}
}

 

转载于——https://www.cnblogs.com/zhaieryuan/p/8074978.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值