融合位置始终返回 null

Posted

技术标签:

【中文标题】融合位置始终返回 null【英文标题】:Fused Location always returns null 【发布时间】:2017-06-22 14:58:51 【问题描述】:

我正在使用 Fused Location 获取设备的当前位置,但我实现的代码始终返回 NULL。我仔细检查了我的设备位置是否已打开并设置为高精度模式。请告诉我的代码有什么问题?

我的代码:

import android.app.Activity;
import android.content.Context;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsStatusCodes;



public class FusedLocationTest implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener 

    private static Location mLastLocation;
    private static GoogleApiClient mGoogleApiClient;
    private static Context context;


    public Location getCurrentLocation(Context context) 
        this.context = context;
        buildGoogleApiClient();
        mGoogleApiClient.connect();
        if ((ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) 
        
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        return mLastLocation;
    


    /*connect to fused location provider*/
    synchronized void buildGoogleApiClient() 
        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    


    @Override
    public void onConnected(Bundle bundle) 

    


    @Override
    public void onLocationChanged(Location location) 

    


    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) 
        buildGoogleApiClient();
    


    @Override
    public void onConnectionSuspended(int i) 

    


点击按钮调用:

@Override
    public void onClick(View v) 
        Location l = new FusedLocationTest().getCurrentLocation(this);
        if(l!=null)
            Toast.makeText(this,l.getLatitude()+", "+l.getLongitude(), Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this,"NULL LOCATION", Toast.LENGTH_LONG).show();
    

【问题讨论】:

【参考方案1】:

将此代码添加到您的 onConnected 中,这就是它获取 Last Known Location 的位置。

private static Location mLastLocation;
private static GoogleApiClient mGoogleApiClient;
private static Context context;

// added
private LocationRequest mLocationRequest; 
private Double latitude;
private Double longitude;
private String TAG = ""; // set your TAG

@Override
public void onConnected(Bundle bundle) 

    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
        return;
    
    startLocationUpdates();
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if(mLastLocation == null)
        startLocationUpdates();
    

    if (mLastLocation != null) 
        latitude = mLocation.getLatitude();
        longitude = mLocation.getLongitude();

        // set your tag
        Log.d(TAG, String.valueOf(latitude));
        Log.d(TAG, String.valueOf(longitude));

     else 
        Toast.makeText(context, "Location not Detected, Did you turn off your location?", Toast.LENGTH_SHORT).show();
    


protected void startLocationUpdates() 
    // Create the location request
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(30 * 1000)
            .setFastestInterval(5 * 1000);

    // Request location updates
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
        return;
    
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

并从您的方法中删除同步,只需将其公开即可。下面是这样的:

public void buildGoogleApiClient()  

【讨论】:

【参考方案2】:

mGoogleApiClient.connect() 函数是异步的。您必须等到onConnected,直到最后一个位置可用。在documentation 中有一个明显的例子。

【讨论】:

以上是关于融合位置始终返回 null的主要内容,如果未能解决你的问题,请参考以下文章

融合位置提供者:getLastKnowLocation 返回 null 并且 onLocationChange 没有被调用?

GPS 位置始终返回 null

使用PendingIntent获取位置始终返回Null

FusedLocationProvider 始终返回 null Location 对象

GetLastLocation 始终返回 null

StreetViewPanorama.getLocation 始终返回 null。