在android中启用GPS后显示当前位置
我是新来的在android中启用GPS后显示当前位置,android,gps,skmaps,Android,Gps,Skmaps,我是新来的Skobbler地图。我使用Skobbler地图显示我的当前位置。最初,标记位于地图的某个位置。 我已将默认位置设置为美利坚合众国。因此,当我打开应用程序时,它会显示以下内容。但是,这个标记仍然在地图的某个地方,在海面上。为了显示默认位置,我使用了以下方法: SKCoordinateRegion region = new SKCoordinateRegion(); region.setCenter(new SKCoordinate(-97.1867366, 38.4488163));
Skobbler地图
。我使用Skobbler地图
显示我的当前位置。最初,标记
位于地图的某个位置。
我已将默认位置设置为美利坚合众国。因此,当我打开应用程序时,它会显示以下内容。但是,这个标记仍然在地图的某个地方,在海面上。为了显示默认位置,我使用了以下方法:
SKCoordinateRegion region = new SKCoordinateRegion();
region.setCenter(new SKCoordinate(-97.1867366, 38.4488163));
region.setZoomLevel(5);
mapView.changeMapVisibleRegion(region, true);
之后,当我启用GPS
时,标记器必须移动并在时间内显示我的当前位置。如何刷新地图以显示我的当前位置。如何解决这个问题 TL;博士:
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);
}
}
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活动:
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);
}
}
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 ());
}
.......................
}
公共类SkobblePractivity扩展LocationActivity实现SKMapSurfaceListener{
专用最终静态浮动ZoomLevel=15;
私有最终静态int AnimationDuration=750;
专用最终静态浮动精度=1;
私有int placedOnCurrentPosCount;
私人SKMapViewHolder地图持有人;
专用SKMapSurfaceView地图视图;
私有SKPosition lastSKPosition=新SKPosition(新SKCoordinate());
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
.....
}
@凌驾
受保护的void onLocationSuccess(位置){
convertLocToSKLoc(位置);
SKPositionerManager.getInstance().reportNewGPSPosition(lastSKPosition);
如果(地图视图!=null){
布尔中心=假;
if(placedOnCurrentPosCount<2&(location.getLatitude()!=0 | | location.getLatitude()!=0)){
中心=真;
mapView.centerOnCurrentPosition(ZoomLevel、true、AnimationDuration);
}
mapView.setPositionAsCurrent(lastSKPosition.getCoordinate(),精度,中心);
placedOnCurrentPosCount++;
}
}
@凌驾
受保护的void onLocationFailure(内部错误代码){
}
专用void convertLocToSKLoc(位置){
lastSKPosition.getCoordinate().setLatitude(location.getLatitude());
lastSKPosition.getCoordinate().setLongitude(location.getLongitude());
}
.......................
}
欢迎来自skobbler开发者的任何批评人士对此进行改进:)将以下位置设置为真:
mapView.getMapSettings().setFollowPositions(true);
这将使地图在每次位置更新时重新居中。无法解析方法
centerOnCurrentPosition
。显示位置时,会显示exterme放大。我希望位置必须如上图所示显示。您使用的是什么版本的Skobbler Sdk?这是最新版本。我已经解决了放大问题。但是,摄像机
的移动速度太快了。当我从当前位置移动一段距离时,相机
更新得非常快。它会在5或6秒内从该距离返回到当前位置。我怎样才能阻止它?因为,我有一个按钮来完成这项工作。由于该相机的自动更新
,我的按钮似乎没有什么意义。通过if(mapView!=null)
如果(placedOnCurrentPosCount<1&(location.getLatitude()!=0 | | | location.getLongitude()!=0)){center=true;mapView.centerOnCurrentPosition更改中的代码(ZoomLevel,true,AnimationDuration);mapView.setPositionAsCurrent(lastSKPosition.getCoordinate(),精度,中心);}
是否有任何方法可用于此?它显示无法解析方法。无法解析方法setFollowPositions…此方法如下所示:mapView.getMapSettings().SetCurrentPositionShowed(true);
!!!你的答案不是我想要的。请在评论中发表,而不是像这样。你的答案与我的问题完全冲突。@SatanPandeya:我想你使用的是旧版本的SDK。