Android obtains the city instance code through the current latitude and longitude

Source: Internet
Author: User

Copy codeThe Code is 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 {
/**
* Use Google MAP to obtain the current city of the user through the current latitude and longitude
*/
Static final String GOOGLE_MAPS_API_KEY = "abcdefg ";

Private LocationManager locationManager;
Private Location currentLocation;
Private String city = "nationwide ";
Public GetCity (Context context ){
This. locationManager = (LocationManager) context
. GetSystemService (Context. LOCATION_SERVICE );
// It's just a simple way to get the city without real-time updates. So please comment out
// This. locationManager. requestLocationUpdates (
// LocationManager. GPS_PROVIDER, 1000, 0,
// New LocationListener (){
// Public void onLocationChanged (Location loc ){
//// This function is triggered when the coordinates change. If the Provider transmits the same coordinates, it will not be triggered.
/// Save the latest location
// CurrentLocation = loc;
/// Update the latitude & longpolling TextViews
// System. out
//. Println ("getCity ()"
// + (Loc. getLatitude () + "" + loc
//. Getlongpolling ()));
//}
//
// Public void onProviderDisabled (String arg0 ){
// System. out. println (". onProviderDisabled (disabled)" + arg0 );
//}
//
// Public void onProviderEnabled (String arg0 ){
// System. out. println (". onProviderEnabled (Enabled)" + arg0 );
//}
//
// Public void onStatusChanged (String arg0, int arg1,
// Bundle arg2 ){
// System. out. println (". onStatusChanged (the Provider's transition is available," +
// "This function is triggered when three statuses are temporarily unavailable and no service are directly switched)" +
// 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 () does not get location ");
}
}

/**
* Obtain the city
* @ Return
*/
Public String getCity (){
Return city;
}

/**
* Use the Google map api to parse cities
* @ 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 & longpolling you want to lookup
// NOTE: I chose XML return format here but you can choose something
// Else
ServerAddress = new URL ("http://maps.google.com/maps/geo? Q ="
+ Double. toString (loc. getLatitude () + ","
+ Double. toString (loc. getlongpolling ())
+ "& 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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.