簡體   English   中英

Android Location返回null

[英]Android Location returning null

嘗試確定經度和緯度時遇到一些麻煩,有時可以,有時則不行。 希望有人願意看一看,我已經花了很多天試圖弄清楚這一點。

在我的清單權限中擁有此權限ACCESS_FINE_LOCATION

 private void comenzarLocalizacion()
    {

        locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Location loc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        mostrarPosicion(loc);

        locListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                mostrarPosicion(location);
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }

        };

        locManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 30000, 0, locListener);
    }

    private void mostrarPosicion(Location loc) {
        if(loc != null)
        {
            lblLatitud.setText("Latitud: " + String.valueOf(loc.getLatitude()));
            lblLongitud.setText("Longitud: " + String.valueOf(loc.getLongitude()));
            lblPrecision.setText("Precision: " + String.valueOf(loc.getAccuracy()));
            Log.i("", String.valueOf(loc.getLatitude() + " - " + String.valueOf(loc.getLongitude())));


        }
        else
        {
            lblLatitud.setText("Latitud: (cannot be found)");
            lblLongitud.setText("Longitud: (cannot be found)");
            lblPrecision.setText("Precision: (cannot be found)");
        }
    }

#准確地,訪問位置信息的最佳方法是通過(新的)融合位置提供者API

import android.app.IntentService;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;

public class Locations extends IntentService implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {


    public Locations() {
        super("Locations");
        Log.d("Locations", "Location service started... ");
    }

    private LocationRequest locationRequest;
    private LocationClient locationClient;
    private Location location;

    @Override
    protected void onHandleIntent(Intent intent) {
        locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationClient = new LocationClient(this, this, this);
        locationClient.connect();
    }

    @Override
    public void onLocationChanged(Location l) {
     // do something on Location change.
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {

    }

    @Override
    public void onConnected(Bundle arg0) {
        Log.w("Locations", "Location client connected...");

            // get last location
        location = locationClient.getLastLocation();
        Log.w("Locations", "Latitude : "+location.getLatitude() + "");
        Log.w("Locations", "Longitude : "+location.getLongitude() + "");
    }

    @Override
    public void onDestroy() {
        if (locationClient.isConnected()) {
            locationClient.removeLocationUpdates(this);
        }
        locationClient.disconnect();
    }

    @Override
    public void onDisconnected() {
    }

}

來源 https://developer.android.com/training/location/index.html

我前段時間也遇到了同樣的問題...如果GPS前段時間在設備中開啟,則LocationManager.GPS_PROVIDER實際上可以工作,否則此代碼無法正常工作,因此我決定借助LocationManager.NETWORK_PROVIDER來獲取位置。可能會幫助您http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

在這里,我給出了獲取位置的代碼。 該代碼將首先通過GPS檢查位置。 如果GPS不可用,它將使用您的網絡提供商獲取大概位置。 嘗試讓我知道是否可行。

暫無
暫無

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

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