Use geocoding API in Android

Source: Internet
Author: User
Tags url example
Step 1: Construct the URL required by the geocoder APIURL format https://maps.googleapis.com/maps/api/geocode/ Output? Parameters The output option is JSON/XML, and the parameters option is address/Language/sensor. URL exampleHttps://maps.googleapis.com/maps/api/geocode/ JSON? Address = Beijing Tiananmen & language = zh_cn & sensor = false Step 2: Send an HTTP request to the API and return a JSON string
String uriapi = "http://maps.googleapis.com/maps/api/geocode/json? Address = % S & language = % S & sensor = % s "; // construct the complete URL of geocoder API = string. format (uriapi, ADDR, "zh_cn", "false"); // create a request object httpget request = new httpget (URL ); // create the httpclient object httpclient client = new defaulthttpclient (); // obtain the Request Response object httpresponse response = client.exe cute (request); // If the status code is 200, if (response. getstatusline (). getstatuscode () = 200) {// obtain the response entry -- this entry is a JSON string resultstr = entityutils. tostring (response. getentity (); geopoint = parsejson (resultstr );}
Step 3: parse the JSON string
// Parse the root element to obtain an array jsonarray jsonobjs = new jsonobject (STR ). getjsonarray ("Results"); // retrieves the first JSON object in the array (in this example, the array actually contains only one element). jsonobject jsonobj = jsonobjs. getjsonobject (0); // parses the formatted_address value string address = jsonobj. getstring ("formatted_address"); // parse the geometry jsonobject geometry = jsonobj in the json object. getjsonobject ("Geometry"); // parse the location object jsonobject location = geometry in the geometry object. getjsonobject ("location"); // parses the latitude and longpolling values in the location object string lat = location. getstring ("Lat"); string lng = location. getstring ("LNG"); dlati = double. parsedouble (LAT); dlong = double. parsedouble (LNG );

Google geocoding API Introduction

Https://developers.google.com/maps/documentation/geocoding? Hl = ZH-CN

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.