google根据地址获取经纬度

第一个百度的,百度是根据key做限制的,地址解析不做限制,但是关键字查询要限制1000个
Java代码 复制代码  收藏代码
  1. package com.jueyue;   
  2.   
  3. import java.io.BufferedReader;   
  4. import java.io.IOException;   
  5. import java.io.InputStreamReader;   
  6. import java.io.UnsupportedEncodingException;   
  7. import java.net.InetSocketAddress;   
  8. import java.net.MalformedURLException;   
  9. import java.net.Proxy;   
  10. import java.net.URL;   
  11. import java.net.URLConnection;   
  12. import java.util.ArrayList;   
  13. import java.util.HashMap;   
  14. import java.util.List;   
  15. import java.util.Map;   
  16.   
  17. import com.google.gson.stream.JsonReader;   
  18.   
  19. /**  
  20.  * 获取经纬度通过  
  21.  *   
  22.  * @author jueyue 返回格式:Map<String,Object> map map.put("status",  
  23.  *         reader.nextString());//状态 map.put("result", list);//查询结果  
  24.  *         list<map<String,String>>  
  25.  *  密钥:f247cdb592eb43ebac6ccd27f796e2d2  
  26.  */  
  27. public class GetLatAndLngByBaidu {   
  28.     /**  
  29.      * @param addr  
  30.      *            查询的地址  
  31.      * @return  
  32.      */  
  33.     public Map<String, Object> getCoordinate(String addr) {   
  34.         String address = null;   
  35.         Map<String, Object> map = new HashMap<String, Object>();   
  36.         try {   
  37.             address = java.net.URLEncoder.encode(addr, "UTF-8");   
  38.         } catch (UnsupportedEncodingException e1) {   
  39.             e1.printStackTrace();   
  40.         }   
  41.         String key = "f247cdb592eb43ebac6ccd27f796e2d2";   
  42.         String url = String   
  43.                 .format("http://api.map.baidu.com/geocoder?address=%s&output=json&key=%s",   
  44.                         address, key);   
  45.         URL myURL = null;   
  46.         URLConnection httpsConn = null;   
  47.         try {   
  48.             myURL = new URL(url);   
  49.         } catch (MalformedURLException e) {   
  50.             e.printStackTrace();   
  51.         }   
  52.         try {   
  53.             httpsConn = (URLConnection) myURL.openConnection();// 不使用代理   
  54.             if (httpsConn != null) {   
  55.                 InputStreamReader insr = new InputStreamReader(   
  56.                         httpsConn.getInputStream(), "UTF-8");   
  57. //              BufferedReader br = new BufferedReader(insr);   
  58. //              String data = null;   
  59. //              while((data= br.readLine())!=null){   
  60. //                  System.out.println(data);   
  61. //              }   
  62.                 JsonReader reader = new JsonReader(insr);   
  63.                 reader.beginObject();   
  64.                 while (reader.hasNext()) {   
  65.                     String tagName = reader.nextName();   
  66.                     if (tagName.equals("result")) {   
  67.                         reader.beginObject();   
  68.                         List<Map<String, String>> list = new ArrayList<Map<String, String>>();   
  69.                         while (reader.hasNext()) {   
  70.                             Map<String, String> map_temp = new HashMap<String, String>();   
  71.                                 tagName = reader.nextName();   
  72.                                 if (tagName.equals("location")) {   
  73.                                     reader.beginObject();   
  74.                                     while (reader.hasNext()) {   
  75.                                         map_temp.put(reader.nextName(),   
  76.                                                 //(Double.valueOf(reader.nextString())-0.05+""));   
  77.                                                 reader.nextString());   
  78.                                     }   
  79.                                     reader.endObject();   
  80.                                 } else if(tagName.equals("precise")) {   
  81.                                     map_temp.put("precise", reader.nextString());   
  82.                                     //reader.skipValue();   
  83.                                 }   
  84.                             list.add(map_temp);   
  85.                         }   
  86.                         map.put("result", list);   
  87.                         reader.endObject();   
  88.                     } else if (tagName.equals("status")) {   
  89.                         map.put("status", reader.nextString());   
  90.                     }   
  91.                 }   
  92.                 insr.close();   
  93.             }   
  94.         } catch (IOException e) {   
  95.             e.printStackTrace();   
  96.         }   
  97.         return map;   
  98.     }   
  99.        
  100.        
  101.     public void getCoordinate(String longitude ,String latitude){   
  102.         String url = String.format(   
  103.                 "http://api.map.baidu.com/geocoder?output=json&" +   
  104.                 "location=%s,%s%s&key=f247cdb592eb43ebac6ccd27f796e2d2",   
  105.                 latitude, "%20",longitude);   
  106.         URL myURL = null;   
  107.         URLConnection httpsConn = null;   
  108.         try {   
  109.             myURL = new URL(url);   
  110.         } catch (MalformedURLException e) {   
  111.             e.printStackTrace();   
  112.         }   
  113.         try {   
  114.             httpsConn = (URLConnection) myURL.openConnection();   
  115.             if (httpsConn != null) {   
  116.                 InputStreamReader insr = new InputStreamReader(   
  117.                         httpsConn.getInputStream(), "UTF-8");   
  118.                 BufferedReader br = new BufferedReader(insr);   
  119.                 String data = null;   
  120.                 while ((data = br.readLine()) != null) {   
  121.                     System.out.println(data);   
  122.                 }   
  123.                 insr.close();   
  124.             }   
  125.         } catch (IOException e) {   
  126.             e.printStackTrace();   
  127.         }   
  128.            
  129.     }   
  130. }  
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代码 复制代码  收藏代码
  1. package com.jueyue;   
  2.   
  3. import java.io.IOException;   
  4. import java.io.InputStreamReader;   
  5. import java.io.UnsupportedEncodingException;   
  6. import java.net.InetSocketAddress;   
  7. import java.net.MalformedURLException;   
  8. import java.net.Proxy;   
  9. import java.net.URL;   
  10. import java.net.URLConnection;   
  11. import java.util.ArrayList;   
  12. import java.util.HashMap;   
  13. import java.util.List;   
  14. import java.util.Map;   
  15.   
  16. import com.google.gson.stream.JsonReader;   
  17.   
  18. /**  
  19.  * 获取经纬度通过google  
  20.  *   
  21.  * @author jueyue 返回格式:Map<String,Object> map map.put("status",  
  22.  *         reader.nextString());//状态 map.put("result", list);//查询结果  
  23.  *         list<map<String,String>>  
  24.  */  
  25. public class GetLatAndLngByGoogle {   
  26.     /**  
  27.      * @param isProxyip  
  28.      *            是否使用代理  
  29.      * @param addr  
  30.      *            查询的地址  
  31.      * @return  
  32.      */  
  33.     public Map<String, Object> getCoordinate(String addr, Boolean isProxyIp) {   
  34.         String address = null;   
  35.         Map<String, Object> map = new HashMap<String, Object>();   
  36.         try {   
  37.             address = java.net.URLEncoder.encode(addr, "UTF-8");   
  38.         } catch (UnsupportedEncodingException e1) {   
  39.             e1.printStackTrace();   
  40.         }   
  41.         int ipPort = (int) Math.round((Math.random() * 190));   
  42.         PostPortConst.getInstance();   
  43.         InetSocketAddress addrss = new InetSocketAddress(   
  44. //              PostPortConst.getProxyip()[ipPort][0],   
  45. //              Integer.valueOf(PostPortConst.getProxyip()[ipPort][1]));   
  46.                 "118.97.103.82",8080);   
  47.         System.out.println(PostPortConst.getProxyip()[ipPort][0]+"        "+   
  48.                 Integer.valueOf(PostPortConst.getProxyip()[ipPort][1]));   
  49.         Proxy proxy = new Proxy(Proxy.Type.HTTP, addrss);   
  50.         String key = "zh-CN";   
  51.         String url = String   
  52.                 .format("http://ditu.google.cn/maps/api/geocode/json?address=%s&sensor=false&language=%s",   
  53.                         address, key);   
  54.         URL myURL = null;   
  55.         URLConnection httpsConn = null;   
  56.         try {   
  57.             myURL = new URL(url);   
  58.         } catch (MalformedURLException e) {   
  59.             e.printStackTrace();   
  60.         }   
  61.         try {   
  62.             if (isProxyIp) {   
  63.                 httpsConn = (URLConnection) myURL.openConnection(proxy);// 使用代理   
  64.             } else {   
  65.                 httpsConn = (URLConnection) myURL.openConnection();// 不使用代理   
  66.             }   
  67.             if (httpsConn != null) {   
  68.                 InputStreamReader insr = new InputStreamReader(   
  69.                         httpsConn.getInputStream(), "UTF-8");   
  70.                 JsonReader reader = new JsonReader(insr);   
  71.                 reader.beginObject();   
  72.                 while (reader.hasNext()) {   
  73.                     String tagName = reader.nextName();   
  74.                     if (tagName.equals("results")) {   
  75.                         reader.beginArray();   
  76.                         List<Map<String, String>> list = new ArrayList<Map<String, String>>();   
  77.                         while (reader.hasNext()) {   
  78.                             reader.beginObject();   
  79.                             Map<String, String> map_temp = new HashMap<String, String>();   
  80.                             while (reader.hasNext()) {   
  81.                                 tagName = reader.nextName();   
  82.                                 if (tagName.equals("address_components")) {   
  83.                                     reader.skipValue();   
  84.                                 } else if (tagName.equals("formatted_address")) {   
  85.                                     map_temp.put("address", reader.nextString());   
  86.                                 } else if (tagName.equals("geometry")) {   
  87.                                     reader.beginObject();   
  88.                                     while (reader.hasNext()) {   
  89.                                         tagName = reader.nextName();   
  90.                                         if (tagName.equals("location")) {   
  91.                                             reader.beginObject();   
  92.                                             while (reader.hasNext()) {   
  93.                                                 map_temp.put(reader.nextName(),   
  94.                                                         reader.nextString());   
  95.                                             }   
  96.                                             reader.endObject();   
  97.                                         } else {   
  98.                                             reader.skipValue();   
  99.                                         }   
  100.                                     }   
  101.                                     reader.endObject();   
  102.                                 } else {   
  103.                                     reader.skipValue();   
  104.                                 }   
  105.                             }   
  106.                             list.add(map_temp);   
  107.                             reader.endObject();   
  108.                         }   
  109.                         map.put("result", list);   
  110.                         reader.endArray();   
  111.                     } else if (tagName.equals("status")) {   
  112.                         map.put("status", reader.nextString());   
  113.                     }   
  114.                 }   
  115.                 insr.close();   
  116.             }   
  117.         } catch (IOException e) {   
  118.             e.printStackTrace();   
  119.         }   
  120.         return map;   
  121.     }   
  122. }  
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代码 复制代码  收藏代码
  1. package com.jueyue;   
  2.   
  3. import java.io.BufferedReader;   
  4. import java.io.IOException;   
  5. import java.io.InputStreamReader;   
  6. import java.io.UnsupportedEncodingException;   
  7. import java.net.MalformedURLException;   
  8. import java.net.URL;   
  9. import java.net.URLConnection;   
  10. import java.util.ArrayList;   
  11. import java.util.HashMap;   
  12. import java.util.List;   
  13. import java.util.Map;   
  14.   
  15. import net.sf.json.JSONArray;   
  16. import net.sf.json.JSONObject;   
  17. /**  
  18.  * 高的的地图  
  19.  * @author jueyue  
  20.  *  
  21.  */  
  22. public class GetLatAndLngByGaoDeMap {   
  23.     /**  
  24.      * @param addr  
  25.      *            查询的地址  
  26.      * @return  
  27.      */  
  28.     public Map<String, Object> getCoordinate(String addr) {   
  29.         String address = null;   
  30.         Map<String, Object> map = new HashMap<String, Object>();   
  31.         try {   
  32.             address = java.net.URLEncoder.encode(addr, "UTF-8");   
  33.         } catch (UnsupportedEncodingException e1) {   
  34.             e1.printStackTrace();   
  35.         }   
  36.         String url = String   
  37.                 .format("http://api.amap.com:9090/geocode/simple?resType=json&" +   
  38.                         "encode=utf-8&range=300&roadnum=3" +   
  39.                         "&crossnum=2&poinum=2&retvalue=1" +   
  40.                         "&key=undefined&sid=7000&" +   
  41.                         "address=%s&rid=89616", address);   
  42.         URL myURL = null;   
  43.         URLConnection httpsConn = null;   
  44.         try {   
  45.             myURL = new URL(url);   
  46.         } catch (MalformedURLException e) {   
  47.             e.printStackTrace();   
  48.         }   
  49.         try {   
  50.             httpsConn = (URLConnection) myURL.openConnection();// 不使用代理   
  51.             if (httpsConn != null) {   
  52.                 InputStreamReader insr = new InputStreamReader(   
  53.                         httpsConn.getInputStream(), "UTF-8");   
  54.                 BufferedReader br = new BufferedReader(insr);   
  55.                 String data = null;   
  56.                 while ((data = br.readLine()) != null) {   
  57.                     data = data.substring(data.indexOf("{"));   
  58.                     JSONObject obj = null;   
  59.                     try {   
  60.                         obj = JSONObject.fromObject(data);   
  61.                     } catch (Exception e) {   
  62.                         e.printStackTrace();   
  63.                     }   
  64.                     //map.put("message", obj.getString("message"));   
  65.                     map.put("count", obj.getString("count"));   
  66.                     List<Map<String, String>> list = new ArrayList<Map<String, String>>();   
  67.                     JSONArray jarray = obj.getJSONArray("list");   
  68.                     for (int i = 0; i < jarray.size(); i++) {   
  69.                         Map<String, String> map_temp = new HashMap<String, String>();   
  70.                         JSONObject obj_temp = jarray.getJSONObject(i);   
  71.                         map_temp.put("name", obj_temp.getString("name"));   
  72.                         map_temp.put("x", obj_temp.getString("x"));   
  73.                         map_temp.put("y", obj_temp.getString("y"));   
  74.                         list.add(map_temp);   
  75.                     }   
  76.                     map.put("list", list);   
  77.                 }   
  78.                 insr.close();   
  79.             }   
  80.         } catch (IOException e) {   
  81.             e.printStackTrace();   
  82.         }   
  83.         return map;   
  84.     }   
  85. }  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值