java后台对百度地图地址纠偏

纠偏使用到的接口是百度提供的纠偏接口
http://api.map.baidu.com/geoconv/v1/?coords=114.21892734521,29.575429778924&from=1&to=5&ak=你的密钥
coords 需转换的源坐标,多组坐标以“;”分隔(经度,纬度)
接口文档

这个是批量纠偏

	/**
	 * @throws IOException
	 * @throws ParseException
	 * @Title: @Description:全部地点信息 @param @return @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getProductPosition", method = { RequestMethod.POST, RequestMethod.GET })
	public String getProductPosition() throws ParseException, IOException {
		List<ProductHistoryStatus> positionList = new ArrayList<ProductHistoryStatus>();
		MapUtil mapUtil = new MapUtil();
		JSONObject json = new JSONObject();
		ProductHistoryStatus info = new ProductHistoryStatus();
		positionList = productRealtimeMapper.getProductPosition();
		JSONArray jsonArray = new JSONArray();
		String locatString = "";
		String locationString = "";
		for (int i = 0; i < positionList.size(); i++) {
			info = positionList.get(i);
			if (i < positionList.size() - 1) {
				locationString = locationString + info.getLongitude() + "," + info.getLatitude() + ";";
			} else {
				locationString = locationString + info.getLongitude() + "," + info.getLatitude();
			}
		}
		String url = "http://api.map.baidu.com/geoconv/v1/?coords=" + locationString
				+ "&from=1&to=5&ak=秘钥";
		jsonArray = mapUtil.getAllEmployee(url);
		for (int i = 0; i < positionList.size(); i++) {
			info = positionList.get(i);
			JSONObject jsonObject = (JSONObject) jsonArray.get(i);
			String longitude = jsonObject.getString("x");
			String latitude = jsonObject.getString("y");
			info.setLatitude(Double.parseDouble(latitude));
			info.setLongitude(Double.parseDouble(longitude));
		}
		json.put("positionList", positionList);
		return json.toString();
	}
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class MapUtil {
	/**
	* Java后台访问url链接,返回JSONArray格式的数据
	* @return
	*/
	public static JSONArray getAllEmployee(String url) {
		try {
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpPost httpPost = new HttpPost(url);
			ResponseHandler<JSONArray> responseHandler = new ResponseHandler<JSONArray>() {
				// 成功调用连接后,对返回数据进行的操作
				@Override
				public JSONArray handleResponse(final HttpResponse response)
						throws ClientProtocolException, IOException {
					int status = response.getStatusLine().getStatusCode();
					if (status >= 200 && status < 300) {
						// 获得调用成功后 返回的数据
						HttpEntity entity = response.getEntity();
						if (null != entity) {
							String result = EntityUtils.toString(entity);
							// 根据字符串生成JSON对象
							JSONObject resultObj = JSONObject.fromObject(result);
							String s1 = resultObj.getString("result");
							JSONArray array = new JSONArray();
							array = JSONArray.fromObject(s1);
							return array;
						} else {
							return null;
						}
					} else {
						throw new ClientProtocolException("Unexpected response status: " + status);
					}
				}
			};
			// 返回的json对象
			JSONArray responseBody = httpclient.execute(httpPost, responseHandler);
			return responseBody;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	}

这个是传单个点

/**
	 * @Title: @Description:单个地点 @param @return @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getOnePosition", method = { RequestMethod.POST, RequestMethod.GET })
	public String getOnePosition(@RequestParam(value = "vin", required = false) String vin) {
		ProductHistoryStatus position = new ProductHistoryStatus();
		MapUtil mapUtil = new MapUtil();
		JSONObject json = new JSONObject();
		position = productRealtimeMapper.getOnePosition(vin);
		if (!StringUtils.isEmpty(position)) {
			String url = "http://api.map.baidu.com/geoconv/v1/?coords=" + position.getLongitude() + ","
					+ position.getLatitude() + "&from=1&to=5&ak=秘钥";
			JSONObject json2 = mapUtil.getOneEmployee(url);
			// 将经纬度解码后进行打印
			String longitude = json2.getString("x");
			String latitude = json2.getString("y");
			position.setLatitude(Double.parseDouble(latitude));
			position.setLongitude(Double.parseDouble(longitude));
		}
		json.put("position", position);
		return json.toString();
	}

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class MapUtil {
	/**
	* Java后台访问url链接,返回JSON格式的数据
	* @return
	*/
	public static JSONObject getOneEmployee(String url) {
		try {
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpPost httpPost = new HttpPost(url);
			ResponseHandler<JSONObject> responseHandler = new ResponseHandler<JSONObject>() {
				// 成功调用连接后,对返回数据进行的操作
				@Override
				public JSONObject handleResponse(final HttpResponse response)
						throws ClientProtocolException, IOException {
					int status = response.getStatusLine().getStatusCode();
					if (status >= 200 && status < 300) {
						// 获得调用成功后 返回的数据
						HttpEntity entity = response.getEntity();
						if (null != entity) {
							String result = EntityUtils.toString(entity);
							// 根据字符串生成JSON对象
							JSONObject resultObj = JSONObject.fromObject(result);
							String s1 = resultObj.getString("result");
							s1 = removeCharAt(s1, 0);
							s1 = removeCharAt(s1, s1.length() - 1);
							resultObj.clear();
							resultObj = JSONObject.fromObject(s1);
							return resultObj;
						} else {
							return null;
						}
					} else {
						throw new ClientProtocolException("Unexpected response status: " + status);
					}
				}
			};
			// 返回的json对象
			JSONObject responseBody = httpclient.execute(httpPost, responseHandler);
			return responseBody;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值