【问题标题】:Get location instantly after enabling location services启用位置服务后立即获取位置
【发布时间】:2017-01-17 12:22:36
【问题描述】:

有什么方法可以在用户启用定位服务后立即获取位置(经纬度)?使用我现在拥有的代码,当用户第一次启用位置服务时,我得到 0.0 的纬度和经度,之后它工作正常。所以我的问题是如何在用户启用定位服务后立即获得经纬度?

GPSTracker.java

public class GPSTracker implements LocationListener {

private Context context;
private double longitude;
private double latitude;
private LocationManager locationManager;
private Location location;
private boolean isGPSEnabled;
private boolean isNetworkEnabled;
private boolean canGetLocation;

public GPSTracker(Context context) {
    this.context = context;
    getLocation();
}

public Location getLocation() {
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (!PermissionsManager.getInstance(context).checkLocationPermission((Activity) context)) {
        PermissionsManager.getInstance(context).requestLocationPermissions((Activity) context);
    } else {
        if (!isGPSEnabled && !isNetworkEnabled) {

            canGetLocation = false;
        } else {
            canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, Constants.MIN_TIME_BETWEEN_UPDATES, Constants.MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    updateGPSCoordinates();
                }
            }

            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, Constants.MIN_TIME_BETWEEN_UPDATES, Constants.MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        updateGPSCoordinates();
                    }
                }
            }
        }
    }

    return location;
}

public void updateGPSCoordinates() {
    if (location != null) {
        longitude = location.getLongitude();
        latitude = location.getLatitude();
    }
}

public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }
    return latitude;
}

public double getLongitude() {
    if (location != null) {
        longitude = location.getLongitude();
    }
    return longitude;
}

public boolean canGetLocation() {
    return this.canGetLocation;
}

@Override
public void onLocationChanged(Location location) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

}

【问题讨论】:

    标签: android locationlistener location-services


    【解决方案1】:

    您可以通过 Broadcast Receiver 来实现这一点,以用于打开/关闭 gps 等提供程序更改。 按照以下代码实现。 在您的清单中将接收者标记如下

    <receiver android:name=".YourReceiverName">
    <intent-filter>
        <action android:name="android.location.PROVIDERS_CHANGED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    

    Receiver.java

        public class YourBroadcastReceiver extends BroadcastReceiver {        
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
                Toast.makeText(context, "in android.location.PROVIDERS_CHANGED",
                Toast.LENGTH_SHORT).show();
    
            }
        }
    }
    

    【讨论】:

    • 我已经实现了,也是一样,开启定位服务后第一次返回null
    • 你在使用 gps tracker 的 canGetLocation() 方法吗?
    • 不幸的是没有。我应该在哪里使用它?
    • 放置一个 if 条件并在其中调用该方法。如果它返回 true 而不是调用你的方法来获取位置
    猜你喜欢
    • 2018-10-03
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    • 2011-03-29
    • 2015-02-10
    • 2013-12-30
    相关资源
    最近更新 更多