基于java的百度地图逆向解析(根据坐标返回地址)

基于java的百度地图逆向解析(根据坐标返回地址)

package com.citcc4.traffic.mirror.controller;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
 
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.RestController;
 
import com.citcc4.traffic.common.util.StringUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
 
/**
 * 
 * 项目名称:civilized-traffic-cloudmirror 类名称:LocationController 创建人: QinBo
 * 创建时间:2017年8月17日 下午2:14:37 类描述:
 * 
 * @version
 */
@RestController
public class LocationUtil {
	/**
	 * 
	 * 输入经纬度坐标返回地址 key lng(经度),lat(纬度)
	 */
	public static JsonNode getposition(String loca) {
		String lng=loca.substring(0, loca.indexOf(","));
		String lat=loca.substring(loca.indexOf(",")+1);
		String location=lat+","+lng;
		BufferedReader in = null;
		URL tirc = null;
		try {
			tirc = new URL("http://api.map.baidu.com/geocoder/v2/ak=key&location="+location+"&output=json&pois=0");
			System.out.println(tirc);
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			URLConnection connection = tirc.openConnection();
			connection.setDoOutput(true);
			in = new BufferedReader(new InputStreamReader(tirc.openStream(), "UTF-8"));
			String res;
			StringBuilder sb = new StringBuilder("");
			while ((res = in.readLine()) != null) {
				sb.append(res.trim());
			}
			String str = sb.toString();
			// System.out.println(str);
			ObjectMapper mapper = new ObjectMapper();
			if (StringUtils.isNotEmpty(str)) {
				JsonNode jsonNode = mapper.readTree(str);
				jsonNode.findValue("status").toString();
				JsonNode resultNode = jsonNode.findValue("result");
				JsonNode locationNode = resultNode.findValue("formatted_address");
				return locationNode;
			}
 		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}	 
}

原文链接:https://blog.csdn.net/u012454676/article/details/77351318

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值