简体   繁体   English

无法获得经纬度和长时间使用GPS提供程序

[英]Unable to get lat and long using GPS provider

ReminderLogic service class invokes GPSTracker Service class. ReminderLogic服务类调用GPSTracker服务类。 isGPSEnabled is true. isGPSEnabled为true。 GPS was on in the device and was clearly under sky view. GPS在设备中处于打开状态,并且明显处于天空视野下。 Still the lat and long is 0.0. 纬度和经度仍然是0.0。

location = locationManager
                                .getLastKnownLocation(Context.LOCATION_SERVICE);

and

location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);

gives location as null. 将位置设为null。

Could someone please help me. 有人可以帮我吗。

//ReminderLogic Service Class // ReminderLogic服务类

            GPSTracker gps;
        double CurrLatitude, CurrLongitude;
        Location CurrentLoc;
        gps = new GPSTracker(HomePageActivity.this); 
if (gps.canGetLocation()) {
                CurrLatitude = gps.getLatitude();
                CurrLongitude = gps.getLongitude();

                Toast.makeText(
                        getApplicationContext(),
                        "canGetLocation is True. CurrLatitude and CurrLongitude is "
                                + CurrLatitude + CurrLongitude, Toast.LENGTH_LONG)
                        .show();
            }

//GPS Tracker Service class // GPS追踪器服务类

          if (isGPSEnabled) {

                this.canGetLocation = true;
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER, 0, 0, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(Context.LOCATION_SERVICE);
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }

try to override onLocationChanges(Location location) 尝试覆盖onLocationChanges(位置位置)

      @Override
     public void onLocationChanged(Location location) {
    if (location != null)
    {
        Log.i("SuperMap", "Location changed : Lat: " + location.getLatitude() + " Lng: " + location.getLongitude());
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        Log.i("latitude,longitude", ""+latitude+","+longitude);


    } 
}

First of all I will explain your that why your code is not working. 首先,我将向您解释为什么您的代码无法正常工作。

You are using GPS to get the location which takes some time to get your location and on getting the location fires onLocationChanged Listener. 您正在使用GPS获取位置,这需要花费一些时间才能获取位置,并且会在onLocationChanged Listener上触发位置。

However, in your code you are asking GPS to get your location using 但是,在您的代码中,您要求GPS使用以下信息获取位置

locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER, 0, 0, this);

and then expecting it to give you location in a second. 然后期望它能在一秒钟内为您提供位置。 You need a LocationListener to listen to the location when GPS tracks you. GPS跟踪您时,您需要一个LocationListener来侦听位置。 This may take upto 20 minutes and depends on whether the GPS has a direct access to a satellite eg. 这最多可能需要20分钟,具体取决于GPS是否可以直接访问卫星(例如, if your device is inside a building, GPS will take several minutes to track you but if you in open the GPS will track you fast. 如果您的设备在建筑物内,GPS将需要几分钟的时间来跟踪您,但是如果您打开设备,GPS将会快速跟踪您。

To use LocationListener just add implements LocationListener in your class declaration. 要使用LocationListener只需在类声明中添加implements LocationListener Then in onLocationChanged() use the following to get your location. 然后在onLocationChanged()中使用以下命令获取您的位置。

@Override
     public void onLocationChanged(Location location) {
    if (location != null)
    {
       Log.d("Location Found", location.getLatitude() + " " + location.getLongitude());
    locationManager.removeUpdates(this);
    } 
}

locationManager.removeUpdates(this); stops android from sending you locations endlessly and is important. 阻止android不断向您发送位置信息,这一点很重要。

Also you can the LocationListener Class internally by using 您还可以通过使用内部内部的LocationListener

LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
              Log.d("Location Found", location.getLatitude() + " " + location.getLongitude());
    locationManager.removeUpdates(this);
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    }

But you must update the requestLocationUpdates() like this 但是您必须像这样更新requestLocationUpdates()

locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER, 0, 0, locationListener);

Update:- 更新: -

You are checking if your locationManager is not null after requestLocationUpdates() which won't be null since you initialized it. 您正在检查requestLocationUpdates()之后您的locationManager是否不为null,因为您初始化它后不会为null。 Then you are using 那你在用

location = locationManager
                                .getLastKnownLocation(Context.LOCATION_SERVICE);

to get the last Known Location which will only be present if you have earlier used any app to get a location. 获取最后的已知位置,该位置仅在您之前使用过任何应用程序获取位置时才会显示。 eg. 例如。 if you use Google Maps app to get your location, then after Google Maps detect your position, you open your own app then you will get LastKnownLocation . 如果您使用Google Maps应用获取位置,则在Google Maps检测到您的位置之后,打开自己的应用即可获得LastKnownLocation This is due to the fact that android has your position from the past. 这是由于android具有过去的位置。

If you never got any location previously, then you won't get any result from it. 如果您以前从未到过任何位置,那么您将不会得到任何结果。 Besides, this is the wrong way of coding. 此外,这是错误的编码方式。 You should do like this 你应该这样

location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
}
else{
locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER, 0, 0, this);
}

This way, if you have a previous location you can use it, if not, then you get a new one. 这样,如果您有一个以前的位置,则可以使用它;如果没有,则可以使用一个新的位置。

Change your code as: 将代码更改为:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location != null) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
    }
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM