其他分享
首页 > 其他分享> > android-无法使用Google的融合位置API获取位置

android-无法使用Google的融合位置API获取位置

作者:互联网

我正在尝试使用Google的融合位置API来获取位置.为此,我创建了两个类.一个是MainActivity,第二个是FusedLocationService,其中MainActivity是主类.但是我的经度和纬度为0.0.所以请帮帮我.

这是我的MainActivity代码:

public class MainActivity extends AppCompatActivity {

TextView tvLocation;
FusedLocationService fusedLocationService;
double latitude;
double longitude;

String locationResult = "";
Location location;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvLocation = (TextView) findViewById(R.id.tvLocation);
    fusedLocationService = new FusedLocationService(this);
    location = fusedLocationService.getLocation();

    if (null != location) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        locationResult = "Latitude: " + latitude + "\n" +
                "Longitude: " + longitude + "\n";
    } else {
        Timber.e("-error-%s", "Location Not Available!");
        locationResult = "Location Not Available!";
    }

    Log.e("Lati ", String.valueOf(latitude));
    Log.e("longi ", String.valueOf(longitude));
    tvLocation.setText(locationResult);
   }
 }

这是我的FusedLocationService类:

public class FusedLocationService implements LocationListener, GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

private static final long INTERVAL = 1000 * 30; //30sec
private static final long FASTEST_INTERVAL = 1000 * 5; // 5sec

Activity mActivity;
public LocationRequest locationRequest;
public GoogleApiClient googleApiClient;
public Location location;
public FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;

public FusedLocationService(Activity activity) {

    Timber.plant(new Timber.DebugTree());
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(INTERVAL);
    locationRequest.setFastestInterval(FASTEST_INTERVAL);
    mActivity = activity;
    googleApiClient = new GoogleApiClient.Builder(mActivity)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    if (googleApiClient != null) {
        googleApiClient.connect();
    }

}

@Override
public void onConnected(Bundle connectionHint) {
    Log.e("-onConnected-", "connected now");
    location = fusedLocationProviderApi.getLastLocation(googleApiClient);
    fusedLocationProviderApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}

@Override
public void onLocationChanged(Location location) {
}

public Location getLocation() {
    return location;
}

@Override
public void onConnectionSuspended(int i) {
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
  }
}

我已经在清单和gradle文件夹中的lib中包含了权限.
所以请帮忙.先感谢您

解决方法:

缺少对新位置的引用:

@Override
public void onLocationChanged(Location location) {
    this.location = location;
}

位置更新也是异步的.因此,您需要类似活动回调的内容.

编辑:

还要检查Google Play服务是否可用.您可以创建这样的方法:

public boolean checkPlayServices() {
   return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS;
}

查看Google documentation

标签:google-api,location,latitude-longitude,android
来源: https://codeday.me/bug/20191118/2030137.html