Android位置管理器,获取GPS位置,如果没有GPS然后到达网络提供商位置 [英] Android Location Manager, Get GPS location ,if no GPS then get to Network Provider location

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

问题描述

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

//获取GPS状态
boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

//获取网络状态
布尔isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

如果(!isGPSEnabled&&!isNetworkEnabled){
//没有启用网络提供商
}其他{
//首先从网络提供商获取位置
if(isNetworkEnabled){
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
Log.d(网络,网络);
if(mLocationManager!= null){
location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!= null){
lat = location.getLatitude();
ng = location.getLongitude();


$ b //通过gps获取位置
if(isGPSEnabled){
if(location == null){
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
Log.d(已启用GPS,已启用GPS);
if(mLocationManager!= null){location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location!= null){
lat = location.getLatitude();
ng = location.getLongitude();





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

返回地点;
}

工作正常,但我想首先获得GPS位置,如果它不可用,位置管理员应该查询网络提供商,我遇到了麻烦。



请推荐我这样做的好方法。如果可用的话,你首先需要GPS定位,但是你所做的首先是从网络获取定位信息提供者,然后从GPS。如果两者都可用,这将从网络和GPS获得位置。你可以做的是,将这些案例写入 if if else if 块。类似于 - $ / $>

  if(!isGPSEnabled&&!isNetworkEnabled){

// Can '以任何方式获取位置

} else {

if(isGPSEnabled){

//从GPS获取位置

} else if(isNetworkEnabled){

//从网络提供商获取位置



code $>

所以这将首先从GPS获取位置(如果有的话),否则它将尝试从网络提供商处获取位置。



编辑



为了让它更好,我将发布一段代码。考虑它在 try-catch

 布尔值gps_enabled = false; 
布尔值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);

位置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); (gps_loc!= null&& net_loc!= null){

//数字越小,结果越精确
if(gps_loc.getAccuracy ()> net_loc.getAccuracy())
finalLoc = net_loc;
else
finalLoc = gps_loc;

//我用这个只是为了得到一个想法(如果两者都有效,则由于我更准确地获取位置而取决于您)

} else {

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




$ b $ p
$ b现在你检查 finalLoc null ,如果不是那么 return 它。
您可以在返回所需( finalLoc )位置的函数中编写上面的代码。我认为这可能有帮助。


I am using this given 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;
    }

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

Please, recommend me the good way to do this.

解决方案

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

    }
}

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

EDIT:

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) {

    //smaller the number more accurate result will
    if (gps_loc.getAccuracy() > net_loc.getAccuracy()) 
        finalLoc = net_loc;
    else
        finalLoc = gps_loc;

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

} else {

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

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位置管理器,获取GPS位置,如果没有GPS然后到达网络提供商位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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