简体   繁体   中英

latitude and longitude of current location in android?

I am trying to fetch my current location through WiFi from Android virtual device but when I debug this program it shows me Null for location in Passive Network provider in getLocation() .It gives me value for location manager. In this my gpsProvider , and NetworkProvider both return false value and provider is passive only.

 import java.util.List;

 import android.content.Context;
  import android.location.Criteria;
 import android.location.Location;
 import android.location.LocationListener;
 import android.location.LocationManager;
import android.os.Bundle;
 import android.support.v4.app.FragmentActivity;
import android.util.Log;

public class ShowLocation extends FragmentActivity 
{
 Context context;
 private LocationManager locationManager=null;
 private LocationListener locationListener=null;
 private Location location=null;
 double latitude,longitude;
 boolean gpsStatus=false,NetworkProvider=false,Status=false,Passiveprovider=false;
 private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

 // The minimum time between updates in milliseconds
  private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
 private Criteria criteria=null;
  String Provider=null;
 public void onCreate(Bundle state)
 {
    super.onCreate(state);
    setContentView(R.layout.fragment_main);
    context=getApplicationContext();
    if(Status=checkStatus())
    {

        getLocation();
    }
}

public boolean checkStatus()
{
    boolean status1=false;
    locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
    List<String> provider =locationManager.getAllProviders();
    System.out.println("Location Manager ="+provider);
    for(String providername : provider)
        {
            if(providername.equals(LocationManager.GPS_PROVIDER))
                {
                    gpsStatus=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                    status1=true;
                }
            if(providername.equals(LocationManager.NETWORK_PROVIDER))
                {
                    NetworkProvider=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                    status1=true;
                }
            if(providername.equals(LocationManager.PASSIVE_PROVIDER))
                {
                    Passiveprovider=locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER);
                    status1=true;
                }
        }


System.out.println("Network provider ="+NetworkProvider + "Gps Provider ="+gpsStatus + "PassiveProvider ="+Passiveprovider);

    return status1;


}
public void getLocation()
{
    locationListener=new LocationLis();
    if(gpsStatus)
    {
        System.out.println("Gps Provider = "+gpsStatus);
        Log.d("Gps is on","=" +gpsStatus);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
        criteria=new Criteria();
        Provider=locationManager.getBestProvider(criteria, true);
        if(locationManager !=null)
            {
                location=locationManager.getLastKnownLocation(Provider);
                if(location != null)
                    {
                        latitude=location.getLatitude();
                        longitude=location.getLongitude();
                        System.out.println("Gps Provider");
                        System.out.println(latitude +"  "+longitude);
                    }

            }
    }
     else if(NetworkProvider)
        {
         System.out.println("NetworkProvider"+NetworkProvider);
          Log.d("Network is on", "="+NetworkProvider);
          locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
          criteria=new Criteria();
          Provider=locationManager.getBestProvider(criteria, true);
          if(locationManager !=null)
            {
              location=locationManager.getLastKnownLocation(Provider);
              if(location !=null)
                {
                  latitude=location.getLatitude();
                  longitude=location.getLongitude();
                  System.out.println("Network Provider");
                  System.out.println(latitude +"  "+longitude);

                }
            }
        }
     else if(Passiveprovider)
        {

             System.out.println("PassiveProvider"+Passiveprovider);
              Log.d("Network is on", "="+Passiveprovider);
              locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,0,0,locationListener);
              criteria=new Criteria();
              Provider=locationManager.getBestProvider(criteria, true);
               // Here Provider is null

              if(locationManager !=null)
                {

                  location=locationManager.getLastKnownLocation(Provider);
                  if(location !=null)
                    {
                      latitude=location.getLatitude();
                      longitude=location.getLongitude();
                      System.out.println("Network Provider");
                      System.out.println(latitude +"  "+longitude);

                    }
                }
        }
     else
        {
            System.out.println("Provider r not available");
        }

}

private class LocationLis implements LocationListener {

    @Override
    public void onLocationChanged(Location location)
    {

    }

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

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


}

    }





}

Manifest.xml file

android:glEsVersion="0x00020000"
android:required="true" />

<uses-feature
    android:name="android.hardware.wifi"
    android:required="true" /> 

<permission
    android:name="com.Priyank.priyankcurrentlocation.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.Priyank.priyankcurrentlocation.MAPS_RECEIVE" />
<uses-permission  android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.INSTALL_LOCATION_PROVIDER" />
<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="I have key with me." />

getLastKnownLocation is allowed to return null. It means it hasn't recently figured out a location, or the location it has is so old it doesn't want to return it. You need to account for this possibility. That's one of the two reasons you need to be careful with getLastKnownLocation (the other being that even if it does return a value, it could be really wrong- as in hours old, there is no promise of freshness with it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM