Java 調用Google Map Api解析地址,解析經緯度執行個體,api經緯度

來源:互聯網
上載者:User

Java 調用Google Map Api解析地址,解析經緯度執行個體,api經緯度
Java 調用Google Map Api解析地址,解析經緯度執行個體
      使用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-->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解析地址,解析經緯度執行個體

java可以調用google 地圖 api

問題是這樣的:我想使用google 地圖的反向位址解析功能,提供一個經緯度得到對應地址,在js 中是可以實現的,但是因為有太多的地址需要解析,我需要放在java 後台代碼中處理,請教該如何做? 謝謝! ------解決方案-------------------------------------------------------- 頂! 在js 的基礎上應用ajax 將你的多個地址發送到後台java 程式進行處理。 ------解決方案-------------------------------------------------------- 有一個方法,那就是你能在伺服器端運行JS,也就是讓java 引擎運行js 的代碼。 呵呵,我想目前的引擎可能還做不到吧!至少做不到那麼相容,那麼好用。 ------解決方案-------------------------------------------------------- 建議lz 分析一下Google Map API 資料, 可以考慮用Java 分析JSON ------解決方案-------------------------------------------------------- 探討 引用: 建議lz 分析一下Google Map API 資料, 可以考慮用Java 分析JSON 這個是後面要做的事情,我首先要能夠用java 調用他的API,調用成功得到資料之後再去分析。
 
為何我用google地圖的API傳入經緯度解析地址總是相差很遠?

如果經緯度準確的話,相差是不會遠的。經緯度你要看你匯入GOO GLE的是有沒經過換算的。GE的資訊是否正確。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.