2016-10-01 55 views
1

我是Skobbler map中的新手。我使用Skobbler map来显示我的当前位置。最初,位于地图的某处。 enter image description here在android中启用GPS后显示当前位置

我已经在美利坚合众国设置了默认位置。所以,当我打开应用程序它显示这个。但是,这个标记仍然在地图上的某个地方,在海面之上。要显示的默认位置,我用这个:

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

enter image description here

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

回答

3

TL; DR:

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); 

不可正对Skobbler的专家,我不很喜欢的Skobbler SDK,因为它看起来太复杂,我和文档很差,太多的方法和类,所以我尽可能地利用Android SDK和Google API。

这不是一个生产就绪代码,但只是让你可以得到的图片。

这是我怎么样来组织我的代码,当涉及到的位置:

摘要位置活动从中你会延长所有使用位置的活动:

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); 
} 

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

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活动:

public class SkobblerActivity extends LocationActivity implements SKMapSurfaceListener { 

    private final static float ZoomLevel   = 15; 
    private final static int AnimationDuration = 750; 
    private final static float Accuracy   = 1; 

    private int     placedOnCurrentPosCount; 

    private SKMapViewHolder  mapHolder; 
    private SKMapSurfaceView mapView; 

    private SKPosition lastSKPosition = new SKPosition (new SKCoordinate()); 

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

    @Override 
    protected void onLocationSuccess (Location location) { 
     convertLocToSKLoc (location); 
     SKPositionerManager.getInstance().reportNewGPSPosition (lastSKPosition); 
     if (mapView != null) { 
      boolean center = false; 
      if (placedOnCurrentPosCount < 2 && (location.getLatitude() != 0 || location.getLongitude() != 0)) { 
       center = true; 
       mapView.centerOnCurrentPosition (ZoomLevel, true, AnimationDuration); 
      } 

      mapView.setPositionAsCurrent (lastSKPosition.getCoordinate(), Accuracy, center); 

      placedOnCurrentPosCount ++; 
     } 
    } 

    @Override 
    protected void onLocationFailure (int errorCode) { 
    } 

    private void convertLocToSKLoc (Location location) { 
     lastSKPosition.getCoordinate().setLatitude (location.getLatitude()); 
     lastSKPosition.getCoordinate().setLongitude (location.getLongitude()); 
    } 

    ....................... 
} 

从skobbler开发商的任何批评,以改善这是欢迎的。

+0

无法解析'centerOnCurrentPosition'方法。显示位置时有“放大”图标。我想要位置必须显示如上图所示。 –

+0

你使用什么版本的Skobbler Sdk? –

+0

这是最新的一个。我已修复该问题。但是,“相机”运动速度如此之快。当我从当前位置移动一段距离时,“摄像头”会更新得如此之快。它在5或6秒内从该距离返回到当前位置。我如何阻止?因为,我有一个工作按钮。由于该“摄像头”的自动更新,我的按钮意义不大。 –

1

设置FollowPositions为True:

mapView.getMapSettings().setFollowPositions(true); 

这将使它以便地图以每个位置更新重新居中。

+0

无法解析方法setFollowPositions ....此方法可能是这样的:'mapView.getMapSettings()。setCurrentPositionShown(true); '!!!你的回答不是我正在寻找的。请发表评论,而不是像这样。你的回答完全冲突了我的问题。 –

+0

@SananPandeya:我想你使用的是旧版本的SDK。 –