How to use a value inside a method outside of that method (location)
I'm am new to android, and I am attempting make a program that would send one's latitude and longitude to a database and store it there. However, I've hit a problem: I can't figure out how to be able to use the lat and lng obtained. The loc.getLatitude() and loc.getLongitude() are inside the method mylocationlistner which does not allow me to use outside of that... What am I supposed to do?????
public class MyLocation2 extends MapActivity {
private MapView myMap;
private MyLocationOverlay myLocOverlay;
private MapController controller;
Location loc;
LocationManager lm;
LocationListener ll;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initMap();
initMyLocation();
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
**double lat = location.getLatitude();
double lng = location.getLongitude();**
} //as u can see, they are inside here
开发者_如何学运维 public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
UpdateLoc(lat, lng); **<------THIS IS WHERE I WANT TO PLUG THE VALUES IN**
}
/**
* Initialise the map and adds the zoomcontrols to the LinearLayout.
*/
private void initMap() {
myMap = (MapView) findViewById(R.id.mymap);
@SuppressWarnings("deprecation")
View zoomView = myMap.getZoomControls();
LinearLayout myzoom = (LinearLayout) findViewById(R.id.myzoom);
myzoom.addView(zoomView);
myMap.displayZoomControls(true);
}
/**
* Initialises the MyLocationOverlay and adds it to the overlays of the map
*/
private void initMyLocation() {
myLocOverlay = new MyLocationOverlay(this, myMap);
myLocOverlay.enableMyLocation();
myMap.getOverlays().add(myLocOverlay);
controller = myMap.getController();
myLocOverlay.runOnFirstFix(new Runnable() {
public void run() {
controller.setZoom(17);
controller.animateTo(myLocOverlay.getMyLocation());
}
});
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
myLocOverlay.enableMyLocation();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
myLocOverlay.disableMyLocation();
}
public void UpdateLoc(final double latitude, final double longitude){
InputStream is = null;
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new DoubleNameValuePair("latitudeE6",latitude));
nameValuePairs.add(new DoubleNameValuePair("longitudeE6",longitude));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://bryankim.in/program/UpdateUserLoc.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
}
//for <String, Double> NameValuePair
public class DoubleNameValuePair implements NameValuePair {
String name;
double value;
public DoubleNameValuePair(String name, double value) {
this.name = name;
this.value = value;
}
@Override
public String getName() {
return name;
}
@Override
public String getValue() {
return Double.toString(value);
}
}
}
First stop and take some time to learn Java. You won't get very far without knowing how to express what you are attempting to say.
To get the data (latitude and longitude) to where you need it, write updateLoc
then pass the values to that method.
Why don't you just call your method as soon as you receive the Latitude / Longitude?
public void onLocationChanged(Location location) {
UpdateLoc(location.getLatitude(), location.getLongitude());
}
Although, instead of calling the UpdateLoc method directly, since it will run on the Thread of the listener, you should consider using your internet connection code inside an AsyncTask.
精彩评论