Android 定位GPS的使用
来源:互联网 发布:centos 修改ssh端口 编辑:程序博客网 时间:2024/05/16 00:32
刚开始研究android本身的GPS定位,很多东西还不够清楚,只想将自己学到的进行记录。关于gps定位的测试可以使用Genymotion模拟器也可以使用真机测试,如果使用真机测试需要注意:Android的GPS定位只有在没有遮盖物的情况下才能获得Location!=null,否则获取不到经纬度信息。比如你在室内是获得不到位置信息的,另外这里只是介绍一下如何获取到经纬度信息,对于地理位置的解析由于无法获取google服务因此无法对地理位置进行解析,因此平时使用的定位基本都是百度地图或是高德地图的sdk。
获取LocationManager
要想使用Android自带的定位服务就要先获取这项服务也就是获得LocationManager
LocationManager mLocationManager = (LocationManager) getSystemService(mContext.LOCATION_SERVICE);
LocationProvider
所有种类的Provider
LocationProvider可以获取多种,下面的方法是获取所有的provider的方式。
/** * 显示定位的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进行筛选的。
/** * 显示有选择的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将不再为空。
// 进行定位的实时监听,每5秒或者每10米监听一次 mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 50, 10, this); if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ mLocation = mLocationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); }
具体位置信息由location获得。
/** * 显示具体位置信息 * @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); }
测量两点距离
// 显示两点距离 private void showDistanceBetweenTwoPoint() { final float[] results = new float[3]; Location.distanceBetween(30.333, 30.333, 40.444, 40.444, results); mTextViewLocationDist.setText(results[0] + "米"); }
完整代码
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 }}
布局
<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>
权限
<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" />
0 0
- Android 定位GPS的使用
- Android的GPS定位
- Android的gps定位
- Android的GPS定位
- Android入门:使用Android GPS实现简单的定位
- Android GPS 定位的实现
- Android GPS 定位的实现
- Android GPS 定位的实现
- Android GPS 定位的实现
- Android GPS 定位的实现
- xamarin android使用gps定位获取经纬度
- Android之使用GPS和NetWork定位
- android GPS定位,基站定位,WIFI定位开关的控制
- android上的GPS定位的问题
- android上的GPS定位的问题
- android的GPS定位跟踪系统
- android中的与GPS定位的API
- Android GPS 定位的实现(1)
- zoj3626 Treasure Hunt I(树型dp+分组背包)
- Android学习(7)---Android Studio集成百度地图SDK时获取SHA1与遇到的错误
- 多线程服务器模型-one loop per thread
- 【完全跨域】异步上传文件并获得返回值
- 深入理解HTTP Session
- Android 定位GPS的使用
- springmvc @ResponseBody返回json 报406 not acceptable
- socket链接失败,程序不往下走的问题
- UITabbarController基本应用
- tab选择效果SelectedTab
- 四种生成和解析XML文档的方法详解
- MySQL模糊查询,按关键词匹配度排序
- CentOS常用的文件操作命令总结
- hdu2561