gps的获取
来源:互联网 发布:mysql error 1007 编辑:程序博客网 时间:2024/05/22 15:21
首先直接贴代码:
package com.example.gpstest;import java.util.Iterator;import android.location.Criteria;import android.location.GpsSatellite;import android.location.GpsStatus;import android.location.GpsStatus.Listener;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.location.LocationProvider;import android.os.Bundle;import android.os.Looper;import android.provider.Settings;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.util.Log;import android.view.Menu;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { private EditText editText; private LocationManager lm; private static final String TAG = "GpsActivity"; @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); lm.removeUpdates(locationListener); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.editText1); lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // 判断GPS是否正常启动 if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) { Toast.makeText(this, "请开启GPS导航...", Toast.LENGTH_SHORT).show(); // 返回开启GPS导航设置界面 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent, 0); return; } // 为获取地理位置信息时设置查询条件 String bestProvider = lm.getBestProvider(getCriteria(), true); // 获取位置信息 // 如果不设置查询要求,getLastKnownLocation方法传人的参数为LocationManager.GPS_PROVIDER Location location = lm.getLastKnownLocation(bestProvider); updateView(location); // 监听状态 lm.addGpsStatusListener(listener); // 绑定监听,有4个参数 // 参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种 // 参数2,位置信息更新周期,单位毫秒 // 参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息 // 参数4,监听 // 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新 // 1秒更新一次,或最小位移变化超过1米更新一次; // 注意:此处更新准确度非常低,推荐在service里面启动一个Thread,在run中sleep(10000);然后执行handler.sendMessage(),更新位置 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener); } // 位置监听 private LocationListener locationListener = new LocationListener() { /** * 位置信息变化时触发 */ public void onLocationChanged(Location location) { updateView(location); Log.i(TAG, "时间:" + location.getTime()); Log.i(TAG, "经度:" + location.getLongitude()); Log.i(TAG, "纬度:" + location.getLatitude()); Log.i(TAG, "海拔:" + location.getAltitude()); } /** * GPS状态变化时触发 */ public void onStatusChanged(String provider, int status, Bundle extras) { switch (status) { // GPS状态为可见时 case LocationProvider.AVAILABLE: Log.i(TAG, "当前GPS状态为可见状态"); break; // GPS状态为服务区外时 case LocationProvider.OUT_OF_SERVICE: Log.i(TAG, "当前GPS状态为服务区外状态"); break; // GPS状态为暂停服务时 case LocationProvider.TEMPORARILY_UNAVAILABLE: Log.i(TAG, "当前GPS状态为暂停服务状态"); break; } } /** * GPS开启时触发 */ public void onProviderEnabled(String provider) { Location location = lm.getLastKnownLocation(provider); updateView(location); } /** * GPS禁用时触发 */ public void onProviderDisabled(String provider) { updateView(null); } }; // 状态监听 GpsStatus.Listener listener = new GpsStatus.Listener() { public void onGpsStatusChanged(int event) { switch (event) { // 第一次定位 case GpsStatus.GPS_EVENT_FIRST_FIX: Log.i(TAG, "第一次定位"); break; // 卫星状态改变 case GpsStatus.GPS_EVENT_SATELLITE_STATUS: Log.i(TAG, "卫星状态改变"); // 获取当前状态 GpsStatus gpsStatus = lm.getGpsStatus(null); // 获取卫星颗数的默认最大值 int maxSatellites = gpsStatus.getMaxSatellites(); // 创建一个迭代器保存所有卫星 Iterator<GpsSatellite> iters = gpsStatus.getSatellites() .iterator(); int count = 0; while (iters.hasNext() && count <= maxSatellites) { GpsSatellite s = iters.next(); count++; } System.out.println("搜索到:" + count + "颗卫星"); editText.append("\n获取的卫星数:"+count); break; // 定位启动 case GpsStatus.GPS_EVENT_STARTED: Log.i(TAG, "定位启动"); break; // 定位结束 case GpsStatus.GPS_EVENT_STOPPED: Log.i(TAG, "定位结束"); break; } }; }; /** * 实时更新文本内容 * * @param location */ private void updateView(Location location) { if (location != null) { editText.setText("设备位置信息\n\n经度:"); editText.append(String.valueOf(location.getLongitude())); editText.append("\n纬度:"); editText.append(String.valueOf(location.getLatitude())); editText.append("\n海拔:"); editText.append(String.valueOf(location.getAltitude())); editText.append("\n时间:"); editText.append(String.valueOf(location.getTime())); editText.append("\n位置精度:"); editText.append(String.valueOf(location.getAccuracy())); editText.append("\n速度:"); editText.append(String.valueOf(location.getSpeed())); editText.append("\n方向角:"); editText.append(String.valueOf(location.getBearing())); } else { // 清空EditText对象 editText.getEditableText().clear(); } } /** * 返回查询条件 * * @return */ private Criteria getCriteria() { Criteria criteria = new Criteria(); // 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细 criteria.setAccuracy(Criteria.ACCURACY_FINE); // 设置是否要求速度 criteria.setSpeedRequired(false); // 设置是否允许运营商收费 criteria.setCostAllowed(false); // 设置是否需要方位信息 criteria.setBearingRequired(false); // 设置是否需要海拔信息 criteria.setAltitudeRequired(false); // 设置对电源的需求 criteria.setPowerRequirement(Criteria.POWER_LOW); return criteria; } }
布局的话,就只要一个EditText控件:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginLeft="22dp" android:layout_marginTop="50dp" android:ems="10" android:inputType="textMultiLine" > <requestFocus /> </EditText></RelativeLayout>
最重要的是在AndroidMainfest.xml文件中添加gps权限:
<manifest... <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>...</manifest>
注意:要添加到中间去。
运行之后的界面是这样的:
如果不想出现那么多的出现卫星的数量,你可以注销掉这句代码: editText.append(“\n获取的卫星数:”+count);
0 0
- gps的获取
- Android的GPS获取方法
- 获取GPS 两点间的直线距离
- GPS获取当前的经度和纬度
- GPS 定位获取 手机当前的位置
- unity 获取设备的GPS信息
- 如何获取行政区域的边界gps数据
- 利用手机的GPS获取经纬度
- 有关使用 GPS Intermediate Driver 无法获取到GPS数据的一个问题
- 有关使用 GPS Intermediate Driver 无法获取到GPS数据的一个问题
- 有关使用 GPS Intermediate Driver 无法获取到GPS数据的一个问题
- 有关使用 GPS Intermediate Driver 无法获取到GPS数据的一个问题
- C# 中GPS数据的获取的基类
- gps获取程序
- Android GPS获取地理位置
- Android获取GPS坐标:
- android 获取GPS定位,
- Android获取GPS经纬度
- Warning: Cannot modify header information - headers already sent by
- Hibernae 的延迟加载
- Xcode 7.2 MD5 SHA1
- appium简明教程
- struts2配置文件中的include和global-results
- gps的获取
- 唯有志存高远,方能风行天下
- MFC单文档添加按钮控件、消息响应函数
- java设置环境变量
- Mysql+Tomcat下载与安装教程地址
- [leetcode]Unique Paths
- Android文件操作的一些小总结
- java中reference libraries如何删除
- Data Structure(1-8)---单链表结构与顺序存储结构优缺点