java - 如何在方法外部使用方法内部的值(位置)

标签 java android

我是android新手,我正在尝试制作一个程序,将一个人的纬度和经度发送到数据库并将其存储在那里。但是,我遇到了一个问题:我不知道如何使用获得的纬度和经度。 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);
        }

    }

}

最佳答案

首先停下来,花一些时间学习Java。如果不知道如何表达你想说的话,你就不会走得太远。

要将数据(纬度和经度)发送到您需要的位置,请编写 updateLoc,然后将值传递给该方法。

关于java - 如何在方法外部使用方法内部的值(位置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7617131/

相关文章:

java - 调用 onSearchRequested 时搜索对话框未激活 (Android)

java - 在 Jersey 中找不到符号 PathParam

android - 为什么我在 Android 上请求 "Device ID & call information"权限?

java - 服务器日志在哪里

java - Avro 迭代 GenericRecord

android - Admob 在第二次请求之前不会展示广告

android - 如何从 Mainactivity 类中的每个函数检索 sqlite 数据库

android - 背景附件弄乱了 Jelly Bean WebView 中的渲染?

java - 用java解析在线xml

java - spring-boot中的过滤顺序