HELO, 我对应用程序有一个奇怪的问题 - 当我使用google的api作为用户位置时,gps从第二个开始并进入定位。但如果我使用mapbox,它需要15分钟才能完成它的工作,然后我必须关闭屏幕并再次打开屏幕底部显示gps徽标,然后再关闭再打开地点。什么可以做到这一点。我在网上到处搜索类似的问题,但我一无所获。我使用http://www.mapbox.com中描述的所有内容来制作它,问题仍然存在。 以下是主要活动的代码:
import android.arch.lifecycle.Lifecycle;
import android.location.Location;
import android.location.LocationManager;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.mapbox.android.core.location.LocationEngine;
import com.mapbox.android.core.location.LocationEngineListener;
import com.mapbox.android.core.location.LocationEnginePriority;
import com.mapbox.android.core.location.LocationEngineProvider;
import com.mapbox.android.core.permissions.PermissionsListener;
import com.mapbox.android.core.permissions.PermissionsManager;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.CameraMode;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
import java.util.List;
public class MbooooActivity extends AppCompatActivity implements OnMapReadyCallback,
LocationEngineListener, PermissionsListener{
private MapView mapView;
private MapboxMap mbm;
private PermissionsManager permissionsManager;
private LocationEngine locationEngine;
private LocationLayerPlugin locationLayerPlugin;
private Location lokation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this,getString(R.string.toke));
setContentView(R.layout.activity_mboooo);
mapView=(MapView)findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(MapboxMap mapboxMap) {
mbm=mapboxMap;
enableLocation();
}
private void enableLocation(){
if(PermissionsManager.areLocationPermissionsGranted(this)){
initialiseLocationEngine();
initialiseLocationLayer();
}else{
permissionsManager=new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}
@SuppressWarnings("MissingPermission")
private void initialiseLocationEngine(){
locationEngine=new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.activate();
Location prevlok=locationEngine.getLastLocation();
if(prevlok!=null){
lokation=prevlok;
setCamera(prevlok);
}else{
locationEngine.addLocationEngineListener(this);
}
}
@SuppressWarnings("MissingPermission")
private void initialiseLocationLayer(){
locationLayerPlugin=new LocationLayerPlugin(mapView,mbm,locationEngine);
locationLayerPlugin.setLocationLayerEnabled(true);
locationLayerPlugin.setCameraMode(CameraMode.TRACKING);
locationLayerPlugin.setRenderMode(RenderMode.NORMAL);
Lifecycle lifecycle=getLifecycle();
lifecycle.addObserver(locationLayerPlugin);
}
private void setCamera(Location location){
mbm.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),
location.getLongitude()),16.0));
}
@SuppressWarnings("MissingPermission")
@Override
public void onConnected() {
locationEngine.requestLocationUpdates();
}
@Override
public void onLocationChanged(Location location) {
if(location!=null){
lokation=location;
setCamera(lokation);}
}
@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {
}
@Override
public void onPermissionResult(boolean granted) {
if(granted){
enableLocation();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// super.onRequestPermissionsResult(requestCode, permissions, grantResults);
permissionsManager.onRequestPermissionsResult(requestCode,permissions,grantResults);
}
@SuppressWarnings("MissingPermission")
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
if(locationEngine!=null){
locationEngine.requestLocationUpdates();
locationEngine.addLocationEngineListener(this);}
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(locationEngine!=null){
locationEngine.removeLocationEngineListener(this);
locationEngine.removeLocationUpdates();}
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
locationEngine.removeLocationEngineListener(this);
locationEngine.removeLocationUpdates();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
locationEngine.removeLocationUpdates();
locationEngine.removeLocationEngineListener(this);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
答案 0 :(得分:1)
您应该独立于上一个位置收听位置更新:
private void initialiseLocationEngine(){
locationEngine=new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.activate();
locationEngine.addLocationEngineListener(this);
Location prevlok=locationEngine.getLastLocation();
if(prevlok!=null){
lokation=prevlok;
setCamera(prevlok);
}
}
此外,您可以设置:
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.setInterval(0);
locationEngine.setFastestInterval(1000);