package com.zhaiugo.common.utils.baidumap;

 

举办结果json 1

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

不久前在做关于百度地图定位的事物,后来察觉要求后台服务层去接口调用获取百度地图定位的连带音信,于是就百度了一下网上的接口工具类发现,有很各类方法完毕,不过都写的过度臃肿,而且封装的不是很完整,有基于java.net包下的JSON对象,也有按照apache包下的httpClient请求工具类,和java.net包下的URLConnection类达成http请求,小编感到过于复杂和向下,于是就融洽下手封装了弹指间,望对大家所有帮忙。

/**
* 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(“未找到相匹配的经纬度!”);
}
}
}

import com.alibaba.fastjson.JSONObject;

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

/**
* 不带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 {
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;
}

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

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

/**
* 带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;
}

相关文章

网站地图xml地图