我是android的新手,我正在尝试制作一个程序,将一个人的纬度和经度发送到数据库并存储在那里。但是,我遇到了一个问题:我无法弄清楚如何能够使用lat和lng获得。 loc.getLatitude()和loc.getLongitude()都在方法mylocationlistner中,它不允许我在那之外使用...我该怎么做?????
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);
}
}
}
答案 0 :(得分:2)
首先停下来,花些时间学习Java。如果不知道如何表达你想说的话,你就不会走得太远。
要将数据(纬度和经度)提供到您需要的位置,请写入updateLoc
,然后将值传递给该方法。
答案 1 :(得分:0)
为什么不在收到纬度/经度后立即致电您的方法?
public void onLocationChanged(Location location) {
UpdateLoc(location.getLatitude(), location.getLongitude());
}
虽然不是直接调用UpdateLoc方法,因为它将在侦听器的Thread上运行,但您应该考虑在AsyncTask中使用您的Internet连接代码。