利用android的辅助功能
定位服务
在移动设备上,定位几乎已经是一个必不可少的功能了。在Android中,设备可以通过GPS、移动通讯网络、WIFI网络来进行定位,这些定位功能都被分装在一个LocationManager对象中,我们可以这样来使用Android的定位服务:
public class AgsLocation extends Activity {
MapView map = null;
LocationManager loc = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map = (MapView) findViewById(R.id.map);
loc = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
loc.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
new LocationListener() {
public void onLocationChanged(Location l) {
Point ptLatLon = new Point(l.getLongitude(), l
.getLatitude());
SpatialReference sr4326 = SpatialReference.create(4326);
Point ptMap = (Point)GeometryEngine.project(ptLatLon, sr4326,
map.getSpatialReference());
map.centerAt(ptMap);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
});
}
}
在代码中我选择了使用GPS定位,注意,要使用Android系统的定位服务(LOCATION_SERVICE),在Android清单文件中一定要有相应的权限:
<manifest ... >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
...
</manifest>
从Android定位服务中获取到的是经纬度信息,我还需要将这个坐标投影到地图上,这里GeometryEngine又派上用场了。好了,现在让我们用Emulator Control来模拟一个GPS信号发送到模拟器:
这是北京的大致位置,在上面的代码中我们希望地图可以以这个坐标为中心点来进行居中,实际的情况与这个预期是一致的:
图 37 模拟GPS定位到北京附近
II. 附录
在线资源
ArcGIS for Android(Resource Center):
http://help.arcgis.com/en/arcgismobile/10.0/apis/android/help/
ArcGIS Android API参考:
http://help.arcgis.com/en/arcgismobile/10.0/apis/android/api/index.html
本文在线地址: