我正在开发Android应用程序以获取我目前的GPS经度和纬度。 我有以下代码
public class setting extends Activity {
TextView longval;
TextView latval;
@Override
public void onCreate(Bundle savedInstanceState){
/** Called when the activity is first created. */
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
longval = (TextView)findViewById(R.id.longitudefield);
latval = (TextView)findViewById(R.id.latitudefield);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//Location lastLocation = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
LocationListener locationListener = new myLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
class myLocationListener implements LocationListener{
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location != null){
double longitude = location.getLongitude();
double latitude = location.getLatitude();
//Toast.makeText(getApplicationContext(), Double.toString(longitude), Toast.LENGTH_SHORT).show();
longval.setText(Double.toString(longitude));
latval.setText(Double.toString(latitude));
}
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
我不确定何时将其放入手机(Galaxy Nexus)。我看到手机顶部的栏上有一个GPS图标。但是,文本字段不会更新以显示我当前的经度和纬度。我想问一下我的代码或手机是否有问题?我只是在没有移动数据的情况下开启wifi。似乎没有调用onLocationChanged。
谢谢大家!
答案 0 :(得分:4)
使用GPS很难获得Lat,Long内部/公寓。而不是LocationManager.GPS_PROVIDER使用LocationManager.NETWORK_PROVIDER ..来解决你的问题。
答案 1 :(得分:0)
您的代码是正确的。在您的设备上安装后,将您当前的位置更改至少10米。我们从您当前的房间转到另一个至少10米远的房间。我希望您能得到您的输出......