Android API中提供了获取位置信息的方法,可以获取GPS的经纬度,速度,高度等。
首先通过LocationManager类来获取设备有哪些相关的位置提供商。
下面为获取位置提供商的代码:
locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
List<String> providers = locationManager.getAllProviders();
ListAdapter listAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, providers);
listview.setAdapter(listAdapter);
常见的位置提供商一般是GPS或passive
下面以GPS为例,获取位置信息,如经纬度,速度,高度等。
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updatelocation(location);//更新位置信息的方法
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000,0, new LocationListener());