我正在创建一个需要GPS位置(或某个位置)的应用。
然而,在设置了听众之后,我从来没有得到第一个GPS位置,我的意思是我打开程序时的位置(并且只是坐着不动)。
我可以获得最后一个已知的位置,但由于GPS在程序未运行时启用(或者看起来似乎如此),那么我不知道这是否是正确的位置
当我打开谷歌地图时,它只需要几秒钟才能移动到实际位置,它会给出一条关于“等待位置”的消息,所以我不知道我做错了什么(或者如果这是正常)..这是我的GPS代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager loc = null;
LocationListener listener;
loc = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE) ;
Context context = getApplicationContext();
Location last = loc.getLastKnownLocation(LocationManager.GPS_PROVIDER) ;
Toast toast = Toast.makeText(context, "GPS:" + last , Toast.LENGTH_LONG );
toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);
toast.show();
listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "Status changed", Toast.LENGTH_LONG );
toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);
toast.show();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "GPS enabled", Toast.LENGTH_LONG );
toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);
toast.show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "GPS disabled", Toast.LENGTH_LONG );
toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);
toast.show();
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location == null) return ;
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "coords:" + location, Toast.LENGTH_LONG );
toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);
toast.show();
}
};
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
String bestProvider = loc.getBestProvider(criteria, true);
loc.requestLocationUpdates(bestProvider, 1000, 0, listener) ;
所以我的问题是,是否有某种方法可以调用某些东西并获取当前的GPS坐标,或者如果GPS打开(但没有运行 - 我的意思是闪烁,那么最后一个已知的位置是否包含精确的坐标最佳)? 那么当我使用getbestprovider时,我不知道最后使用的是GPS还是WIFI等。