Android Location Manager, Get GPS location ,if no GPS then get to Network Provider location

前端 未结 7 2206
臣服心动
臣服心动 2020-11-29 20:52

I am using this given below code to get locations:

public Location getLocation() {
        try {
            mLocationManager = (LocationManager) context.get         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 20:56

    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.

提交回复
热议问题