可能重复:
Android AVD wifi error
我正在使用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
}
}
或者我需要做一些事情,例如从somwhere传递纬度或经度值?或者它会自动获取当前位置值?
我在清单文件中有这些 -
<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
进入我的文本框,因为它将转向其他循环。
答案 0 :(得分:1)
正如您在comment中提到的那样,您正在运行来自eclipse的代码。让我告诉你,无法在Eclipse中获取真正的GPS Co-Ordinates。但是,您可以通过将.gpx文件加载到DDMS来模拟它。
为了同样的目的,请访问我的answer。
答案 1 :(得分:0)
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
你应该有两个而不是int,你知道lat,longs是double,你只是取整数部分。即使在小数点后4位,位置也会发生变化。