1、地区经纬度信息获取利用Google地图API获取地区经纬度信息获取(利用Google地图API获取)package com.leg3s.rld.util;import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.
2、OutputStreamWriter;import java.io.UnsupportedEncodingException;import .HttpURLConnection;import .URL;import .URLConnection;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.apache.log4j.Logger;/* 地址经纬度信息获取工具类* * autho
3、r liuw* create date 2010/01/18*/public class AddressLatLngUtilprivate static final String REQUEST_ENCODE = UTF-8;private static final String ADDRESS_KEY = address;private static final String ADDRESS_COUNTRY_KEY = CountryName;private static final String ADDRESS_REGION_KEY = AdministrativeAreaName;pri
4、vate static final String ADDRESS_CITY_KEY = LocalityName;private static final String ADDRESS_POINT_KEY = coordinates;private static final String ADDRESS_SPLIT_STR = :;private static final String LOCATION_COUNTRY_KEY = country;private static final String LOCATION_REGION_KEY = region;private static fi
5、nal String LOCATION_CITY_KEY = city;/private static final String LOCATION_POINT_KEY = location;private static final String LOCATION_POINT_LATITUDE_KEY = latitude;private static final String LOCATION_POINT_LONGITUDE_KEY = longitude;private static final String LOCATION_POINT_PRECISIONY_KEY = accuracy;
6、private static final String LOCATION_SPLIT_STR = ,;public static final int MIN_ZOOM = 11;public static final int MAX_ZOOM = 18;private static final int DOWNNUM_FOR_PROXY = 8000;private static final int CONNECT_TIMEOUT = 30000; / 30秒private static final int THREAD_SLEEP_MILLIS = 200;private static bo
7、olean flag = false;private static int downNum = 0;private static String proxyHost = 165.228.128.10;private static String proxyPort = 3128;private static String proxySet = true;private static final Logger logger = Logger.getLogger(AddressLatLngUtil.class);/* 获取地址详细信息及经纬度信息* * param address* return Ma
8、p*/public static Map getAddressLatLng(String address)StringBuilder urlBuilder = new StringBuilder();urlBuilder.append(urlBuilder.append(?output=json);urlBuilder.append(&oe=utf-8);urlBuilder.append(&q=).append(encodeURLForUTF8(address);urlBuilder.append(&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1
9、-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA);urlBuilder.append(&mapclient=jsapi);urlBuilder.append(&hl=zh-CN);urlBuilder.append(&callback=_xdc_._1g4gm5mh3);/logger.info(urlBuilder.toString();HttpURLConnection httpConnection = null;try/ 1、构造HttpURLConnection连接URL url = new URL(urlBuilder.toString();
10、URLConnection urlConnection = url.openConnection();httpConnection = (HttpURLConnection) urlConnection;httpConnection.setDoInput(true);httpConnection.setDoOutput(true);httpConnection.setConnectTimeout(CONNECT_TIMEOUT);httpConnection.connect();/ 2、接收响应结果InputStream inStream = httpConnection.getInputSt
11、ream();String htmlContent = getContentByStream(inStream, REQUEST_ENCODE);/ 关闭流资源inStream.close();/ 3、解析结果Map map = parseAddressLatLng(htmlContent); updateProxy();return map; catch (Exception e)/ TODO Auto-generated catch blocke.printStackTrace();logger.error(=获取经纬度信息异常!请求地址: + address, e);return jav
12、a.util.Collections.emptyMap(); finally/ 关闭连接if (null != httpConnection)httpConnection.disconnect();/* 根据地址信息获取经纬度* * param addressList 地址集合* return ListMap*/public static ListMap getAddressLatLng(List addressList)ListMap list = new ArrayListMap();if (null = addressList | addressList.isEmpty()return
13、list;for (String address : addressList)try/ 休息一下Thread.sleep(THREAD_SLEEP_MILLIS); catch (InterruptedException e)/ TODO Auto-generated catch blocke.printStackTrace();Map addrMap = getAddressLatLng(address);if(false = addrMap.isEmpty()list.add(addrMap);else/System.out.println(address + point is null!
14、);logger.warn(=获取经纬度信息为空!请求地址: + address);return list;/* 解析网页内容地址信息及经纬度信息* * param htmlContent* return Map*/private static Map parseAddressLatLng(String htmlContent)Map addr = new HashMap(1);if (isNullOrEmpty(htmlContent)return addr;String contents = htmlContent.split(rn);String ss = null;String add
15、ress = null;String country = null;String region = null;String city = null;Point point = null;for (String line : contents)if (isNullOrEmpty(line)continue;line = line.trim();if (line.contains(ADDRESS_POINT_KEY)/* coordinates: 113.9465830, 22.5309650, 0 */ss = line.split(ADDRESS_SPLIT_STR);if (null !=
16、ss & ss.length 1)String pointStr = getMiddleStr(ss1, , );String pss = pointStr.split(,);if (null != pss & pss.length 1)double defaultValue = 0D;point = new Point(isNullOrEmpty(pss0)?defaultValue:Double.parseDouble(pss0.trim(), isNullOrEmpty(pss1)?defaultValue:Double.parseDouble(pss1.trim();else if (
17、line.contains(ADDRESS_KEY)address = getValue(line, ADDRESS_KEY);else if (line.contains(ADDRESS_COUNTRY_KEY)country = getValue(line, ADDRESS_COUNTRY_KEY);else if (line.contains(ADDRESS_REGION_KEY)region = getValue(line, ADDRESS_REGION_KEY);else if (line.contains(ADDRESS_CITY_KEY)city = getValue(line,
18、 ADDRESS_CITY_KEY);/ 默认取第一个地址信息if (false = isNullOrEmpty(address)& null != point)point.setCountry(country);point.setRegion(region);point.setCity(city);break; / end-for-contents/ 如果地址不为空if (false = isNullOrEmpty(address) addr.put(address, point);return addr;/* 获取中间字符串内容* * param content* param beginS
19、tr* param endStr* return*/private static String getMiddleStr(String content, String beginStr, String endStr)String str = ;if (isNullOrEmpty(content)return str;content = content.trim();int bIndex = content.indexOf(beginStr);int eIndex = -1;if (null != beginStr & beginStr.equals(endStr)int index = con
20、tent.substring(bIndex+beginStr.length().indexOf(endStr);eIndex = content.substring(0, bIndex+beginStr.length().length() + index;else if (null != endStr & false = endStr.equals(beginStr)eIndex = content.indexOf(endStr);if (-1 != bIndex & -1 != eIndex)str = content.substring(bIndex+beginStr.length(),
21、eIndex);return str;private static final String PROXY_HOST_KEY = http.proxyHost;private static final String PROXY_PORT_KEY = http.proxyPort;private static final String PROXY_SET_KEY = http.proxySet;/* 切换代理*/private static synchronized void updateProxy()downNum+;if (downNum % DOWNNUM_FOR_PROXY = 0)if
22、(flag)clearProxy();flag = false;elsesetProxy();flag = true;private static void setProxy()System.setProperty(PROXY_HOST_KEY, proxyHost);System.setProperty(PROXY_PORT_KEY, proxyPort);System.setProperty(PROXY_SET_KEY, proxySet);logger.info(setProxy=+proxyHost+:+proxyPort);/System.out.println(setProxy=+
23、proxyHost+:+proxyPort);private static void clearProxy()System.clearProperty(PROXY_HOST_KEY);System.clearProperty(PROXY_PORT_KEY);System.clearProperty(PROXY_SET_KEY);logger.info(clearProxy=);/System.out.println(clearProxy=);private static String encodeURLForUTF8(String str)trystr = .URLEncoder.encode
24、(str, UTF-8); catch (UnsupportedEncodingException e)/ TODO Auto-generated catch blocke.printStackTrace();logger.error(字符串转码异常,字符串:+ str, e);return str;/* 按照指定编码从流中读取信息* * param inStream* param encode* return* throws IOException*/private static String getContentByStream(InputStream inStream, String e
25、ncode) throws IOExceptionif (null = inStream)return null;StringBuilder content = new StringBuilder();/ 采用指定编码格式读取流内容BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, encode);String message = null;while (null != (message = reader.readLine()content.append(message);content.appe
26、nd(rn);/ 关闭读取器,释放资源reader.close();return (content.toString();/* 获取偏移后的经纬度(Google中国地图偏移接口) * 中国地图和卫星图都存在偏移量,这个是由中国规划局确定的,* google的地图服务,以ditu.gogle开头的都没有偏差,以maps.google开头的服务就有偏差* * param zoom* 偏移级别(从11级到18级,18级最精确)* param sourcePoint* 经纬度对象* return*/public static Point getOffsetLatLng(int zoom, Point
27、sourcePoint)if (null = sourcePoint)return null;StringBuilder urlBuilder = new StringBuilder();urlBuilder.append(urlBuilder.append(?spn=0.0,0.0);urlBuilder.append(&z=).append(zoom);urlBuilder.append(&vp=);urlBuilder.append(sourcePoint.getLatitude();/ 纬度urlBuilder.append(,);urlBuilder.append(sourcePoi
28、nt.getLongitude();/ 经度HttpURLConnection httpConnection = null;try/ 1、构造HttpURLConnection连接URL url = new URL(urlBuilder.toString();URLConnection urlConnection = url.openConnection();httpConnection = (HttpURLConnection) urlConnection;httpConnection.setDoInput(true);httpConnection.setDoOutput(true);httpConnection.setConnectTimeout(CONNECT_TIMEOUT);httpConnection.connect();/ 2、接收响应结果InputStream inStream = httpConnection.getInputStream();String htmlContent = getConte
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1