0

我想做这样的应用程序:

用户单击按钮 应用程序显示用户的坐标(纬度和经度)

我正在按照这里的步骤

// Acquire a reference to the system Location Manager
    LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
          double glat = location.getLatitude();
          double glong = location.getLongitude();
          Toast.makeText(getApplicationContext(), "Your position\n"+glat+"\n"+glong, Toast.LENGTH_LONG).show();
        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}
        @Override
        public void onProviderEnabled(String provider) {}
        @Override
        public void onProviderDisabled(String provider) {}
      };

    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

如何实现按钮点击事件?

如果用户点击按钮,吐司会出现并显示位置:)

更新

我使用 startActivityForResult() 实现它,但结果为空或 null 这是我的代码:

这是我要单击的按钮上的代码

btn_obj_useKor.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View arg0) {             
     Intent intent = new Intent(getApplicationContext(), MyLocationListener.class);
     startActivityForResult(intent, 0);
   }
});

这是我的 MyLocationListener 类:

public class MyLocationListener extends Activity{

    Intent intent;
    String output = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();

        LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                double glat = location.getLatitude();
                double glong = location.getLongitude();
                output = glat+","+glong;                
            }
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {}
            @Override
            public void onProviderEnabled(String provider) {}
            @Override
            public void onProviderDisabled(String provider) {}
        };

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

        intent.putExtra("returnedData", output);
        setResult(RESULT_OK, intent);
        finish();
    }

}

当我单击按钮时,它显示 WTF!这意味着结果为空或为空。

有什么问题,我该怎么办?

4

2 回答 2

0

响应 onClick 是一个用户事件,LocationListener onLocationChanged 也是一个事件。如果在 LocationListener 事件之前触发用户事件,将没有可显示的位置。

我的建议是在按钮 onClick 处理程序中的另一个活动上调用 startActivityForResult,第二个活动包含所有 LocationManager 和 LocationListener 代码,在 onLocationChanged 事件处理程序中,您将位置传递回第一个活动。

于 2011-06-22T11:35:57.653 回答
0

它不会立即返回结果,因为 GPS 提供商需要时间来启动并找到您的位置。这就是你监听回调事件的原因。

目前您的代码正在侦听网络位置,这不是很准确。将 LocationManager 更改为 GPS_Provider(在您的最后一行)以使用 GPS(如果已启用)。

于 2011-06-22T11:22:52.140 回答