使用google地图的反向地址解析功能,提供一个经纬度得到对应地址,或者给出模糊地址,得到经纬度,放在java后台代码中处理,这个使用的是Google的地理编码服务。一般而言数据量不大的情况使用是不限制的。按照Google官方说法是连续90天请求地理编码服务次数超过2000次就会受到限制,因此可以将这些解析好的地址放在Database中,这样可以避免重复请求同一个地址。
JAVA Code:
/*
* System Abbrev :
* system Name :
* Component No :
* Component Name:
* File name :GoogleGeocoderUtil.java
* Author :Peter.Qiu
* Date :2014-9-18
* Description : <description>
*/
/* Updation record 1:
* Updation date : 2014-9-18
* Updator : Peter.Qiu
* Trace No: <Trace No>
* Updation No: <Updation No>
* Updation Content: <List all contents of updation and all methods updated.>
*/
package com.qiuzhping.google;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import com.qiuzhping.google.beans.GoogleGeocodeJSONBean;
/**
* <Description functions in a word>
* type :1-->address 2-->latlng
* <Detail description>
*
* @author Peter.Qiu
* @version [Version NO, 2014-9-18]
* @see [Related classes/methods]
* @since [product/module version]
*/
public final class GoogleGeocoderUtil {
public static final int ADDRESS = 1;
public static final int LATLNG = 2;
private final String GOOGLEAPIURL="http://maps.googleapis.com/maps/api/geocode/json?language=en&sensor=true";
private Logger log = Logger.getLogger(GoogleGeocoderUtil.class.getName());
private int type ;//1-->address 2-->latlng
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
private static GoogleGeocoderUtil instance;
public static GoogleGeocoderUtil getInstance() {
if(instance == null){
instance = new GoogleGeocoderUtil();
}
return instance;
}
/** <Description functions in a word>
* 2014-9-18
* <Detail description>
* @author Peter.Qiu
* @param address
* @return
* @return GoogleGeocodeJSONBean [Return type description]
* @throws Exception
* @exception throws [Exception] [Exception description]
* @see [Related classes#Related methods#Related properties]
*/
public GoogleGeocodeJSONBean geocodeByAddress(String address) throws Exception{
if(address == null || address.equals("")){
return null;
}
log.info("geocode By Address : "+address);
log.info("Start geocode");
GoogleGeocodeJSONBean bean = null;
BufferedReader in= null;
HttpURLConnection httpConn = null;
try {
log.info("Start open url");
String urlPath = GOOGLEAPIURL+"&address="+URLEncoder.encode(address,"UTF-8");;
if(this.getType() == LATLNG){
urlPath = GOOGLEAPIURL+"&latlng="+address;
}
log.info("url : "+urlPath);
URL url = new URL(urlPath);
httpConn = (HttpURLConnection) url.openConnection();
log.info("End open url");
httpConn.setDoInput(true);
in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
String line;
String result="";
while ((line = in.readLine()) != null) {
result += line;
}
in.close();
//httpConn.disconnect();
JSONObject jsonObject = JSONObject.fromObject( result );
bean = (GoogleGeocodeJSONBean) JSONObject.toBean( jsonObject, GoogleGeocodeJSONBean.class );
if(bean != null && bean.status.equalsIgnoreCase("ok") && bean.results != null && bean.results[0].geometry.getLocation() != null){
log.info("Start display Geocode info");
log.info("Formatted Address :" + bean.results[0].getFormatted_address());
log.info("geometry Location : " + bean.results[0].geometry.getLocation().getLat() + ","+bean.results[0].geometry.getLocation().getLng());
log.info("End display Geocode info");
}
log.info("End geocode");
return bean;
} catch (MalformedURLException e) {
log.error(e);
throw e;
} catch (IOException e) {
log.error(e);
throw e;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
log.error(e);
throw e;
}
}
if (httpConn != null) {
httpConn.disconnect();
}
}
}
public String getGoogleLongitudeDimensions(GoogleGeocodeJSONBean googleBean) throws IOException{
if (googleBean != null && googleBean.status.equalsIgnoreCase("ok")
&& googleBean.results[0] != null
&& googleBean.results[0].formatted_address != null
&& googleBean.results[0].getGeometry().location != null
&& googleBean.results[0].getGeometry().location.getLat() != null
&& googleBean.results[0].getGeometry().location.getLng() != null) {
String formatted_Address = googleBean.results[0].formatted_address;
String location = googleBean.results[0].getGeometry().location.getLat()+","+googleBean.results[0].getGeometry().location.getLng();
return formatted_Address.trim()+"|"+location;
}
return null;
}
/** <Description functions in a word>
* 2014-9-18
* <Detail description>
* @author Peter.Qiu
* @param args [Parameters description]
* @return void [Return type description]
* @throws Exception
* @exception throws [Exception] [Exception description]
* @see [Related classes#Related methods#Related properties]
*/
public static void main(String[] args) throws Exception {
try {
getInstance().setType(2);
GoogleGeocodeJSONBean bean = getInstance().geocodeByAddress("39.90403,116.407526");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
完整的Demo:Java 调用Google Map Api解析地址,解析经纬度实例
相关推荐
本实例主要演示Android调用google map api 的方法,根据地址获取对应经纬度,定义一个HttpClient用于向指定地址发送请求,向指定地址发送get请求,将服务器返回的字符串转换成JSONObject对象,从JSONObject中提取...
本文介绍了Google Maps API的相关基础知识,并且通过两个地图应用的实际开发,总结了Google Maps API 中,设置地图属性,添加地标和信息窗口,获取鼠标点击位置的经纬度以及在地图上添加简单图形等几个常用的功能,...
精心收集的android googlemap开发实例与教程,调用gps定位,以及给定经纬度显示地图信息等示例代码
qt qml使用高德地图的WebAPI获取静态地图,见博客 http://blog.csdn.net/d759378563/article/details/76576550
调用微信内置api获取当前经纬度坐标; 通过经纬度坐标,使用讯位置服务逆解析成详细地址; 地图marker标注; marker多点定位; 仿滴滴可拖动marker点; 搜索列表、搜索历史记录、搜索地点定位; 定位移动复位到当前...
9.6 移动版GoogleMap——Geocoder反查Address对象 9.7 规划导航路径——DirectionsRoute 9.8 移动设备上的Picasa相册——GooglePicasaAPI 9.9 随身翻译机——GoogleTranslateAPI 第10章 创意Android程序设计 10.1 ...
根据经纬度查询地址 ''' items = {'location': str(lat) + ',' + str(lng), 'ak': '你自己申请的百度ak', 'output': 'json'} res = requests.get('http://api.map.baidu.com/geocoder/v2/', params=items) ...
Google Map应用:GPS定位、规划导航路径、GPS Google地图、地址反查地理坐标等卫星全球定位实例。 创意程序设计:即时动态桌面、电蚊香、转转轮盘、任务管理器、中英发音字典、手机手电筒、GPS轨迹记录器、女性贴身...
Google Map应用:GPS定位、规划导航路径、GPS Google地图、地址反查地理坐标等卫星全球定位实例。 创意程序设计:即时动态桌面、电蚊香、转转轮盘、任务管理器、中英发音字典、手机手电筒、GPS轨迹记录器、女性贴身...
Google Map应用:GPS定位、规划导航路径、GPS Google地图、地址反查地理坐标等卫星全球定位实例。 创意程序设计:即时动态桌面、电蚊香、转转轮盘、任务管理器、中英发音字典、手机手电筒、GPS轨迹记录器、女性贴身...
利用node接收到的ip数组组装url后对百度地图api发送请求并返回请求结果数组给前端 1. 前端代码部分(jquery) 重要步骤: 1> 引用百度地图 2> 实例化百度地图,添加相关缩放控件,设置主图 3> 重写http请求,设置...
Google Map应用:GPS定位、规划导航路径、GPS Google地图、地址反查地理坐标等卫星全球定位实例。 创意程序设计:即时动态桌面、电蚊香、转转轮盘、任务管理器、中英发音字典、手机手电筒、GPS轨迹记录器、女性贴身...
9.6 移动版GoogleMap——Geocoder反查Address对象 9.7 规划导航路径——DirectionsRoute 9.8 移动设备上的Picasa相册——GooglePicasaAPI 9.9 随身翻译机——GoogleTranslateAPI 第10章 创意Android程序设计 10.1 ...
9.6 移动版GoogleMap——Geocoder反查Address对象 9.7 规划导航路径——DirectionsRoute 9.8 移动设备上的Picasa相册——GooglePicasaAPI 9.9 随身翻译机——GoogleTranslateAPI 第10章 创意Android程序设计 10.1 ...
9.6 移动版GoogleMap——Geocoder反查Address对象 9.7 规划导航路径——DirectionsRoute 9.8 移动设备上的Picasa相册——GooglePicasaAPI 9.9 随身翻译机——GoogleTranslateAPI 第10章 创意Android程序设计 ...
9.6 移动版GoogleMap——Geocoder反查Address对象 9.7 规划导航路径——DirectionsRoute 9.8 移动设备上的Picasa相册——GooglePicasaAPI 9.9 随身翻译机——GoogleTranslateAPI 第10章 创意Android程序设计 10.1 ...
9.6 移动版GoogleMap——Geocoder反查Address对象 9.7 规划导航路径——DirectionsRoute 9.8 移动设备上的Picasa相册——GooglePicasaAPI 9.9 随身翻译机——GoogleTranslateAPI 第10章 创意Android程序设计 10.1 ...