Android开发实战之GPS定位实例及代码展示

2016-03-24 11:54:41 600浏览

         在目前随着移动互联网科技飞速的发展,如今Android用户在中国更是达到80%以上的市场占有率,火爆的市场带来了Android开发人员旺盛的需求,下面我们来介绍一个关于Android开发实战之使用GPS定位技术获取当前位置的信息的案例及代码吧。

         1、在AndroidManifest.xml中添加ACCESS_FINE_LOCATION权限,具体代码如下所示。

         2、编写主文件main.xml,用于创建用户界面,具体代码如下所示。

         android:orientation="vertical"

         android:layout_width="fill_parent"

         android:layout_height="fill_parent"

         >

         android:id="@+id/myLocationText"

         android:layout_width="fill_parent"

         android:layout_height="wrap_content"

         android:text="@string/hello"

         />

         3、在onCreate(Bundle savedInstanceState)中获取当前位置信息,具体代码如下所示。

         publicvoid onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         setContentView(R.layout.main);

         LocationManager locationManager;

         StringserviceName=Context.LOCATION_SERVICE;

         locationManager=(LocationManager)getSystemService(serviceName);

         //Stringprovider=LocationManager.GPS_PROVIDER;

         Criteria criteria=newCriteria;

         criteria.setAccuracy(Criteria.ACCURACY_FINE);

         criteria.setAltitudeRequired(false);

         criteria.setBearingRequired(false);

        criteria.setCostAllowed(true);

         criteria.setPowerRequirement(Criteria.POWER_LOW);

         Stringprovider=locationManager.getBestProvider(criteria,true);

         Location location=locationManager.getLastKnownLocation(provider);

         updateWithNewLocation(location);

         /*每隔1000ms更新一次,并且不考虑位置的变化。*/

         locationManager.requestLocationUpdates(provider,2000,10,

         locationListener);

         }

         在上述代码中,LocationManager用于周期获得当前设备的一个类。要获取LocationManager实例,需要调用Context.getSystemService方法并传入服务名LOCATION_SERVICE("location")。创建LocationManager实例后,就可以通过调用getLastKnownLocation方法将上一次LocationManager获得的有效位置信息以Location对象的形式返回。getLastKnownLocation方法需要传入一个字符串参数来确定使用定位服务类型,本实例传入的是静态常量LocationManager.GPS_PROVIDER,这表示使用GPS技术定位。最后还需要使用Location对象将位置信息以文本方式显示到用户界面。

         4、定义方法updateWithNewLocation(Location location),用于更新显示用户界面。具体代码如下所示。

         void updateWithNewLocation(Location location) {

        StringlatLongString;

         TextView myLocationText;

         myLocationText=(TextView)findViewById(R.id.myLocationText);

         if(location !=null) {

         doublelat=location.getLatitude;

         doublelng=location.getLongitude;

         latLongString="纬度:"+lat+"\n经度:"+lng;

         }else{

         latLongString="无法获取地理信息";

         }

         myLocationText.setText("您当前的位置是:\n"+

         latLongString);

         }

         }

         5、定义LocationListener对象locationListener,当坐标改变时触发此函数。如果Provider传进相同的坐标,它就不会被触发。

         final LocationListener locationListener=newLocationListener {

         publicvoid onLocationChanged(Location location) {

         updateWithNewLocation(location);

         }

         publicvoid onProviderDisabled(Stringprovider){

         updateWithNewLocation(null);

         }

         publicvoid onProviderEnabled(Stringprovider){ }

         publicvoid onStatusChanged(Stringprovider,intstatus,

         Bundle extras){ }

         };

         至此整个实例介绍完毕。因为模拟器上没有GPS设备,所以需要在eclipse的DDMS工具中提供模拟的GPS数据。即依次单击“DDMS”︱“Emulator Control”,在弹出对话框中找到“Location Control”选项,在此输入坐标,完成后单击“Send”按钮。

         因为用到了Google API,所以要在项目中引入Google API,邮件单击项目选择“Properties”,在弹出对话框中选择Google API版本。

 查看更多关于“Android培训资讯”的相关文章>>

标签:

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号