ArcGiS for Android 10.6定位功能
你好! 我最近在做一个基于ArcGIS for
Android展示地层信息的本科毕业设计,将其SDK中的一些基本功能在这里笔记一下,便于后面使用,同时希望其他人少走弯路。全部都是从网上学习或者自己摸索的一些东西,如果有错误希望评论出来,我们一起交流学习。
首先要在项目中的AndroidManif.xml文件中添加定位权限,Android定位根据定位的经度分为三个等级,下面就是三种定位权限的名字
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arcgis">
<!--添加-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
后面代码中使用时还要确认用户是否开启了定位服务,如果没有开启转到定位服务的设置界面
//LocationManager是Android原生的定位管理
LocationManager locationManager= (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//如果没有权限打开设置界面
if (!(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
|| locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))) {
Toast.makeText(this, "请打开网络或GPS定位功能!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0);
return;
}
还要检擦用户是否给了应用程序定位的权限,如果没有权限就重新申请获取权限。
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPerission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, 100);
}
使用LocationManager获取位置并显示到地图上:
通过LocationManager获取位置
//LocationProvider有三种LocationManager.PASSIVE_PROVIDER、
//LocationManager.GPS_PROVIDER、LocationManager.NETWORK_PROVIDER
String provider;
provider=LocationManager.NETWORK_PROVIDER;
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//避免获取不到位置都试一下
if (location == null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
provider=LocationManager.GPS_PROVIDER;
}
Point locat=new Point(location.getLongitude(),location.getLatitude());
初始化一个LocationListener
LocationListener locationListener = new LocationListener() {
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
// Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {
}
// Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {
}
//当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
@Override
public void onLocationChanged(Location location) {
//移除原来的图形
mScecneView.getGraphicsOverlays().clear();
//添加新的图形
Point locat=new Point(location.getLongitude(),location.getLatitude());
Point pointGeometry = locat;
SimpleMarkerSymbol pointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 10);
Graphic pointGraphic = new Graphic(pointGeometry);
GraphicsOverlay pointGraphicOverlay = new GraphicsOverlay();
SimpleRenderer pointRenderer = new SimpleRenderer(pointSymbol);
pointGraphicOverlay.setRenderer(pointRenderer);
pointGraphicOverlay.getGraphics().add(pointGraphic);
mScecneView.getGraphicsOverlays().add(pointGraphicOverlay);
}
};
并监听位置的变化
locationManager.requestLocationUpdates(provider, 1, 1, locationListener);
使用ArcGIS的LocationDisplay显示位置
ArcGIS的mapview控件下有一个mMapView.getLocationDisplay()能够实现定位功能,并且能在mapview控件中显示出来你所在的位置。不用上面的那么长代码:
//定义LocationDisplay对象
LocationDisplay locationDisplay;
int requestCode = 2;
//定位所需权限
String[] reqPermissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
//根据mapview对象获取locationDisplay对象
locationDisplay = mMapView.getLocationDisplay();
//设置定位模式
locationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.RECENTER );
locationDisplay.startAsync();
//监听位置的变化
locationDisplay.addDataSourceStatusChangedListener(new LocationDisplay.DataSourceStatusChangedListener() {
@Override
public void onStatusChanged(LocationDisplay.DataSourceStatusChangedEvent dataSourceStatusChangedEvent) {
if (dataSourceStatusChangedEvent.isStarted())
return;
if (dataSourceStatusChangedEvent.getError() == null)
return;
}
});