mike20132013
mike20132013

Reputation: 5425

Trying to get latitude and latitude with Network provider

I am trying to get my current coordinates with network provider and not gps provider.

I was able to figure out the solution for that but I am a bit confused with the concept in this scenario.

Working Code

Here's my code for getting my coordinates:

public void getLocation(){

        locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
        if(appUtils.isOnline()){

            try{

                Geocoder geocoder = new Geocoder(
                        MainActivity.this.getApplicationContext(),
                        Locale.getDefault());
                Location locationNetwork = locationManager
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                List<Address> list;

                if(locationNetwork!=null){

                    Toast.makeText(context,"Network Available!!",Toast.LENGTH_LONG).show();
                    list = geocoder.getFromLocation(locationNetwork.getLatitude(),locationNetwork.getLongitude(),3);

                    if(list!=null&&list.size()>0){

                        latitude = list.get(0).getLatitude();
                        longitude = list.get(0).getLongitude();

                        Toast.makeText(context,String.valueOf(latitude) + " (....) " + String.valueOf(longitude),Toast.LENGTH_LONG).show();

                        int count = 0;
                        while (latitude==null||longitude==null){

                            latitude = list.get(count).getLatitude();
                            longitude = list.get(count).getLongitude();
                            count++;
                            Toast.makeText(context,String.valueOf(latitude) + " --- " + String.valueOf(longitude),Toast.LENGTH_LONG).show();
                        }
                    }
                }else{

                    Toast.makeText(context,"No response!!",Toast.LENGTH_LONG).show();

                }
            }catch (IOException e){

                e.printStackTrace();
            }

        }else{

            Toast.makeText(context,"Server not responding",Toast.LENGTH_LONG).show();

        }

    }

This piece of code is working perfectly fine when the gps is enabled. If gps is disabled, it doesn't work.

Now, if we are setting the location to NETWORK_PROVIDER:

Location locationNetwork = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

Why do we still require gps ?

Now if I change it to PASSIVE PROVIDER:

Location locationNetwork = locationManager
                                .getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

It works fine with the passive provider. Why is it that ?

Can someone explain what is the major difference here and what would be the right way to get the coordinates with network provider ?

I know this question is been asked several times and I did went through it. I just want to get cleared with this concept.

Thank's in advance.. :)

Upvotes: 1

Views: 548

Answers (2)

venom2124
venom2124

Reputation: 168

This is the bit of code that I use to quickly get the current location, by checking all available network options.

private double[] getGPS(){
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
    List<String> providers = lm.getProviders(true);

    /* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
    Location l = null;

    for (int i=providers.size()-1; i>=0; i--) {
            l = lm.getLastKnownLocation(providers.get(i));
            if (l != null) break;
    }

    double[] gps = new double[2];
    if (l != null) {
            gps[0] = l.getLatitude();
            gps[1] = l.getLongitude();
    }
    return gps;
}

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93559

It doesn't require GPS to use the network provider, I've done it many times. However, getLastKnowLocation may not return a value if either it has never had an app request updates for that provider, or if the last time that happened was too long ago. You cannot count on that function always returning non-NULL. If you want to ensure that you get a location, use requestSingleUpdate instead. This will always get you a location (assuming the provider you use is enabled), but may take some time- a result may not be immediately available.

(There is one other time that function may never return- if you use the GPS provider and it can't get a lock on enough sattelites to find a location. Such as if you're in an underground parking garage).

Upvotes: 2

Related Questions