package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class GetCity {
/**
* Get the user's current city through the user's current latitude and longitude with the help of Google MAP
*/
static final String GOOGLE_MAPS_API_KEY = "abcdefg";
private LocationManager locationManager;
private Location currentLocation;
private String city="National";
public GetCity(Context context) {
= (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
//Just simply get the city and there is no need for real-time updates, so I comment here first
// (
// LocationManager.GPS_PROVIDER, 1000, 0,
// new LocationListener() {
// public void onLocationChanged(Location loc) {
// // This function is triggered when the coordinates change. If the Provider passes the same coordinates, it will not be triggered
// // Save the latest location
// currentLocation = loc;
// // Update the latitude & longitude TextViews
//
// .println("getCity()"
// + (() + " " + loc
// .getLongitude()));
// }
//
// public void onProviderDisabled(String arg0) {
// (".onProviderDisabled(closed)"+arg0);
// }
//
// public void onProviderEnabled(String arg0) {
// (".onProviderEnabled(Open)"+arg0);
// }
//
// public void onStatusChanged(String arg0, int arg1,
// Bundle arg2) {
// (".onStatusChanged(Provider's transposition is available," +
// "This function is triggered when the three states are temporarily unavailable and no service is switched directly)"+
// arg0+" "+arg1+" "+arg2);
// }
// });
currentLocation = (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&&()>=2)
city=temp;
}
}.start();
}else{
("() not obtained location");
}
}
/**
* Obtain the city
* @return
*/
public String getCity(){
return city;
}
/**
* Dissect cities through Google map api
* @param loc
* @return
*/
public String reverseGeocode(Location loc) {
// /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 you want to lookup
// NOTE: I chose XML return format here but you can choose something
// else
serverAddress = new URL("/maps/geo?q="
+ (()) + ","
+ (())
+ "&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) ();
("GET");
(true);
(10000);
();
try {
InputStreamReader isr = new InputStreamReader(connection
.getInputStream());
InputSource source = new InputSource(isr);
SAXParserFactory factory = ();
SAXParser parser = ();
XMLReader xr = ();
GoogleReverseGeocodeXmlHandler handler = new GoogleReverseGeocodeXmlHandler();
(handler);
(source);
localityName = ();
("()"+localityName);
} catch (Exception ex) {
();
}
} catch (Exception ex) {
();
("()"+ex);
}
return localityName;
}
/**
* The final piece of this puzzle is parsing the xml tha