Android 常用代码---GPS

本文介绍如何在Android设备上实现GPS定位功能,包括实时获取GPS坐标、获取最近一次已知坐标、计算两点间距离、监听GPS状态变化及设置邻近提示等关键技术点。
1获取当前的GPS坐标
权限<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
代码LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
 public void onStatusChanged(String provider, int status, Bundle extras) {
  // called when the provider status changes. Possible status: OUT_OF_SERVICE, TEMPORARILY_UNAVAILABLE or AVAILABLE.
 }
 public void onProviderEnabled(String provider) {
  // called when the provider is enabled by the user
 }
 public void onProviderDisabled(String provider) {
  // called when the provider is disabled by the user, if it's already disabled, it's called immediately after requestLocationUpdates
 }
 public void onLocationChanged(Location location) {
  double latitute = location.getLatitude();
  double longitude = location.getLongitude();
  // do whatever you want with the coordinates
 }
});
2获取最近一次已知的GPS坐标
权限<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
代码LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitute, longitude = 0;
if(location != null){
 latitute = location.getLatitude();
 longitude = location.getLongitude();
});
3GPS坐标之间的距离
权限
代码Location originLocation = new Location("gps");
Location destinationLocation = new Location("gps");
originLocation.setLatitude(originLatitude);
originLocation.setLongitude(originLongitude);
destinationLocation.setLatitude(originLatitude);
destinationLocation.setLongitude(originLongitude);
float distance = originLocation.distanceTo(destinationLocation);
4注册监听GPS状态变化
权限<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
代码LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.addGpsStatusListener(new GpsStatus.Listener(){
 public void onGpsStatusChanged(int event) {
  switch(event){
   // Event sent when the GPS system has started
   case GpsStatus.GPS_EVENT_STARTED:
    // put your code here
    break;
   // Event sent when the GPS system has stopped
   case GpsStatus.GPS_EVENT_STOPPED:
    // put your code here
    break;
   // Event sent when the GPS system has received its first fix since starting
   case GpsStatus.GPS_EVENT_FIRST_FIX:
    // put your code here
    break;
   // Event sent periodically to report GPS satellite status
   case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
    // put your code here
    break;
  }
 }
});
5注册监听邻近提示
权限<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
代码// Use PendingIntent.getActivity(Context, int, Intent, int), PendingIntent.getBroadcast(Context, int, Intent, int) or 
// PendingIntent.getService(Context, int, Intent, int) to create the PendingIntent, which will be used to generate an Intent to fire 
// when the proximity condition is satisfied.
PendingIntent pendingIntent;
// latitude  the latitude of the central point of the alert region
// longitude  the longitude of the central point of the alert region
// radius   the radius of the central point of the alert region, in meters
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.addProximityAlert(latitude, longitude, radius, -1, pendingIntent);
使用GPS 定位,首先,需要在清单文件(AndroidManifest.xml)中注册获取定位的权限: **1.获取位置管理器对象LocationManager** ``` import android.location.LocationManager; LocationManager lm; // lm =(LocationManager) this.getSystemService(Context`.LOCATION_SERVICE); // ``` **2.一般使用LocationManager的getLastKnownLocation(LocationManager.GPS_PROVIDER);方法获取Location对象** ``` String provider = LocationManager.GPS_PROVIDER;// 指定LocationManager的定位方法 Location location = locationManager.getLastKnownLocation(provider);// 调用getLastKnownLocation()方法获取当前的位置信息 ``` 不过不建议用这种方法,有几点原因: 一,在很多提供定位服务的应用程序中,不仅需要获取当前的位置信息,还需要监视位置的变化,在位置改变时调用特定的处理方法 ,其中LocationManager提供了一种便捷、高效的位置监视方法requestLocationUpdates(),可以根据位置的距离变化和时间间隔设定,产生位置改变事件的条件,这样可以避免因微小的距离变化而产生大量的位置改变事件 。 二,当你开启GPS,provider的值为GPS。这时的定位方式为GPS,由于GPS定位慢,所以它不可能立即返回你一个Location对象,所以就返回null了。 **3.推荐locationManager.requestLocationUpdates();方法** LocationManager中设定监听位置变化的代码如下: ``` lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10,new MyLocationListener()); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值