安卓开发----->GPS定位
2013-06-20 23:48
232 查看
安卓开发中需要用到GPS定位功能,我自己写了一些代码基本实现了这个功能,下面和大家分享一下
1、添加用户权限:在配置清单文件中添加用户权限使用系统的GPS功能
2、在布局文件中添加一个Button和一个TextView(用来显示结果)
3、在主代码中添加相关的代码块
4、将应用布置到安卓模拟器上得到最终效果:
1、添加用户权限:在配置清单文件中添加用户权限使用系统的GPS功能
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
2、在布局文件中添加一个Button和一个TextView(用来显示结果)
<Button android:id="@+id/Dingwei" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/Dingwei"/> <TextView android:id="@+id/txtIfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/Dingwei"/>
3、在主代码中添加相关的代码块
public class MainActivity extends Activity { //获取Button对象 Button btnGps; //获取TextView对象 TextView tv; //获得系统服务的LocatinManager对象 LocationManager locationManager; //获得系统服务 String contextServer=Context.LOCATION_SERVICE; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
//获取Button对象实例 btnGps=(Button)findViewById(R.id.Dingwei);
//获取tv对象实例 tv=(TextView)findViewById(R.id.txtIfo); //设置BtnGps的单击事件 btnGps.setOnClickListener(new myClickListener()); } //定位按钮btnGps 的单击事件 (implements继承了View.OnClickListener) private class myClickListener implements View.OnClickListener{ @Override public void onClick(View v) { //通过系统服务,取得LocationManager对象 locationManager=(LocationManager)getSystemService(contextServer); //通过GPS位置提供器获得位置 //String provider=LocationManager.GPS_PROVIDER; //Location location =locationManager.getLastKnownLocation(provider); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度 criteria.setAltitudeRequired(false);//不要求海拔 criteria.setBearingRequired(false);//不要求方位 criteria.setCostAllowed(true);//允许有花费 criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗 //从可用的位置提供器中,匹配以上标准的最佳提供器 String provider = locationManager.getBestProvider(criteria, true); //获得最后一次变化的位置 Location location = locationManager.getLastKnownLocation(provider); //获取经度和纬度 double alt=location.getLatitude(); double lng=location.getLongitude(); StringBuilder builder=new StringBuilder(); builder.append("您的位置是:\n"); builder.append("经度:\n"); builder.append(alt+"\n"); builder.append("纬度\n"); builder.append(lng); tv.setText(builder); //监听位置变化,2秒钟一次,距离10米以上 locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); } } private final LocationListener locationListener=new LocationListener() { //位置状态发生改变时调用 @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO 自动生成的方法存根 } //当位置提供器可用时调用 @Override public void onProviderEnabled(String provider) { } //当位置提供器不可用时调用 @Override public void onProviderDisabled(String provider) { } //当位置发生变化时调用 @Override public void onLocationChanged(Location location) { updataWithNewLocation(location); } private void updataWithNewLocation(Location location) { double alt=location.getLatitude(); double lng=location.getLongitude(); StringBuilder builder=new StringBuilder(); builder.append("您的位置是:\n"); builder.append("经度:\n"); builder.append(alt+"\n"); builder.append("纬度\n"); builder.append(lng); tv.setText(builder); } }; }
4、将应用布置到安卓模拟器上得到最终效果:
相关文章推荐
- 安卓开发-app异常退出的善后工作->UncaughtExceptionHandler
- <android>我遇到的机型适配(安卓开发屏幕适配&rom适配)之Rom篇
- <儿童成长乐园>安卓项目开发(三)
- 安卓开发——报错:You must specifiy a layout in the include tag: <include layout="@layout/layoutID" />
- Android 开发中遇到的Error:Execution failed for task ': :dexDebug'. > com.android.ide.common.process.Proces
- Web开发之分页算法,N(N>=3)种解决方案
- 开发<c:if>标签
- iOS网络开发之NSURLSession学习<3>
- 【Qt编程】基于Qt的词典开发系列<八>--用户登录及API调用的实现
- <杂谈1001> 一种Android手机开发架构
- <Android开发代码库>achartengine实现柱状图、圆饼图、线图等
- <杂谈1000>笛卡尔的方法论与项目开发
- JavaWeb开发Tomcat-->总结(1)
- ASP.NET MVC3 开发分类信息网:(一)ModelBase<TModel,TKey>简介
- 22.IOS开发中(null)与<null>的处理
- 【Virtual C++ 3D游戏开发】前置知识 三 <Win32窗口程序剖析>
- 【Qt编程】基于Qt的词典开发系列<十一>系统托盘的显示
- 2012.9.16 安卓游戏开发笔记(五)_ProgressBar&SeekBar
- 《开源合辑-(软件开发->框架)之(Delphi/Kylix)》 (2)
- 读<<cocos2d-x 游戏技术开发精解>>_1