地址转换成百度经纬度,数据太多!不愿意花钱?

需求

最近出了一个这样的需求,需要把几百万的数据,进行地址,进行经纬度转换

百度api

缺点:需要收费,然后看了下价格,有点贵,不愿意花钱?那没就,只能自己进行数据爬取

先找好目标网站

https://apis.map.qq.com/jsapi?qt=geoc&addr=
没错就他家了,直接拼接就能直接反显,最后得到JSON解析得到数据

技术要点

伪造IP
解析JSON
HTTP请求

开发包

commons-codec-1.9.jar
commons-httpclient-3.1.jar
commons-logging-1.1.1.jar
json-2.2.1.jar
json-lib-2.4-jdk15.jar
json-parser_fat.jar
源码下载地址:
https://download.csdn.net/download/qq_15294055/10850953
作为一个程序员,你怎摸那么多废话,老铁们上代码了

package com.bx.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import net.sf.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Random;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class InitialUtil {
	public static final String STR_F = "f";
	public static final String STR_T = "t";
	private static final String ak = "";// 百度地图经纬度反现密钥

	/** 查询百度接口地址转经�? */
	public static String loadJSON(String url) {
		StringBuilder json = new StringBuilder();
		try {
			URL oracle = new URL(url);
			URLConnection yc = oracle.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
			String inputLine = null;
			while ((inputLine = in.readLine()) != null) {
				json.append(inputLine);
			}
			in.close();
		} catch (MalformedURLException e) {
		} catch (IOException e) {
		}
		return json.toString();
	}

	/**
	 * 获取百度接口地址转经纬度
	 */
	public static String getLngAndLat(String address) {
		String json = "";
		try {
			String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&city=北京�?&output=json&ak=" + ak;
			json = loadJSON(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}

	/**
	 * 获取百度地图的经纬度
	 * 
	 * @param address
	 */
	public static String getcoorder(String address) {
		// https://jingweidu.51240.com/
		HttpClient httpClient = new HttpClient();
		try {
			String urlString = "https://apis.map.qq.com/jsapi?qt=geoc&addr=" + getURLEncoderString(address);
			GetMethod post = new GetMethod(urlString);
			String randomIp = getRandomIp();
			post.setRequestHeader("X-Real-IP", randomIp);
			post.setRequestHeader("X-Forwarded-For", randomIp);
			post.setRequestHeader("Proxy-Client-IP", randomIp);
			post.setRequestHeader("WL-Proxy-Client-IP", randomIp);
			post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); // 设置编码
			httpClient.executeMethod(post);
			// System.out.println(post.getResponseBodyAsString());
			JSONObject json = JSONObject.fromObject(post.getResponseBodyAsString());
			JSONObject jsono = json.getJSONObject("detail");
			System.out.println(json);
			return map_tx2bd(Double.valueOf(jsono.get("pointx").toString()), Double.valueOf(jsono.get("pointy").toString()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 坐标转换,腾讯地图转换成百度地图坐标
	 * 
	 * @param lat
	 *            腾讯纬度
	 * @param lon
	 *            腾讯经度
	 * @return 返回结果:经度,纬度
	 */
	public static String map_tx2bd(double lon, double lat) {
		double bd_lat;// 纬度
		double bd_lon;// 经度
		double x_pi = 3.14159265358979324;
		double x = lon, y = lat;
		double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
		double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
		bd_lon = z * Math.cos(theta) + 0.0065;
		bd_lat = z * Math.sin(theta) + 0.006;
		return bd_lon + "," + bd_lat;
	}

	/**
	 * 坐标转换,百度地图坐标转换成腾讯地图坐标
	 * 
	 * @param lat
	 *            百度坐标纬度
	 * @param lon
	 *            百度坐标经度
	 * @return 返回结果:经度,纬度
	 */
	public String map_bd2tx(double lat, double lon) {
		double tx_lat;
		double tx_lon;
		double x_pi = 3.14159265358979324;
		double x = lon - 0.0065, y = lat - 0.006;
		double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
		double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
		tx_lon = z * Math.cos(theta);
		tx_lat = z * Math.sin(theta);
		return tx_lat + "," + tx_lon;
	}

	private final static String ENCODE = "utf-8";

	/**
	 * URL 转码
	 * 
	 * @return String
	 * @date 2015-3-17 下午04:10:28
	 */
	public static String getURLEncoderString(String str) {
		String result = "";
		if (null == str) {
			return "";
		}
		try {
			result = java.net.URLEncoder.encode(str, ENCODE);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 生成随机ip数据
	 * 
	 * @return
	 */
	public static String getRandomIp() {
		// ip范围
		int[][] range = { { 607649792, 608174079 }, // 36.56.0.0-36.63.255.255
				{ 1038614528, 1039007743 }, // 61.232.0.0-61.237.255.255
				{ 1783627776, 1784676351 }, // 106.80.0.0-106.95.255.255
				{ 2035023872, 2035154943 }, // 121.76.0.0-121.77.255.255
				{ 2078801920, 2079064063 }, // 123.232.0.0-123.235.255.255
				{ -1950089216, -1948778497 }, // 139.196.0.0-139.215.255.255
				{ -1425539072, -1425014785 }, // 171.8.0.0-171.15.255.255
				{ -1236271104, -1235419137 }, // 182.80.0.0-182.92.255.255
				{ -770113536, -768606209 }, // 210.25.0.0-210.47.255.255
				{ -569376768, -564133889 }, // 222.16.0.0-222.95.255.255
		};

		Random rdint = new Random();
		int index = rdint.nextInt(10);
		String ip = num2ip(range[index][0] + new Random().nextInt(range[index][1] - range[index][0]));
		return ip;
	}

	/*
	 * 将十进制转换成IP地址
	 */
	public static String num2ip(int ip) {
		int[] b = new int[4];
		String x = "";
		b[0] = (int) ((ip >> 24) & 0xff);
		b[1] = (int) ((ip >> 16) & 0xff);
		b[2] = (int) ((ip >> 8) & 0xff);
		b[3] = (int) (ip & 0xff);
		x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[3]);
		return x;
	}

	// 测试方法
	public static void main(String[] args) {
		// https://jingweidu.51240.com/

		// String string = InitialUtil.getcoorder("北京市海�?区有福敬老院");
		// System.out.println(string);

		HttpClient httpClient = new HttpClient();
		for (int i = 0; i < 1; i++) {
			try {
				String address = getURLEncoderString("北京市海�?区有福敬老院");
				String urlString = "https://apis.map.qq.com/jsapi?qt=geoc&addr=" + address;
				GetMethod post = new GetMethod(urlString);
				String randomIp = getRandomIp();
				post.setRequestHeader("X-Real-IP", randomIp);
				post.setRequestHeader("X-Forwarded-For", randomIp);
				post.setRequestHeader("Proxy-Client-IP", randomIp);
				post.setRequestHeader("WL-Proxy-Client-IP", randomIp);
				post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); // 设置编码
				httpClient.executeMethod(post);
				// System.out.println(post.getResponseBodyAsString());
				JSONObject json = JSONObject.fromObject(post.getResponseBodyAsString());// new
																						// JSONObject();
				JSONObject jsono = json.getJSONObject("detail");
				System.out.println(json);
				System.out.println("解析出来的经度:" + String.format("%.7f", Double.parseDouble(jsono.get("pointx").toString())));
				System.out.println("解析出来的纬度:" + String.format("%.7f", Double.parseDouble(jsono.get("pointy").toString())));
				System.out.println("转换成百度经纬度�?" + map_tx2bd(Double.valueOf(jsono.get("pointx").toString()), Double.valueOf(jsono.get("pointy").toString())));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

发布了6 篇原创文章 · 获赞 10 · 访问量 1491

猜你喜欢

转载自blog.csdn.net/qq_15294055/article/details/85006270