Google Maps location determined by longitude and latitude-JSON Parsing

Source: Internet
Author: User

JSON parsing involves the following steps:

1. Download all JSON data on the URL

Package mars.com; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; import java.net. httpurlconnection; import java.net. URL; public class httpdownloader {// download the file based on the URL, provided that the content in the file is text, and the return value of the function is the content in the text. // 1. create a URL object // 2. create an httpurlconnection object through a URL object // 3. obtain inputstream // 4. read Private URL from inputstream; Public String download (string urlstr) {stringbuffer sb = new Stringbuffer (); string line = NULL; bufferedreader buffer = NULL; try {url = new URL (urlstr); httpurlconnection urlconn = (httpurlconnection) URL. openconnection (); buffer = new bufferedreader (New inputstreamreader (urlconn. getinputstream (); While (line = buffer. readline ())! = NULL) {sb. append (line) ;}} catch (exception e) {e. printstacktrace ();} finally {try {buffer. close ();} catch (ioexception e) {e. printstacktrace () ;}} return sb. tostring ();}}

2.

Package mars.com; import Java. util. arraylist; import Java. util. hashmap; import Java. util. iterator; import Java. util. list; import Java. util. map; import Org. JSON. jsonarray; import Org. JSON. jsonobject; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import android. widget. textview; Public class demomapjsonactivity extends activity {private button; private textview show; private edittext longpolling; private edittext latitude; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button = (button) findviewbyid (R. id. location); show = (textview) findviewbyid (R. id. show); longpolling = (edittext) findviewbyid (R. id. Longpolling); latitude = (edittext) findviewbyid (R. id. latitude); button. setonclicklistener (New mylocationlistener ();} class mylocationlistener implements onclicklistener {public void onclick (view v) {httpdownloader htt = new httpdownloader (); string strurl = "http://maps.googleapis.com/maps/api/geocode/json? Latlng = "+ latitude. gettext (). tostring () + "," + longpolling. gettext (). tostring () + "& sensor = false & language = ZH-CN"; string jsondata = htt. download (strurl); stringbuffer Buf = new stringbuffer (); try {Map <string, Object> result = parsejson (jsondata); List <Map <string, object> All = (list <Map <string, Object>) result. get ("Results"); iterator <Map <string, Object> iterator = All. iterator (); int I = 0; while (iterat Or. hasnext () {Map <string, Object> map = iterator. next (); Buf. append ("\ n address:" + map. get ("formatted_address") + "*********" + I ++); break;} Map <string, Object> address_components = parsejson (jsondata ); list <Map <string, Object> all2 = (list <Map <string, Object>) address_components.get ("address_components"); iterator <Map <string, object> iterator2 = all2.iterator (); Int J = 0; while (iterator2.hasnext () {Ma P <string, Object> map = iterator2.next (); Buf. append ("\ n address:" + map. get ("long_name") + "**********" + J ++);} catch (exception e) {e. printstacktrace ();} Show. settext (BUF) ;}} private Map <string, Object> parsejson (string data) throws exception {Map <string, Object> allmap = new hashmap <string, Object> (); jsonobject alldata = new jsonobject (data); // all content is changed to a jsonarray = alldata. getjsonarray ("resul TS "); // retrieve the array list <Map <string, Object> All = new arraylist <Map <string, Object> (); List <Map <string, object> all2 = new arraylist <Map <string, Object> (); For (INT I = 0; I <jsonarray. length (); I ++) {Map <string, Object> map = new hashmap <string, Object> (); jsonobject = jsonarray. getjsonobject (I); map. put ("formatted_address", jsonobject. getstring ("formatted_address"); All. add (MAP); if (I! = 0) {continue;} // only the first group of complete data is retrieved. None of the others have detailed jsonarray jsonarray2 = jsonobject. getjsonarray ("address_components"); For (Int J = 0; j <jsonarray2.length (); j ++) {Map <string, Object> MAP2 = new hashmap <string, object> (); jsonobject jsonobject3 = jsonarray2.getjsonobject (j); map2.put ("long_name", jsonobject3.get ("long_name"); all2.add (MAP2); system. out. println ("***");} allmap. put ("Results", all); // all the Retrieved lists are exactly the same, and all maps are placed under results. put ("address_components", all2); Return allmap ;}}

3. Main. xml file

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <edittext Android: id = "@ + ID/longpolling" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "114.3579530"/> <edittext Android: id = "@ + ID/latitude" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "30.6096780"/> <button Android: id = "@ + ID/location" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = ""/> <textview Android: id = "@ + ID/Show" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "display"/> </linearlayout>

4. Configure permissions

<Uses-Permission Android: Name = "android. Permission. Internet"/>

I am afraid that some people may not understand the code. Here I will describe the most detailed address I need here. In fact, this JSON is complicated enough, but I am actually cutting corners, I have only obtained the most detailed set of data.

And the most detailed grouping data. So no tool class is used.

The tool class should have been used. For example, the results in should be a single class, but I didn't use other attributes, so I didn't parse them in detail.

If you want to use other attributes, such as address_components and formatted_address
, Geometry, types. Then you have to write the structure of this class in detail and put it in the list as a key-value pair. Then you can retrieve any data you want.

JSON data is as follows:

{"Results": [{"address_components": [{"long_name": "1178", "short_name": "1178", "types ": ["street_number"] },{ "long_name": "Heping Avenue", "short_name": "Heping Avenue", "types": ["Route"]}, {"long_name": "Wuchang District", "short_name": "Wuchang District", "types": ["sublocality", "political"] },{ "long_name ": "Wuhan", "short_name": "Wuhan", "types": ["locality", "political"] },{ "long_name": "Hubei Province", "short_name ": "Hubei Province", "types": ["administrative_area_level_1", "political"] },{ "long_name": "China", "short_name": "cn", "types ": ["country", "political"]}], "formatted_address": "No. 1178 Heping Avenue, Wuchang district, Wuhan City, Hubei Province, China", "Geometry": {"location ": {"Lat": 30.61071999999999, "LNG": 114.3566410}, "location_type": "rooftop", "viewport": {"Northeast": {"Lat": 30.61206898029149, "LNG": 114.3579899802915}, "Southwest": {"Lat": 30.60937101970850, "LNG": 114.3552920197085 }}, "types": ["street_address"]}, {"address_components": [{"long_name": "Wuchang District", "short_name": "Wuchang District", "types": ["sublocality", "political"]}, {"long_name": "Wuhan", "short_name": "Wuhan", "types": ["locality", "political"] },{ "long_name ": "Hubei Province", "short_name": "Hubei Province", "types": ["administrative_area_level_1", "political"] },{ "long_name": "China", "short_name ": "cn", "types": ["country", "political"]}], "formatted_address": "Wuchang district, Wuhan City, Hubei Province, China", "Geometry": {"bounds ": {"Northeast": {"Lat": 30.62807880, "LNG": 114.42031060}, "Southwest": {"Lat": 30.49578920, "LNG": 114.26042410 }}, "location": {"Lat": 30.5538990, "LNG": 114.3158970}, "location_type": "Approximate", "viewport": {"Northeast": {"Lat ": 30.62807880, "LNG": 114.42031060}, "Southwest": {"Lat": 30.49578920, "LNG": 114.26042410 }}, "types": ["sublocality ", "Political"] },{ "address_components": [{"long_name": "Wuhan", "short_name": "Wuhan", "types": ["locality ", "Political"] },{ "long_name": "Hubei Province", "short_name": "Hubei Province", "types": ["administrative_area_level_1", "political"]}, {"long_name": "China", "short_name": "cn", "types": ["country", "political"]}], "formatted_address ": "Wuhan City, Hubei Province, China", "Geometry": {"bounds": {"Northeast": {"Lat": 31.36126030, "LNG": 115.08257280}, "Southwest ": {"Lat": 29.96907670, "LNG": 113.70228110 }}, "location": {"Lat": 30.5930870, "LNG": 114.3053570}, "location_type ": "Approximate", "viewport": {"Northeast": {"Lat": 30.78745989999999, "LNG": 114.6189880}, "Southwest": {"Lat": 30.34877210, "LNG": 113.9817810 }}, "types": ["locality", "political"] },{ "address_components": [{"long_name": "Hubei Province ", "short_name": "Hubei Province", "types": ["administrative_area_level_1", "political"] },{ "long_name": "China", "short_name": "cn ", "types": ["country", "political"]}], "formatted_address": "Hubei Province, China", "Geometry": {"bounds": {"Northeast ": {"Lat": 33.27561610, "LNG": 116.13484340}, "Southwest": {"Lat": 29.02948840, "LNG": 108.36696460 }}, "location ": {"Lat": 30.5458610, "LNG": 114.3419210}, "location_type": "Approximate", "viewport": {"Northeast": {"Lat": 33.27561610, "LNG": 116.13484340}, "Southwest": {"Lat": 29.02948840, "LNG": 108.36696460 }}, "types": ["administrative_area_level_1 ", "Political"] },{ "address_components": [{"long_name": "China", "short_name": "cn", "types": ["country ", "Political"]}], "formatted_address": "China", "Geometry": {"bounds": {"Northeast": {"Lat": 53.56097399999999, "LNG ": 134.772810}, "Southwest": {"Lat": 18.15352160, "LNG": 73.49941369999999 }}, "location": {"Lat": 35.861660, "LNG": 104.1953970 }, "location_type": "Approximate", "viewport": {"Northeast": {"Lat": 53.56097399999999, "LNG": 134.772810}, "Southwest": {"Lat ": 18.15352160, "LNG": 73.49941369999999 }}, "types": ["country", "political"]}], "status": "OK "}

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.