`
jueyue
  • 浏览: 193957 次
社区版块
存档分类
最新评论

google根据地址获取经纬度

阅读更多
第一个百度的,百度是根据key做限制的,地址解析不做限制,但是关键字查询要限制1000个
package com.jueyue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.stream.JsonReader;

/**
 * 获取经纬度通过
 * 
 * @author jueyue 返回格式:Map<String,Object> map map.put("status",
 *         reader.nextString());//状态 map.put("result", list);//查询结果
 *         list<map<String,String>>
 *  密钥:f247cdb592eb43ebac6ccd27f796e2d2
 */
public class GetLatAndLngByBaidu {
	/**
	 * @param addr
	 *            查询的地址
	 * @return
	 */
	public Map<String, Object> getCoordinate(String addr) {
		String address = null;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			address = java.net.URLEncoder.encode(addr, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String key = "f247cdb592eb43ebac6ccd27f796e2d2";
		String url = String
				.format("http://api.map.baidu.com/geocoder?address=%s&output=json&key=%s",
						address, key);
		URL myURL = null;
		URLConnection httpsConn = null;
		try {
			myURL = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			httpsConn = (URLConnection) myURL.openConnection();// 不使用代理
			if (httpsConn != null) {
				InputStreamReader insr = new InputStreamReader(
						httpsConn.getInputStream(), "UTF-8");
//				BufferedReader br = new BufferedReader(insr);
//				String data = null;
//				while((data= br.readLine())!=null){
//					System.out.println(data);
//				}
				JsonReader reader = new JsonReader(insr);
				reader.beginObject();
				while (reader.hasNext()) {
					String tagName = reader.nextName();
					if (tagName.equals("result")) {
						reader.beginObject();
						List<Map<String, String>> list = new ArrayList<Map<String, String>>();
						while (reader.hasNext()) {
							Map<String, String> map_temp = new HashMap<String, String>();
								tagName = reader.nextName();
								if (tagName.equals("location")) {
									reader.beginObject();
									while (reader.hasNext()) {
										map_temp.put(reader.nextName(),
												//(Double.valueOf(reader.nextString())-0.05+""));
												reader.nextString());
									}
									reader.endObject();
								} else if(tagName.equals("precise")) {
									map_temp.put("precise", reader.nextString());
									//reader.skipValue();
								}
							list.add(map_temp);
						}
						map.put("result", list);
						reader.endObject();
					} else if (tagName.equals("status")) {
						map.put("status", reader.nextString());
					}
				}
				insr.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}
	
	
	public void getCoordinate(String longitude ,String latitude){
		String url = String.format(
				"http://api.map.baidu.com/geocoder?output=json&" +
				"location=%s,%s%s&key=f247cdb592eb43ebac6ccd27f796e2d2",
				latitude, "%20",longitude);
		URL myURL = null;
		URLConnection httpsConn = null;
		try {
			myURL = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			httpsConn = (URLConnection) myURL.openConnection();
			if (httpsConn != null) {
				InputStreamReader insr = new InputStreamReader(
						httpsConn.getInputStream(), "UTF-8");
				BufferedReader br = new BufferedReader(insr);
				String data = null;
				while ((data = br.readLine()) != null) {
					System.out.println(data);
				}
				insr.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}




然后是google的,google是根据ip做限制的,可以使用代理,但是我测试代理效果不好
package com.jueyue;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.stream.JsonReader;

/**
 * 获取经纬度通过google
 * 
 * @author jueyue 返回格式:Map<String,Object> map map.put("status",
 *         reader.nextString());//状态 map.put("result", list);//查询结果
 *         list<map<String,String>>
 */
public class GetLatAndLngByGoogle {
	/**
	 * @param isProxyip
	 *            是否使用代理
	 * @param addr
	 *            查询的地址
	 * @return
	 */
	public Map<String, Object> getCoordinate(String addr, Boolean isProxyIp) {
		String address = null;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			address = java.net.URLEncoder.encode(addr, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		int ipPort = (int) Math.round((Math.random() * 190));
		PostPortConst.getInstance();
		InetSocketAddress addrss = new InetSocketAddress(
//				PostPortConst.getProxyip()[ipPort][0],
//				Integer.valueOf(PostPortConst.getProxyip()[ipPort][1]));
				"118.97.103.82",8080);
		System.out.println(PostPortConst.getProxyip()[ipPort][0]+"        "+
				Integer.valueOf(PostPortConst.getProxyip()[ipPort][1]));
		Proxy proxy = new Proxy(Proxy.Type.HTTP, addrss);
		String key = "zh-CN";
		String url = String
				.format("http://ditu.google.cn/maps/api/geocode/json?address=%s&sensor=false&language=%s",
						address, key);
		URL myURL = null;
		URLConnection httpsConn = null;
		try {
			myURL = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			if (isProxyIp) {
				httpsConn = (URLConnection) myURL.openConnection(proxy);// 使用代理
			} else {
				httpsConn = (URLConnection) myURL.openConnection();// 不使用代理
			}
			if (httpsConn != null) {
				InputStreamReader insr = new InputStreamReader(
						httpsConn.getInputStream(), "UTF-8");
				JsonReader reader = new JsonReader(insr);
				reader.beginObject();
				while (reader.hasNext()) {
					String tagName = reader.nextName();
					if (tagName.equals("results")) {
						reader.beginArray();
						List<Map<String, String>> list = new ArrayList<Map<String, String>>();
						while (reader.hasNext()) {
							reader.beginObject();
							Map<String, String> map_temp = new HashMap<String, String>();
							while (reader.hasNext()) {
								tagName = reader.nextName();
								if (tagName.equals("address_components")) {
									reader.skipValue();
								} else if (tagName.equals("formatted_address")) {
									map_temp.put("address", reader.nextString());
								} else if (tagName.equals("geometry")) {
									reader.beginObject();
									while (reader.hasNext()) {
										tagName = reader.nextName();
										if (tagName.equals("location")) {
											reader.beginObject();
											while (reader.hasNext()) {
												map_temp.put(reader.nextName(),
														reader.nextString());
											}
											reader.endObject();
										} else {
											reader.skipValue();
										}
									}
									reader.endObject();
								} else {
									reader.skipValue();
								}
							}
							list.add(map_temp);
							reader.endObject();
						}
						map.put("result", list);
						reader.endArray();
					} else if (tagName.equals("status")) {
						map.put("status", reader.nextString());
					}
				}
				insr.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}
}



接下来是高德的,这个高德的一个测试接扣,没有做限制,使用还可以,如果有谁是做高德系还是用这个比较好
package com.jueyue;

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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 高的的地图
 * @author jueyue
 *
 */
public class GetLatAndLngByGaoDeMap {
	/**
	 * @param addr
	 *            查询的地址
	 * @return
	 */
	public Map<String, Object> getCoordinate(String addr) {
		String address = null;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			address = java.net.URLEncoder.encode(addr, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String url = String
				.format("http://api.amap.com:9090/geocode/simple?resType=json&" +
						"encode=utf-8&range=300&roadnum=3" +
						"&crossnum=2&poinum=2&retvalue=1" +
						"&key=undefined&sid=7000&" +
						"address=%s&rid=89616", address);
		URL myURL = null;
		URLConnection httpsConn = null;
		try {
			myURL = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			httpsConn = (URLConnection) myURL.openConnection();// 不使用代理
			if (httpsConn != null) {
				InputStreamReader insr = new InputStreamReader(
						httpsConn.getInputStream(), "UTF-8");
				BufferedReader br = new BufferedReader(insr);
				String data = null;
				while ((data = br.readLine()) != null) {
					data = data.substring(data.indexOf("{"));
					JSONObject obj = null;
					try {
						obj = JSONObject.fromObject(data);
					} catch (Exception e) {
						e.printStackTrace();
					}
					//map.put("message", obj.getString("message"));
					map.put("count", obj.getString("count"));
					List<Map<String, String>> list = new ArrayList<Map<String, String>>();
					JSONArray jarray = obj.getJSONArray("list");
					for (int i = 0; i < jarray.size(); i++) {
						Map<String, String> map_temp = new HashMap<String, String>();
						JSONObject obj_temp = jarray.getJSONObject(i);
						map_temp.put("name", obj_temp.getString("name"));
						map_temp.put("x", obj_temp.getString("x"));
						map_temp.put("y", obj_temp.getString("y"));
						list.add(map_temp);
					}
					map.put("list", list);
				}
				insr.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}
}




百度的经纬度偏差最大,google小点,而且测试了,百度搜索还是可以的,如果做开发百度还是好点
1
1
分享到:
评论
3 楼 yuanyuan2597 2015-12-09  
谷歌那里,PostPortConst是个什么类啊?没有啊,是你自己自定义的吗?
2 楼 jueyue 2012-10-08  
hxze220 写道
之前做过一段时间的GIS,如果没弄错的话,百度的应该是使用的坐标系不同吧,在地图上显示到正确的位置需要做纠偏,国内的地图数据应有关部门的要求,都做了偏移的。。。

是的所有的都做了偏差,所以如果要用那个还是用那个地地图,我这只是举个例子
1 楼 hxze220 2012-09-30  
之前做过一段时间的GIS,如果没弄错的话,百度的应该是使用的坐标系不同吧,在地图上显示到正确的位置需要做纠偏,国内的地图数据应有关部门的要求,都做了偏移的。。。

相关推荐

Global site tag (gtag.js) - Google Analytics