GPS定位例子
2016-03-15 12:33
501 查看
package com.itheima62.gpsdemo; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv_mess; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_mess = (TextView) findViewById(R.id.tv_gps_mess); //定位 定位管理器 LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); LocationListener listener = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } /* (non-Javadoc) * 位置变化,就触发此方法调用,覆盖此方法可以追踪回调结果信息 * @see android.location.LocationListener#onLocationChanged(android.location.Location) */ @Override public void onLocationChanged(Location location) { //获取位置变化的结果 float accuracy = location.getAccuracy();//精确度,以米为单位 double altitude = location.getAltitude();//获取海拔高度 double longitude = location.getLongitude();//获取经度 double latitude = location.getLatitude();//获取纬度 float speed = location.getSpeed();//速度 tv_mess.append("accuracy:" + accuracy + "\n"); tv_mess.append("altitude:" + altitude + "\n"); tv_mess.append("longitude:" + longitude + "\n"); tv_mess.append("latitude:" + latitude + "\n"); tv_mess.append("speed:" + speed + "\n"); } }; /** * provider 定位的方式: 1,wifi 2,3g/4g 消极定位 3,gps * minTime 定位的时间差 10分钟 * minDistance 定位的距离差 10m * listener 定位的监听回调 */ lm.requestLocationUpdates("gps", 0, 0, listener); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
相关文章推荐
- C++怎么把字符串中的小写变为大写
- 直接插入排序
- maven 常用命令
- 珠宝网站 今日金价 采集(伦敦实时交易价格)
- Codeforces 627B Factory Repairs 线段树
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
- Tomcat Connector三种运行模式(BIO, NIO, APR)的比较和优化
- 第一周作业(续)
- NYOJ-108-士兵杀敌(一)
- UIScrollView使用自动布局不能滑动的解决方法
- Git操作整理
- PHPExcel 报 Allowed memory size of 8388608 byte
- php 多语言(UTF-8编码)导出Excel、CSV乱码解决办法之导出UTF-8编码的Excel、CSV
- 多个tableView共用一个Controller
- bzoj1072: [SCOI2007]排列perm
- 最长回文子串
- Java中读取一个TXT文件中的每行内容的前5个字符,并保存到另一个TXT文件中。
- syetem("pause")与getchar()在解决闪退问题时的区别
- c 链表排序 - 选择排序
- iOS开发小技巧