我试图作出LocationUpdates收听者,并且当我在同时使用显影剂/机器人教程和方法requestLocationUpdates()用于与googleClientApi,locationRequest和“这个”作为第三个参数我发现在网络上的教程。
出于某种原因,这不作为参数。我在stackoverflow上尝试过类似问题的不同建议。
我已将imoport更改为
import android.location.LocationListener;
我已经实施了所有必要的方法
public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback,GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleApiClient mGoogleApiClient=null;
LocationRequest mLocationRequest = null;
private Location mLastLocation;
private GoogleMap mMap;
所有工作都有效,直到有时间在底部的startLocationUpdates中使用requestLocationUpdates-method
public void buildGoogleMapApi()
{
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
mGoogleApiClient.connect();
}
protected LocationRequest createLocationRequest(LocationRequest mLocationRequest) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
return mLocationRequest;
}
protected void startLocationUpdates() {
createLocationRequest(mLocationRequest);
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnected(Bundle connectionHint)
{
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED)
{
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
}
else
{
}
}
else
{
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
}
LatLng latLngPosition = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLngPosition).title("YOU ARE HERE!!"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLngPosition));
mMap.setMapType(mMap.MAP_TYPE_HYBRID);
startLocationUpdates();
}
我试图将此更改为getApplicationContext(),但它不起作用。
我已经学过很多教程,但到目前为止还没有任何工作。有人在我的代码中看到任何错误吗? 感谢