Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中获取第一个GPS坐标(移动前)_Android_Gps - Fatal编程技术网

在Android中获取第一个GPS坐标(移动前)

在Android中获取第一个GPS坐标(移动前),android,gps,Android,Gps,我正在创建一个需要GPS定位(或某些位置)的应用程序 然而,在设置了侦听器之后,我从来没有得到第一个GPS位置,我指的是打开程序时的位置(只是坐着不动) 我可以得到最后一个已知的位置,但由于GPS是在程序不运行时打开的(或者看起来是这样),所以我不知道这是否是正确的位置 当我打开谷歌地图时,它只需要几秒钟就可以移动到实际位置,它会给出一条关于“等待位置”的消息,所以我不知道我做错了什么(或者这是否正常)。。这是我的GPS代码: public void onCreate(Bundle savedI

我正在创建一个需要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什么的。

请查看:

在启动应用程序之前是否启用了GPS(假设是真正的设备,即不是模拟器)?请求位置更新不会自动启用GPS。