0

可能重复:
Android AVD wifi 错误

我正在使用 Android,我想在文本框中显示当前纬度值,在另一个文本框中显示当前经度值。我写了下面的代码,但它不起作用。它没有在相应的文本框中设置任何纬度或经度值。我的代码有问题吗?

public class MainActivity extends Activity implements LocationListener {
    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager locationManager;
    private String provider;

    private static final String TAG = "CurrentLocation";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        latituteField = (TextView) findViewById(R.id.lat1);
        longitudeField = (TextView) findViewById(R.id.lat2);

     // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the location provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        locationManager.requestLocationUpdates(provider, 0, 0, this);
        Location location = locationManager.getLastKnownLocation(provider);
        onLocationChanged(location);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
       if (location != null) {
           System.out.println("Provider " + provider + " has been selected.");
           int lat = (int) (location.getLatitude());
           int lng = (int) (location.getLongitude());
           Log.e("GPS", "location changed: lat="+lat+", lon="+lng);
           latituteField.setText(String.valueOf(lat));
           longitudeField.setText(String.valueOf(lng));
       } else {
           latituteField.setText("Provider not available");
           longitudeField.setText("Provider not available");
       }
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}

或者我需要做一些事情,比如从某个地方传递纬度或经度值?还是会自动获取当前位置值?

我的清单文件中有这些-

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />
    <uses-permission android:name="android.permission.INTERNET" />

还有什么我需要补充的吗?

当我在 logCAT 中看到时,我也收到了这个错误

07-18 22:43:59.836: E/Trace(3410): error opening trace file: No such file or directory (2)

我正在进入我的文本框,Provider Not Available因为它将进入其他循环。

4

2 回答 2

1

正如您在评论中提到的,您正在从 Eclipse 运行代码。让我告诉你,在 Eclipse 中无法获取真正的 GPS 坐标。但是,您可以通过将 .gpx 文件加载到 DDMS 来模拟它。

出于相同目的访问我的答案。

于 2012-07-19T06:02:37.237 回答
0
int lat = (int) (location.getLatitude());
           int lng = (int) (location.getLongitude());

你应该有 double 而不是 int,你知道 lat,long 是 double 的,你只取整数部分。即使在小数点后 4 位后位置也会发生变化。

于 2012-07-19T05:45:15.030 回答