您的位置:首页 > 编程语言 > Go语言

google根据地址获取经纬度

2013-04-21 23:05 691 查看

google根据地址获取经纬度

博客分类:
经验

google地址解析百度地址解析

第一个百度的,百度是根据key做限制的,地址解析不做限制,但是关键字查询要限制1000个

Java代码







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();
}

}
}

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做限制的,可以使用代理,但是我测试代理效果不好

Java代码







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.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;
}
}


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

Java代码







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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: