SoFunction
Updated on 2025-03-11

Android programming implements a method of querying addresses based on latitude and longitude and parsing the obtained json data

This article describes the Android programming method of querying addresses based on latitude and longitude and parsing the obtained json data. Share it for your reference, as follows:

Step 1: Obtain json format data containing address from Google server based on the specified URL (it also provides xml format, but json parsing efficiency is higher than xml)

private static StringBuffer getJSONData(String urlPath){
    try {
      URL url = new URL(urlPath);
      HttpURLConnection httpURLConnection = (HttpURLConnection) ();
      (5000);
      ("GET");
      if(() == 200){
        InputStream inputStream = ();
        InputStreamReader isr = new InputStreamReader(inputStream);
        BufferedReader br = new BufferedReader(isr);
        String temp = null;
        StringBuffer jsonsb = new StringBuffer();
        while((temp = ()) != null){
          (temp);
        }
        return jsonsb;
      }
    } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      ();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      ();
    }
    return null;
}

Incoming latitude and longitude as parameters

/**
 * Obtain the address according to the latitude and longitude
 * @param latitude
 * @param longitude
 * @return
 */
public static StringBuffer getCurrentAddressByGPS(long latitude,long longitude){
    StringBuffer stringBuffer = new StringBuffer();
    (GOOGLE_GPS_PREFIX).append(latitude).append(",")
      .append(longitude).append(GOOGLE_GPS_SUFFIX);
    return getJSONData(());
}

Third, parse json data:

public static boolean parseAddressJSON(StringBuffer sb){
    try {
      if(sb != null){
        JSONObject jsonAllData = new JSONObject(());
        /**
          * Get a JSON array of length 1, such as: [{data content}]
          */
        String placemarkStr = ("Placemark");
        /**
          * Construct the placemarkStr array type string into a JSONArray object
          */
        JSONArray placemarkArray = new JSONArray(placemarkStr);
        /**
          * Placemark tag content is an array of length 1, obtain the content of the array and convert it into a string
          */
        String jsonDataPlacemarkStr = (0).toString();
        /**
          * Parse the string of JSON data type (jsonDataPlacemarkStr) obtained above
          */
        JSONObject jsonDataPlacemark = new JSONObject(jsonDataPlacemarkStr);
        /**
          * Get JSON data of the tag AddressDetails
          */
        String jsonAddressDetails = ("AddressDetails");
        /**
          * parse the string (jsonAddressDetails) of the JSON data type obtained above
          */
        JSONObject jsonDataAddressJDetails = new JSONObject(jsonAddressDetails);
        /**
          * Get JSON data of tag Country
          */
        String jsonCountry = ("Country");
        /**
          * Parse the string (jsonCountry) of the JSON data type obtained above
          */
        JSONObject jsonDataCountry = new JSONObject(jsonCountry);
        /**
          * Encapsulate parsed data of interest
          */
        LewatekGPSAddress lewatekGPSAddress = new LewatekGPSAddress();
        /**
          * Set CountryName
          */
        (("CountryName"));
        /**
          * Set CountryNameCode
          */
        (("CountryNameCode"));
        /**
          * Get JSON data of the tag AdministrativeArea
          */
        String jsonAdministrativeArea = ("AdministrativeArea");
        /**
          * parse the string of JSON data type (jsonAdministrativeArea) obtained above
          */
        JSONObject jsonDataAdministrativeArea = new JSONObject(jsonAdministrativeArea);
        /**
          * Set AdministrativeAreaName
          */
        (("AdministrativeAreaName"));
        /**
          * Get JSON data of tag Locality
          */
        String jsonLocality = ("Locality");
        /**
          * Parse the string (jsonLocality) of the JSON data type obtained above
          */
        JSONObject jsonDataLocality = new JSONObject(jsonLocality);
        /**
          * Set LocalityName
          */
        (("LocalityName"));
        /**
          * Get JSON data of the tag DependentLocality
          */
        String jsonDependentLocality = ("DependentLocality");
        /**
          * parse the string (jsonDependentLocality) of the JSON data type obtained above
          */
        JSONObject jsonDataDependentLocality = new JSONObject(jsonDependentLocality);
        (("DependentLocalityName"));
        (TAG,());
        return true;
      }
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      ();
    }
    return false;
}

JSON data obtained from Google server (extract data that is useful to me: CountryName, LocalityName, Administrative AreaName, DependentLocalityName, Shanghai Pudong New District, Shanghai, China (data such as Hengshan County, Hengyang, Hunan Province, China can also be extracted)):

{
 "name": "31.20322202833381,121.59876351250254",
 "Status": {
  "code": 200,
  "request": "geocode"
 },
 "Placemark": [ {
  "id": "p1",
  "address": "No. 994-1088, Zuchong Road, Pudong New District, Shanghai, China",
  "AddressDetails": {
  "Accuracy" : 8,
  "Country" : {
   "AdministrativeArea" : {
     "AdministrativeAreaName" : "Shanghai",
     "Locality" : {
      "DependentLocality" : {
        "DependentLocalityName" : "Pudong New Area",
        "Thoroughfare" : {
         "ThoroughfareName" : "No. 994-1088, Zuchong Road"
        }
      },
      "LocalityName" : "Shanghai"
     }
   },
   "CountryName" : "China",
   "CountryNameCode" : "CN"
  }
},
  "ExtendedData": {
   "LatLonBox": {
    "north": 31.2070152,
    "south": 31.2007199,
    "east": 121.6018752,
    "west": 121.5955799
   }
  },
  "Point": {
   "coordinates": [ 121.5986103, 31.2038252, 0 ]
  }
 } ]
}
Value [{"id":"p1","ExtendedData":{"LatLonBox":{"south":31.2007199,"west":121.5955799,"east":121.6018752,"north":31.2070152}},"address":"No. 994-1088, Zuchong Road, Pudong New District, Shanghai, China","Point":{"coordinates":[121.5986103,31.2038252,0]},"AddressDetails":{"Country":{"CountryNameCode":"CN","CountryName":"China","AdministrativeArea":{"Locality":{"LocalityName":"Shanghai","DependentLocality":{"DependentLocalityName":"Pudong New Area","Thoroughfare":{"ThoroughfareName":"No. 994-1088, Zuchong Road"}}},"AdministrativeAreaName":"Shanghai"}},"Accuracy":8}}] at Placemark of type  cannot be converted to JSONObject

PS: Here are a few more practical json online tools for your reference:

Online JSON code verification, inspection, beautification and formatting tools:
http://tools./code/json

JSON online formatting tool:
http://tools./code/jsonformat

Online XML/JSON mutual conversion tool:
http://tools./code/xmljson

json code online formatting/beautification/compression/editing/converting tools:
http://tools./code/jsoncodeformat

C language style/HTML/CSS/json code formatting and beautification tools:
http://tools./code/ccode_html_css_json

For more information about Android related content, please check out the topic of this site:Summary of Android data skills for operating json format》、《Android database operation skills summary》、《Android programming activity operation skills summary》、《Android file operation skills summary》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.