Android的LocationManager,获取GPS位置,如果没有GPS然后让网络提供商的位置 [英] Android LocationManager, Get GPS location ,if no GPS then get Network Provider location

查看:806
本文介绍了Android的LocationManager,获取GPS位置,如果没有GPS然后让网络提供商的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是下面的code获得位置:

I am using the below code to get locations:

public Location getLocation() {
        try {
            mLocationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

            // getting GPS status
            boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            boolean isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                // First get location from Network Provider
                if (isNetworkEnabled) {
                    mLocationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER,  MIN_TIME_BW_UPDATES,  MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (mLocationManager != null) {
                        location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            lat = location.getLatitude();
                            lng = location.getLongitude();
                        }
                    }
                }
                //get the location by gps
                if (isGPSEnabled) {
                    if (location == null) {
                        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (mLocationManager != null) {location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                lat = location.getLatitude();
                                lng = location.getLongitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

据工作正常,但我想先获得GPS位置,并在情况下,如果它是不可用的,locationmanager应该查询网络服务提供者,在我收到的麻烦。

It is working properly, but I would like to get GPS location first, and in case if it is unavailable , locationmanager should query for Network provider, in which I am getting trouble.

请,我推荐的好办法做到这一点。

Please, recommend me the good way to do this.

推荐答案

你是说,你第一次如果需要提供GPS位置,但你做了什么,首先你是从网络提供商获得的位置,然后从GPS。这将让位置,从网络和GPS,以及如果两者都可用。你可以做的是,写这些案件中的 的if..else如果 块。类似 -

You're saying that you need GPS location first if its available, but what you did is first you're getting location from network provider and then from GPS. This will get location from Network and GPS as well if both are available. What you can do is, write these cases in if..else if block. Similar to-

if( !isGPSEnabled && !isNetworkEnabled) {

// Can't get location by any way

} else {

    if(isGPSEnabled) {

    // get location from GPS

    } else if(isNetworkEnabled) {

    // get location from Network Provider

    }
}

所以这将获取的GPS位置第一(如果可用),否则它会尝试从网络提供商获取的位置。

So this will fetch location from GPS first (if available), else it will try to fetch location from Network Provider.

修改

要变得更好,我会发布一个片段。考虑它是在 的try-catch

To make it better, I'll post a snippet. Consider it is in try-catch:

boolean gps_enabled = false;
boolean network_enabled = false;

LocationManager lm = (LocationManager) mCtx
                .getSystemService(Context.LOCATION_SERVICE);

gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

Location net_loc = null, gps_loc = null, finalLoc = null;

if (gps_enabled)
    gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
    net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (gps_loc != null && net_loc != null) {

    if (gps_loc.getAccuracy() >= net_loc.getAccuracy())
        finalLoc = gps_loc;
    else
        finalLoc = net_loc;

        // I used this just to get an idea (if both avail, its upto you which you want to take as I taken location with more accuracy)

} else {

    if (gps_loc != null) {
        finalLoc = net_loc;
    } else if (net_loc != null) {
        finalLoc = gps_loc;
    }
}

现在你检查 finalLoc ,如果不是则返回它。 你可以写上面的code在函数返回所需的( finalLoc )的位置。我想,这可能帮助。

Now you check finalLoc for null, if not then return it. You can write above code in a function which returns the desired (finalLoc) location. I think this might help.

这篇关于Android的LocationManager,获取GPS位置,如果没有GPS然后让网络提供商的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆