/**
* 获取经纬度通过google
*
* @author jueyue 返回格式:Map<String,Object> map map.put("status",
* reader.nextString());//状态 map.put("result", list);//查询结果
* list<map<String,String>>
*/
public static void main(String[] args) {
// 也可以是 http://maps.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s,不过解析出来的是英文地址
// 密钥可以随便写一个key=abc
// output=csv,也可以是xml或json,不过使用csv返回的数据最简洁方便解析
String addr = "1825 sw broadway";
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));
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);
httpsConn = (URLConnection) myURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(
httpsConn.getInputStream(), "UTF-8");
// BufferedReader bf = new BufferedReader(insr);
// StringBuffer sBuffer = new StringBuffer();
// String data = "";
// while ((data = bf.readLine())!= null) {
// sBuffer.append(data+"\r\n");
// }
// System.out.println(sBuffer.toString());
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();
}
System.out.println(map.toString());
}
* 获取经纬度通过google
*
* @author jueyue 返回格式:Map<String,Object> map map.put("status",
* reader.nextString());//状态 map.put("result", list);//查询结果
* list<map<String,String>>
*/
public static void main(String[] args) {
// 也可以是 http://maps.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s,不过解析出来的是英文地址
// 密钥可以随便写一个key=abc
// output=csv,也可以是xml或json,不过使用csv返回的数据最简洁方便解析
String addr = "1825 sw broadway";
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));
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);
httpsConn = (URLConnection) myURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(
httpsConn.getInputStream(), "UTF-8");
// BufferedReader bf = new BufferedReader(insr);
// StringBuffer sBuffer = new StringBuffer();
// String data = "";
// while ((data = bf.readLine())!= null) {
// sBuffer.append(data+"\r\n");
// }
// System.out.println(sBuffer.toString());
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();
}
System.out.println(map.toString());
}