簡體   English   中英

Android當前位置的經緯度?

[英]latitude and longitude of current location in android?

我試圖通過Android虛擬設備通過WiFi獲取當前位置,但是當我調試該程序時,它在getLocation()中顯示被動網絡提供程序中的位置為Null,這為位置管理器提供了價值。 在此我的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");
        }

}

私有類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." />

getLastKnownLocation允許返回null。 這意味着它最近還沒有找到位置,或者它的位置太舊了,不想返回它。 您需要考慮這種可能性。 這是您需要小心使用getLastKnownLocation的兩個原因之一(另一個原因是,即使它確實返回了一個值,也可能確實是錯誤的-如數小時之久,就沒有新鮮的保證)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM