在android中启用GPS后显示当前位置

时间:2016-10-01 17:53:51

标签: android gps skmaps

我是Skobbler map的新人。我使用Skobbler map显示我当前的位置。最初marker位于地图中的某个位置。 enter image description here

我在美利坚合众国设置了默认位置。因此,当我打开应用程序时,它显示了这一点。但是,标记仍然在地图中的某个地方,在海面之上。为了显示默认位置,我使用了这个:

SKCoordinateRegion region = new SKCoordinateRegion();
region.setCenter(new SKCoordinate(-97.1867366, 38.4488163));
region.setZoomLevel(5);
mapView.changeMapVisibleRegion(region, true);

enter image description here

之后,当我启用GPS时,标记必须移动并在时间内显示我当前的位置。可以做些什么来刷新地图以显示我当前的位置。可以做些什么来解决这个问题?

2 个答案:

答案 0 :(得分:2)

TL; DR:

homeService.$inject = ['dataService', 'localDataService'];

function homeService(dataService, localDataService){ 
  ...
}

不是Skobbler的专家,我不太喜欢Skobbler SDK,因为它对我来说太复杂了,而且文档很差,方法和类太多,所以我尝试利用Android SDK和Google API尽我所能。

这不是生产就绪的代码,只是为了让你能够得到图片。

这就是我喜欢在位置上组织代码的方式:

抽象位置活动,您可以从中扩展所有使用位置的活动:

SKCoordinate coordinates = new SKCoordinate (-97.1867366, 38.4488163)
SKPosition lastSKPosition  = new SKPosition (coordinates);
SKPositionerManager.getInstance ().reportNewGPSPosition (lastSKPosition);
mapView.centerOnCurrentPosition (ZoomLevel, true, AnimationDuration);
mapView.setPositionAsCurrent (lastSKPosition.getCoordinate (), Accuracy, center);

提供地理处理方法的GeoLoc服务:

public abstract class LocationActivity extends AppCompatActivity {

    private GeoLocService locationService;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);

        if (GeoLocService.checkLocationPerms (this)) {
            initLocService ();
        }
    }

    @Override
    protected void onStart () {
        super.onStart ();
        if (locationService != null) {
            locationService.onStart ();
        }
    }

    @Override
    public void onRequestPermissionsResult (int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult (requestCode, permissions, grantResults);
        if (requestCode == 1000) {
            if (GeoLocService.checkLocationPerms (this)) {
                initLocService ();
                locationService.onStart ();
            }
        }
    }

    @Override
    protected void onResume () {
        super.onResume ();
        if (locationService != null) {
            locationService.onResume ();
        }
    }

    @Override
    protected void onPause () {
        super.onPause ();
        if (locationService != null) {
            locationService.onPause ();
        }
    }

    @Override
    protected void onStop () {
        if (locationService != null) {
            locationService.disconnect ();
        }
        super.onStop ();
    }

    private void initLocService () {
        GeoLocService.LocationResponse response = new GeoLocService.LocationResponse () {

            @Override
            public void onLocation (Location location) {
                onLocationSuccess (location);
            }

            @Override
            public void onFailure (int errorCode) {
                onLocationFailure (errorCode);
            }
        };
        locationService = new GeoLocService (this, response);
    }

    protected void stopFetchingLocations () {
        if (locationService != null) {
            locationService.stopLocationUpdates ();
            locationService.disconnect ();
        }
    }

    protected GeoLocService getLocationService () {
        return locationService;
    }

    protected abstract void onLocationSuccess (Location location);
    protected abstract void onLocationFailure (int errorCode);
}

最后你的Skobbler活动:

public class GeoLocService implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {

    public final static long    UPDATE_INTERVAL_IN_MILLISECONDS = 10000;

    public final static long    FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;

    private GoogleApiClient     googleApiClient;
    private LocationRequest     locationRequest;
    private LocationResponse    locationResponse;
    private Location            currentLocation;

    private Date                lastUpdateTime;

    public GeoLocService (Activity context, LocationResponse locationResponse) {
        this.locationResponse   = locationResponse;

        googleApiClient         = new GoogleApiClient.Builder (context)
            .addConnectionCallbacks (this)
            .addOnConnectionFailedListener (this)
            .addApi (LocationServices.API)
            .build ();

        createLocationRequest ();
    }

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

    public void onResume () {
        if (googleApiClient != null && googleApiClient.isConnected ()) {
            startLocationUpdates ();
        }
    }

    public void onPause () {
        if (googleApiClient != null && googleApiClient.isConnected ()) {
            stopLocationUpdates ();
        }
    }

    public void disconnect () {
        if (googleApiClient != null) {
            googleApiClient.disconnect ();
        }
    }

    protected void createLocationRequest () {
        locationRequest = new LocationRequest ();
        locationRequest.setInterval (UPDATE_INTERVAL_IN_MILLISECONDS);
        locationRequest.setFastestInterval (FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        locationRequest.setPriority (LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    public void startLocationUpdates () {
        LocationServices.FusedLocationApi.requestLocationUpdates (googleApiClient, locationRequest, this);
    }

    public void stopLocationUpdates() { 
        LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        if (currentLocation == null) {
            currentLocation = LocationServices.FusedLocationApi.getLastLocation (googleApiClient);
            lastUpdateTime  = Calendar.getInstance ().getTime ();
            sendUpdates ();
        }

        startLocationUpdates ();
    }

    private void sendUpdates () {
        if (locationResponse != null && currentLocation != null) {
            locationResponse.onLocation (currentLocation);
        }
    }

    @Override
    public void onLocationChanged (Location location) {
        currentLocation = location;
        lastUpdateTime  = Calendar.getInstance ().getTime ();
        sendUpdates ();
    }

    @Override
    public void onConnectionSuspended (int cause) {
        googleApiClient.connect ();
    }

    @Override
    public void onConnectionFailed (ConnectionResult result) {
        if (locationResponse != null) {
            locationResponse.onFailure (result.getErrorCode ());
        }
    }

    public static boolean checkLocationPerms (Activity context) {
        if (ActivityCompat.checkSelfPermission (context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission (context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions (
                    context,
                    new String [] {
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_NETWORK_STATE
                    },
                    1000
            );
            return false;
        }
        return true;
    }

    public GoogleApiClient getGoogleApiClient () {
        return googleApiClient;
    }

    public Date getLastUpdatedTime () {
        return lastUpdateTime;
    }

    public interface LocationResponse {
        void onLocation (Location location);
        void onFailure (int errorCode);
    }
}

欢迎来自skobbler开发者的任何批评者改进这一点:)

答案 1 :(得分:1)

将FollowPositions设置为True:

mapView.getMapSettings().setFollowPositions(true);

这样可以使地图重新定位每个位置更新。