我正在开发 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 。
谢谢大家!