Parse maps longitude and latitude using JSON in Android
This error occurs when you use geocoder in googlemaps to reverse the longitude and latitude of a geographical location,
"The service is not available" & geocoder returns the list as null, and the street information is not obtained. In fact, there has been a bug in the getcoder reversal of latitude and longitude in googlemaps. Google terminated the service some time ago and added it to the Google website:
The JSON parsing package is integrated into Android, and cannot be used directly now. We now use JSON parsing data, first download the JSON package into your project, JSON parsing package URL is http://code.google.com/p/google-gson/downloads/detail? Name‑google-gson-1.7.1-release.zip. Then you enter http://maps.google.com/maps/api/geocode/json in the browser? Address = Pingdingshan Emy Ka & sensor = false. You can see a JSON data and some data of Pingdingshan Emy. Here, you can see the longitude and latitude of our Emy "location"
: {"Lat": 33.766190, "LNG": 113.1928140}, so that we can parse the longitude and latitude of the geographical location without the getcoder class. Let's take a look at the implementation code:
Public static jsonobject getlocationinfo (string address)
{// Address is the name of Pingdingshan University.
Httpget = new httpget ("http://maps.google.com/maps/api/geocode/json? Address = "+ address
+ "Ka & sensor = false ");
Httpclient client = new defaulthttpclient ();
Httpresponse;
Stringbuffer = new stringbuffer ();
Try {
Httpresponse=client.exe cute (httpget );
Httpentity = httpresponse. getentity ();
Inputstream = httpentity. getcontent ();
Int B;
While (B = inputstream. Read ())! =-1)
{
Stringbuffer. append (char) B );
}
}
Catch (clientprotocolexception E)
{
}
Catch (exception E)
{
}
Jsonobject = new jsonobject ();
Try {
Jsonobject = new jsonobject (stringbuffer. tostring ());
Log. D (tab, "stringbuffer value" + stringbuffer. tostring ());
}
Catch (exception e ){
}
Return jsonobject;
}
Public static geopoint getgeopoint (jsonobject)
{
Double Lon = new double (0); // latitude of the location
Double lat = new double (0); // longitude of the location
Try {
Lon = (jsonarray) jsonobject. Get ("Results"). getjsonobject (0)
. Getjsonobject ("Geometry"). getjsonobject ("location ")
. Getdouble ("LNG ");
Lat = (jsonarray) jsonobject. Get ("Results"). getjsonobject (0)
. Getjsonobject ("Geometry"). getjsonobject ("location ")
. Getdouble ("Lat"); // above these parameters you can in http://maps.google.com/maps/api/geocode/json? Address = Pingdingshan Emy Ka & sensor = false
}
Catch (jsonexception E)
{
Log. D (tab, "exceptions caught by the geopoint method" + E. getmessage ());
}
Catch (exception E)
{
}
Return new geopoint (INT) (LAT * 1e6), (INT) (Lon * 1e6 ));
}
In the response event, write this sentence controller. animateto (getgeopoint (getlocationinfo ("Pingdingshan Emy"); googlemaps will show my school. Hahaha, parse the geographical longitude and latitude using JSON to achieve this, it's not good to use the geocoder class anymore. Of course, I didn't come up with this code. I saw a foreigner on Google's English website writing this code. I tried it myself and did not expect it to succeed, we suggest you take a good look at this website
Thank you!