Android 定位GPS的使用
2016-02-21 15:18
597 查看
刚开始研究android本身的GPS定位,很多东西还不够清楚,只想将自己学到的进行记录。关于gps定位的测试可以使用Genymotion模拟器也可以使用真机测试,如果使用真机测试需要注意:Android的GPS定位只有在没有遮盖物的情况下才能获得Location!=null,否则获取不到经纬度信息。比如你在室内是获得不到位置信息的,另外这里只是介绍一下如何获取到经纬度信息,对于地理位置的解析由于无法获取google服务因此无法对地理位置进行解析,因此平时使用的定位基本都是百度地图或是高德地图的sdk。
获取LocationManager
要想使用Android自带的定位服务就要先获取这项服务也就是获得LocationManager[code]LocationManager mLocationManager = (LocationManager) getSystemService(mContext.LOCATION_SERVICE);
LocationProvider
所有种类的Provider
LocationProvider可以获取多种,下面的方法是获取所有的provider的方式。[code]/** * 显示定位的Provider的类型 */ private void showLocationType() { List<String> locationtype = new ArrayList<String>(); locationtype = mLocationManager.getAllProviders(); StringBuffer strb = new StringBuffer(); strb.append("LocationProvider类型:\n"); for (int i = 0; i < locationtype.size(); i++) { strb.append("类型" + i +":"+ locationtype.get(i) + "\n"); } mTextViewLocationType.setText(strb); }
筛选的Provider
对于LocationProvider的筛选是通过Criteria进行筛选的。[code]/** * 显示有选择的Provider的信息,通过Criteria进行过滤 */ private void showSelectedLocationProvider() { //获取各种类型的LocationProvider的方式,这里作为一种补充 // // 获取passive locationprovider // LocationProvider locattionprovider_passive = mLocationManager // .getProvider(LocationManager.PASSIVE_PROVIDER); // // 获取gps locationprovider // LocationProvider locattionprovider_gps = mLocationManager // .getProvider(LocationManager.GPS_PROVIDER); // // 获取network locationprovider // LocationProvider locattionprovider_network = mLocationManager // .getProvider(LocationManager.NETWORK_PROVIDER); // LocationProvider Criteria criteria = new Criteria(); // 设置LocationProvider是免费的 criteria.setCostAllowed(false); // 设置LocationProvider提供高度信息 criteria.setAltitudeRequired(true); // 设置LocationProvider提供方向信息 criteria.setBearingRequired(true); List<String> providerName = mLocationManager.getProviders(criteria, true); StringBuffer strb = new StringBuffer(); for (int i = 0; i < providerName.size(); i++) { strb.append("类型" + i + providerName.get(i) + "\n"); } mTextViewLocationSele.setText(strb); }
获取经纬度
为了进行实时定位需要对LocationManager进行监听,刚开始的时候由于LocationManager获得的location是上次的location而上次的location还未进行检测与监听因此可能出现为空的情况,不过LocationManager进行监听之后Location将不再为空。[code]// 进行定位的实时监听,每5秒或者每10米监听一次 mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 50, 10, this); if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ mLocation = mLocationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); }
具体位置信息由location获得。
[code]/** * 显示具体位置信息 * @param location */ private void showLocationInfo(Location location) { StringBuffer strb = new StringBuffer(); strb.append("位置信息:" + "\n"); if(location!=null){ strb.append("经度:" + location.getLongitude() + "\n"); strb.append("纬度:" + location.getLatitude() + "\n"); strb.append("高度:" + location.getAltitude() + "\n"); strb.append("速度:" + location.getSpeed() + "\n"); } mTextViewLocationInfo.setText(strb); }
测量两点距离
[code]// 显示两点距离 private void showDistanceBetweenTwoPoint() { final float[] results = new float[3]; Location.distanceBetween(30.333, 30.333, 40.444, 40.444, results); mTextViewLocationDist.setText(results[0] + "米"); }
完整代码
[code]public class MainActivity extends Activity implements LocationListener { private TextView mTextViewLocationType; private TextView mTextViewLocationInfo; private TextView mTextViewLocationSele; private TextView mTextViewLocationDist; private LocationManager mLocationManager; private Context mContext; private Location mLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextViewLocationType = (TextView) findViewById(R.id.textview_locationType); mTextViewLocationInfo = (TextView) findViewById(R.id.textview_locationInfo); mTextViewLocationSele = (TextView) findViewById(R.id.textview_locationSelected); mTextViewLocationDist = (TextView) findViewById(R.id.textview_locationdistance); mContext = getApplicationContext(); mLocationManager = (LocationManager) getSystemService(mContext.LOCATION_SERVICE); showLocationType(); showSelectedLocationProvider(); // 进行定位的实时监听,每5秒或者每10米监听一次 mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 50, 10, this); if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ mLocation = mLocationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); } showLocationInfo(mLocation); showDistanceBetweenTwoPoint(); } // 显示两点距离 private void showDistanceBetweenTwoPoint() { final float[] results = new float[3]; Location.distanceBetween(30.333, 30.333, 40.444, 40.444, results); mTextViewLocationDist.setText(results[0] + "米"); } /** * 显示具体位置信息 * @param location */ private void showLocationInfo(Location location) { StringBuffer strb = new StringBuffer(); strb.append("位置信息:" + "\n"); if(location!=null){ strb.append("经度:" + location.getLongitude() + "\n"); strb.append("纬度:" + location.getLatitude() + "\n"); strb.append("高度:" + location.getAltitude() + "\n"); strb.append("速度:" + location.getSpeed() + "\n"); } mTextViewLocationInfo.setText(strb); } /** * 显示有选择的Provider的信息,通过Criteria进行过滤 */ private void showSelectedLocationProvider() { //获取各种类型的LocationProvider的方式,这里作为一种补充 // // 获取passive locationprovider // LocationProvider locattionprovider_passive = mLocationManager // .getProvider(LocationManager.PASSIVE_PROVIDER); // // 获取gps locationprovider // LocationProvider locattionprovider_gps = mLocationManager // .getProvider(LocationManager.GPS_PROVIDER); // // 获取network locationprovider // LocationProvider locattionprovider_network = mLocationManager // .getProvider(LocationManager.NETWORK_PROVIDER); // LocationProvider Criteria criteria = new Criteria(); // 设置LocationProvider是免费的 criteria.setCostAllowed(false); // 设置LocationProvider提供高度信息 criteria.setAltitudeRequired(true); // 设置LocationProvider提供方向信息 criteria.setBearingRequired(true); List<String> providerName = mLocationManager.getProviders(criteria, true); StringBuffer strb = new StringBuffer(); for (int i = 0; i < providerName.size(); i++) { strb.append("类型" + i + providerName.get(i) + "\n"); } mTextViewLocationSele.setText(strb); } /** * 显示定位的Provider的类型 */ private void showLocationType() { List<String> locationtype = new ArrayList<String>(); locationtype = mLocationManager.getAllProviders(); StringBuffer strb = new StringBuffer(); strb.append("LocationProvider类型:\n"); for (int i = 0; i < locationtype.size(); i++) { strb.append("类型" + i +":"+ locationtype.get(i) + "\n"); } mTextViewLocationType.setText(strb); } @Override public void onLocationChanged(Location arg0) { // GPS位置发生变化时,更新信息 showLocationInfo(arg0); } @Override public void onProviderDisabled(String arg0) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String arg0) { // 当GPS location Provider可用时,更新位置 showLocationInfo(mLocationManager.getLastKnownLocation(arg0)); } @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) { // TODO Auto-generated method stub } }
布局
[code]<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textview_locationType" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="@string/hello_world" /> <View android:layout_width="match_parent" android:layout_height="2px" android:background="#00f" /> <TextView android:id="@+id/textview_locationSelected" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="@string/hello_world" /> <View android:layout_width="match_parent" android:layout_height="2px" android:background="#00f" /> <TextView android:id="@+id/textview_locationInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="@string/hello_world" /> <View android:layout_width="match_parent" android:layout_height="2px" android:background="#00f" /> <TextView android:id="@+id/textview_locationdistance" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="@string/hello_world" /> </LinearLayout> </ScrollView> </LinearLayout>
权限
[code] <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
相关文章推荐
- Core Animation一些Demo总结 (动态切换图片、大转盘、图片折叠、进度条等动画效果)
- Android真机调试试验
- android下数据库的创建
- Android五个进程等级(转)
- android - Support 包
- Android 地理位置Location服务之LocationManager,Geocoder详解
- Android支付宝支付设计开发
- android中关联第三方jar源码与Javadoc
- Android笔记---Intent实现Activity跳转
- Android项目中包名的改动
- android 常见系统广播
- Android学习心得第一课
- Android 手机截屏
- ANDROID_MARS学习笔记_S04_002_用AsyncTask实现异步操作
- 《Android源码设计模式》读书笔记 (11) 第11章 命令模式
- Android MediaPlayer Error -1004
- the rendering library is more recent than your version of android studio
- Android 一键打造仿IOS右滑退出Activity,非常简单的集成方式(SwipeFinishLayout)
- Android开发学习之路--百度地图之初体验
- MVP模式在Android开发中的最佳实践