bla*_*awk 25 java android google-maps-android-api-2 android-fusedlocation
我想定期(比如每2分钟)当前位置更新为这个我下面的官方文档,我写了这个代码,但它不会给当前的位置即使在我传递requestLocationUpdates LocationRequest对象指定每两分钟更新( ),这里是代码:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private FusedLocationProviderClient FusedLocationClient;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
SupportMapFragment map =
getSupportFragmentManager().findFragmentById(R.id.map));
map.getMapAsync(this);
FusedLocationClient LocationServices.getFusedLocationProviderClient(this);
}
@Override
public void onConnected(Bundle bundle) {
FusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
Log.i("MainActivity ", "" + location.getLongitude())
}
}
});
FusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
FusedLocationClient.requestLocationUpdates(requestLocation(),
new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
Log.i("MainActivity ", "" + location.getLongitude());
//not getting current location updates every 2 minutes
}
};
},null);
}
@Override
public void onConnectionSuspended(int i) {}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {}
Run Code Online (Sandbox Code Playgroud)
Dan*_*ent 69
这与我在这里的其他答案类似,更新后使用最近推出的FusedLocationProviderClient类.
要将FusedLocationProviderClient与Google Map结合使用,请执行以下操作:
等到谷歌地图准备好了
如果需要,在运行时请求位置权限
授予权限后请求位置更新
获取用户的位置后更新Google Map
首先要确保您至少使用版本11的Google Play服务,因为旧版本没有FusedLocationProviderClient类(较新的版本也可以使用):
dependencies {
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
//........
}
Run Code Online (Sandbox Code Playgroud)
请注意,FusedLocationProviderClient存在于版本11.0.2中,但由于初始实现中存在错误,建议您仅在11.6.0及更高版本上使用此类.从文档:
注意:建议使用版本11.6.0或更高版本的Google Play服务,其中包括此类的错误修复.
这是完整的Activity类:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Run Code Online (Sandbox Code Playgroud)
系统将提示用户接受"位置"权限:
该位置将在应用启动时更新,每两分钟更新一次:
Ash*_* VL -1
private LocationRequest locationRequest;
public class MapsActivity extends FragmentActivity implements LocationListener{
locationRequest = new LocationRequest();
locationRequest.setInterval(60 * 1000);
locationRequest.setFastestInterval(15 * 1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
Run Code Online (Sandbox Code Playgroud)
实施位置更改侦听器,您将能够覆盖 onlocation 更改...
| 归档时间: |
|
| 查看次数: |
41282 次 |
| 最近记录: |