Android位置返回null

时间:2013-12-05 17:53:16

标签: java android gps location

在尝试确定经度和纬度方面遇到一些麻烦,有时它会起作用,有时则不起作用。希望有人愿意看一看,我花了好几天试图解决这个问题。

在我的清单权限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)");
        }
    }

3 个答案:

答案 0 :(得分:1)

  

#Precisely访问位置的最佳方式是通过(新)融合位置提供商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

答案 1 :(得分:0)

前一段时间我也遇到了同样的问题...实际上如果GPS在设备上开启的话,LocationManager.GPS_PROVIDER会工作,否则这段代码不起作用,所以我决定在LocationManager.NETWORK_PROVIDER的帮助下获取位置。 .... 可能它会帮助你 http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

答案 2 :(得分:0)

Here我已经提供了获取位置的代码。代码将首先通过GPS检查位置。如果GPS不可用,它将使用您的网络提供商获取粗略位置。试着告诉我它是否有效。