Android obtains the city's instance code through the current latitude and longitude _android

Source: Internet
Author: User
Copy Code code as follows:

Package com.yy;
Import Java.io.InputStreamReader;
Import java.net.HttpURLConnection;
Import Java.net.URL;

Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;

Import org.xml.sax.Attributes;
Import Org.xml.sax.InputSource;
Import org.xml.sax.SAXException;
Import Org.xml.sax.XMLReader;
Import Org.xml.sax.helpers.DefaultHandler;

Import Android.content.Context;
Import android.location.Location;
Import Android.location.LocationManager;

public class Getcity {
/**
* Using Google MAP to obtain user's current city through the current latitude and longitude of users
*/
Static final String Google_maps_api_key = "ABCDEFG";

Private Locationmanager Locationmanager;
Private Location currentlocation;
Private String city= "the whole country";
Public Getcity {
This.locationmanager = (Locationmanager) context
. Getsystemservice (Context.location_service);
Simply getting the city does not need to be updated in real time so here's the comment first
This.locationManager.requestLocationUpdates (
Locationmanager.gps_provider, 1000, 0,
New Locationlistener () {
public void onlocationchanged (Location Loc) {
This function is triggered when the coordinates change, and if provider passes the same coordinates, it will not be triggered
Save the latest location
currentlocation = loc;
Update the latitude & longitude Textviews
System.out
. println ("getcity ()"
+ (Loc.getlatitude () + "" + loc
. Getlongitude ());
//     }
//
public void onproviderdisabled (String arg0) {
System.out.println (". onproviderdisabled (Close)" +arg0);
//     }
//
public void onproviderenabled (String arg0) {
System.out.println (". onproviderenabled (Open)" +arg0);
//     }
//
public void onstatuschanged (String arg0, int arg1,
Bundle arg2) {
System.out.println (". Onstatuschanged" (provider) in usable, "+
"Temporarily unavailable and no service three states direct switch when triggering this function" +
arg0+ "" +arg1+ "" +arg2);
//     }
//    });
Currentlocation = Locationmanager.getlastknownlocation (Locationmanager.gps_provider);

if (currentlocation = null)
Currentlocation = Locationmanager
. Getlastknownlocation (Locationmanager.network_provider);
}
/**
* Start parsing
*/
public void Start () {
if (currentlocation!=null) {
New Thread () {
public void Run () {
String Temp=reversegeocode (currentlocation);
if (Temp!=null&&temp.length () >=2)
City=temp;
}
}.start ();
}else{
System.out.println ("Getcity.start () not obtained location");
}
}

/**
* Access to the city
* @return
*/
Public String getcity () {
return to City;
}

/**
* Through the Google Map API to resolve the city
* @param Loc
* @return
*/
Public String Reversegeocode (Location Loc) {
Http://maps.google.com/maps/geo?q=40.714224,-73.961452&output=json&oe=utf8&sensor=true_or_false &key=your_api_key
String localityname = "";
HttpURLConnection connection = null;
URL serveraddress = null;

try {
Build the URL using the latitude & longitude your want to lookup
NOTE:I chose XML return format here but can choose something
Else
serveraddress = new URL ("http://maps.google.com/maps/geo?q="
+ double.tostring (Loc.getlatitude ()) + ","
+ double.tostring (Loc.getlongitude ())
+ "&output=xml&oe=utf8&sensor=true&key="
+ Google_maps_api_key);
Set up out communications stuff
connection = null;

Set up the initial connection
Connection = (httpurlconnection) serveraddress.openconnection ();
Connection.setrequestmethod ("get");
Connection.setdooutput (TRUE);
Connection.setreadtimeout (10000);

Connection.connect ();

try {
InputStreamReader ISR = new InputStreamReader (connection
. getInputStream ());
InputSource Source = new InputSource (ISR);
SAXParserFactory factory = Saxparserfactory.newinstance ();
SAXParser parser = Factory.newsaxparser ();
XMLReader XR = Parser.getxmlreader ();
Googlereversegeocodexmlhandler handler = new Googlereversegeocodexmlhandler ();

Xr.setcontenthandler (handler);
Xr.parse (source);

Localityname = Handler.getlocalityname ();
System.out.println ("Getcity.reversegeocode ()" +localityname);
catch (Exception ex) {
Ex.printstacktrace ();
}
catch (Exception ex) {
Ex.printstacktrace ();
System.out.println ("Getcity.reversegeocode ()" +ex);
}

return localityname;
}

/**
* The final piece of this puzzle is parsing the XML tha
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.