简体   繁体   English

Android当前位置的经纬度?

[英]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. 我试图通过Android虚拟设备通过WiFi获取当前位置,但是当我调试该程序时,它在getLocation()中显示被动网络提供程序中的位置为Null,这为位置管理器提供了价值。 In this my gpsProvider , and NetworkProvider both return false value and provider is passive only. 在此我的gpsProviderNetworkProvider都返回假值,并且provider仅是被动的。

 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 { 私有类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 file 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." />

getLastKnownLocation is allowed to return null. getLastKnownLocation允许返回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). 这是您需要小心使用getLastKnownLocation的两个原因之一(另一个原因是,即使它确实返回了一个值,也可能确实是错误的-如数小时之久,就没有新鲜的保证)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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