android中当前位置的经纬度?

时间:2014-04-19 08:21:35

标签: android

我正在尝试通过WiFi从Android虚拟设备获取当前位置,但是当我调试此程序时,它显示我在getLocation()中的被动网络提供程序中的位置为空。它给了我位置管理器的价值。在此我的gpsProviderNetworkProvider都返回false值,提供者只是被动。

 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");
        }

}

私有类LocationLis实现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档案

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." />

1 个答案:

答案 0 :(得分:0)

允许getLastKnownLocation返回null。这意味着它最近没有找到一个位置,或者它所拥有的位置太旧了它不想返回它。你需要考虑到这种可能性。这是你需要小心使用getLastKnownLocation的两个原因之一(另一个原因是,即使它确实返回了一个值,它也可能是错误的 - 就像几个小时一样,没有新鲜的承诺)。

相关问题