Using GOOGLEAPI to query geographical names according to latitude and longitude, and the name of the base to query latitude and longitude Java code __java

Source: Internet
Author: User
Tags parse string readline

Import Java.io.BufferedReader; Import java.io.IOException; Import Java.io.InputStreamReader; Import java.io.UnsupportedEncodingException; Import java.net.MalformedURLException; Import Java.net.URL; Import java.net.URLConnection; public class Demo {public static void main (string[] args) {String addr = getaddr ("35.8616600", "104.1953970"); System.out.println (addr); Getcoordinate ("China"); /** * The reverse resolution of the address according to latitude and longitude, sometimes need to try more than a few times * Note: (Excerpt: Http://code.google.com/intl/zh-CN/apis/maps/faq.html * The number of address resolution requests submitted is limited.) If you receive more than 2,500 address resolution requests from an IP address over a 24-hour period, or if the rate of address resolution requests from an IP * address is too fast, the Google map API Encoder will start responding with a 620 status code. If the address resolver is used too much, access to the Google map API address parser from that IP * address may be permanently blocked. * @param latitude * latitude * @param longitude * Longitude * @return/public static string Getaddr (string latitude, String Longitu de) {String addr = ""; can also be http://maps.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s, but the solution is the English address//key can be casually write a KEY=ABC// Output=csv can also be XML or JSON, but the data returned using CSV is the simplest and easiest to parse String urls = String.Format ("http:ditu.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s ", latitude, longitude); URL myurl = null; URLConnection httpsconn = null; try {myurl = new URL (URL);} catch (Malformedurlexception e) {e.printstacktrace (); return null;} try {httpsconn = (url Connection) myurl.openconnection (); if (httpsconn!= null) {InputStreamReader INSR = new InputStreamReader (Httpsconn.getinputstream (), "UTF-8"); BufferedReader br = new BufferedReader (INSR); String data = null; if (data = Br.readline ())!= null) {System.out.println (data); string[] Retlist = Data.split (","); if (Retlist.length > 2 && ("retlist[0. equals")) {addr = retlist[2]; addr = Addr.replace ("/", "");} else {addr = "";}} Insr.close (); The catch (IOException e) {e.printstacktrace (); return null;} return addr; public static void Getcoordinate (String addr) {string Addrs = ""; String address = null; try {address = Java.net.URLEncoder.encode (addr, "UTF-8");} catch (Unsupportedencodingexception E1) {E1.printStackTrace (); }; String output = "csv"; String key = "abc"; String url = String.Format ("http://maps.google.com/maps/geo?q=%s&output=%s&key=%s", address, output, key); URL myurl = null; URLConnection httpsconn = null; Make transcoding try {myurl = new URL (URL);} catch (Malformedurlexception e) {e.printstacktrace ();} try {httpsconn = (Urlconnec tion) myurl.openconnection (); if (httpsconn!= null) {InputStreamReader INSR = new InputStreamReader (Httpsconn.getinputstream (), "UTF-8"); BufferedReader br = new BufferedReader (INSR); String data = null; if (data = Br.readline ())!= null) {System.out.println (data); string[] Retlist = Data.split (","); /* String latitude = retlist[2]; String longitude = retlist[3]; System.out.println ("latitude" + latitude); System.out.println ("longitude" + longitude); */if (Retlist.length > 2 && ("Retlist[0")) {Addrs = retlist[2]; Addrs = Addr.replace ("/", ""); else {Addrs = "";}} Insr.close (); } catch (IOException e) {e.printstacktRace (); } System.out.println (Addrs); } ============================ domestic detailed address has other address resolution server to provide more detailed address resolution services, or the data request to the server, the next time the user requests from their own server request, can not find the address of the Google request.

From: http://hi.baidu.com/zdz8207/blog/item/f8bbc55c36a32657faf2c025.html

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.