gpt4 book ai didi

Android Studio 无法解析方法 requestLocationUpdates FusedLocationProviderAPI

转载 作者:行者123 更新时间:2023-11-30 00:55:29 25 4
gpt4 key购买 nike

除此行外一切正常:LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

它抛出一个 cannot resolve method requestLocationUpdates 错误。我在文档中没有看到任何内容,但我是 Android 的新手,所以可能遗漏了一些内容。

这是完整的代码。我哪里出错了?谢谢!

package com.example.alham.location;

import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener{

private final String LOG_TAG ="LaurenceTestApp";
private TextView txtOutput;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;

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

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
txtOutput = (TextView) findViewById(R.id.txtOutput);
}

@Override
protected void onStart(){
super.onStart();
mGoogleApiClient.connect();
}

@Override
protected void onStop(){
mGoogleApiClient.disconnect();
super.onStop();
}

@Override
public void onConnected(Bundle bundle){
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(1000);

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}

@Override
public void onLocationChanged(Location location){
Log.i(LOG_TAG,location.toString());
txtOutput.setText(Double.toString(location.getLatitude()));
}

@Override
public void onConnectionSuspended(int i){
Log.i(LOG_TAG, "GoogleApiClient connection has been suspended");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult){
Log.i(LOG_TAG, "GoogleApiClient connection has failed");
}
}

最佳答案

在 list 中添加:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

这在代码中:

    LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Here you have the updated location
}
});
}

关于Android Studio 无法解析方法 requestLocationUpdates FusedLocationProviderAPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40241241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com