繁体   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