Java 调用Google Map Api解析地址,解析经纬度实例
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-->latlngpublic 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 blocke.printStackTrace();}}
}
完整的Demo:Java 调用Google Map Api解析地址,解析经纬度实例
转载于:https://my.oschina.net/qiuzhping/blog/611687
Java 调用Google Map Api解析地址,解析经纬度实例相关推荐
- Java调用Google Analytics API实现网站统计
首先,申请Google帐号和Google Analytics服务,并将统计代码放入你想统计的网站中一段时间,确保你的Google Analytics中已有数据. 在Google Analytics中, ...
- Android或者Windows Phone在HTTP调用Google Map Api时返回英文结果的解决方法
比如要提交的是地址 : http://maps.googleapis.com/maps/api/directions/json?origin=大连理工大学&destination=大连大学&a ...
- java调用百度地图API根据地理位置获取经纬度
前言: 自己申请一个百度地图得账号,然后创建自己需要服务拿到Ak 下载地址:登录百度帐号 https://lbsyun.baidu.com/apiconsole/key 1.第一步:申请账号,这一步大 ...
- java google map_java如何通过google map api实现地址解析
地址解析就是将地址(如:广东省广州市)转换为地理坐标(如经度:113.26442,纬度:23.129074)的过程.google map api提供两种方法实现地址解析. 第一种,是通过使用 GCli ...
- Java调用高德地图API根据详细地址获取经纬度
Java调用高德地图API根据详细地址获取经纬度 官方API:https://lbs.amap.com/api/webservice/guide/api/georegeo * Web服务API 地理/ ...
- 主流电子地图API比较 google map api, mapabc ,yahoo地图
主流电子地图API比较 Google Maps API : Google Maps API 基于Google Maps,能够使用 JavaScript 将 Google Maps 嵌入网页中.API ...
- google map Api接口整理
Google Map Api接口整理 一:基本知识: 1. 使用谷歌地图 API 的第一步就是要注册一个 API 密钥,需要注意一下两点: 1.如果使用 API 的页面还没有发布,只是在本地调试,可以 ...
- google Map API实现地址解析
google Map API实现地址解析 Google map API的地址解析服务目前是不收费的,可以将详细的地址转化成经纬度. <!DOCTYPE html> <html> ...
- python 调用Google Translate API进行翻译
调用Google 翻译API进行翻译 #!/usr/bin/python #coding: UTF-8 import sys reload(sys) sys.setdefaultencoding('u ...
最新文章
- 铁线蕨算法(Adiantum)为低端智能手机提供磁盘加密服务
- python的cfg是什么模块_cfg4py:一个严肃的Python配置模块应有的风格-层级式、部署环境自适应、自动补全...
- dva.js在index。tsx中获取script标签引用的文件
- centOS 6 和centOS 7 防火墙指令
- 公钥 私钥_公钥 私钥 签名 验签 说的啥?
- linux7ip路由,Centos7源地址访问路由(双IP双网关配置)
- 在jupyter上绘制caffe网络迭代时的损失和精度曲线
- 金万维异速联服务器重装,金万维异速联客户端常见错误提示及解决办法
- brep文件在线预览
- 基于智慧灯杆(路灯)的新型智慧城市物联网系统集成解决方案
- matlab激光扩束总结,zemax笔记14——激光扩束系统的设计
- 用户计算机MAC地址在哪看,电脑mac地址查询_mac地址怎么查-太平洋IT百科
- App推广要做哪些事?渠道、方案、人脉......
- python图片自动上色_自己实现黑白图片自动上色AI(一)
- android4.4系统 分屏,基于Android系统的宽屏后视镜分屏方法及系统与流程
- 电子学:第011课——实验 10:晶体管开关
- 常州大学向艳c语言答案,C程序设计(2)
- 怎么压缩gif图片大小?如何压缩gif又不影响画质
- springboot项目启动报错 url‘ attribute is not specified and no embedded datasource could be configured
- Verilog专题(二十二)Lemmings1