Java calls the Google Map API to resolve the address, the resolution of latitude and longitude instances

Source: Internet
Author: User

Java calls the Google Map API to resolve the address, the resolution of latitude and longitude instances
Use the Reverse address resolution feature of Google Maps, provide a latitude and longitude to get the corresponding address, or give a fuzzy address, get latitude and longitude, in the Java Background code processing, this is using Google's GeoCode service. In general, there is no limit to the use of small amounts of data. According to Google's official claim that the number of requests for geocode services over 2000 times in a row is limited, so you can place these resolved addresses in database so that you can avoid repeating requests for the same address.
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> * updatio N Content: <list all contents of updation and All methods updated.> */package Com.qiuzhping.google;import JAVA.IO.B Ufferedreader;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 * @versi On [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 ";p rivate Logger log = Logger.getlogger (GoogleGeocoderUtil.class.getName ());p rivate 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! = N ull) {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 [P Arameters description] * @return void [return type description] * @throws Exception * @exception throws [Exception] [exce Ption description] * @see [related classes#related methods#related properties] */public static void Main (string[] args) th Rows Exception {try {getinstance (). SetType (2); Googlegeocodejsonbean bean = getinstance (). Geocodebyaddress ("39.90403,116.407526");} CatcH (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} 

Full Demo:java call the Google Map API parsing address, resolving latitude and longitude instances

Java calls the Google Map API to resolve the address, the resolution of latitude and longitude instances

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.