Java calls the Google Map Api to parse the address, parse the latitude and longitude instances, and api latitude and longitude

Source: Internet
Author: User

Java calls the Google Map Api to parse the address, parse the latitude and longitude instances, and api latitude and longitude
Java calls the Google Map Api to parse the address and latitude and longitude instance
Use the Reverse Address Resolution function of google Maps to provide a corresponding address by latitude and longitude, or provide a fuzzy address to obtain the latitude and longitude, which is processed in the java background code, this is Google's geocode service. Generally, when the data volume is small, the usage is unlimited. According to Google's official statement, if the number of requests to the geographic code service exceeds 2000 for 90 consecutive days, the request will be limited. Therefore, you can put these resolved addresses in the Database, this avoids repeated requests to 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> * 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();}}}

Complete Demo: Java calls Google Map Api to parse the address and latitude and longitude instance

Java can call the google map api

The problem is: I want to use the Reverse Address Resolution function of google map to provide a latitude and longitude to get the corresponding address, which can be implemented in js, but because there are too many addresses to be resolved, I need to handle it in the java background code. What should I do? Thank you! ------ Solution ---------------------------------------------------------- top! Based on js, use ajax to send multiple addresses to the background java program for processing. ------ Solution -------------------------------------------------------- one way is that you can run JS on the server side, that is, let the java engine run js Code. Well, I think the current engine may not be able to do it! At least not so compatible, so easy to use. ------ Solution -------------------------------------------------------- it is recommended that lz analyze the Google Map API data. You can consider using Java to analyze JSON ------ solution references: It is recommended that lz analyze Google Map API data, you can consider using Java to analyze JSON. This is the next thing to do. First, I must be able to use java to call his API and analyze the data after the call is successful.

Why is the resolution address of latitude and longitude always very different when I use the google map API?

If the longitude and latitude are accurate, the difference will not be far. The longitude and latitude are not converted when you import goo gle. Whether the GE information is correct.

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.