本代码实现gps定位

上传人:第*** 文档编号:34513834 上传时间:2018-02-25 格式:DOC 页数:12 大小:58KB
返回 下载 相关 举报
本代码实现gps定位_第1页
第1页 / 共12页
本代码实现gps定位_第2页
第2页 / 共12页
本代码实现gps定位_第3页
第3页 / 共12页
本代码实现gps定位_第4页
第4页 / 共12页
本代码实现gps定位_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《本代码实现gps定位》由会员分享,可在线阅读,更多相关《本代码实现gps定位(12页珍藏版)》请在金锄头文库上搜索。

1、本代码实现 GPS 定位,并定时在界面上呈现定位的精度以及在用卫星数量。布局代码如下:首先写后台的 LBSServiceListener 实现 LocationListener 接口,在这个 LBSServiceListener 中可以重写方法,代码如下:package com.exams.demo10_lbs;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.Tim

2、eZone;import android.location.Location;import android.location.LocationListener;import android.os.Bundle;import android.util.Log;public class LBSServiceListener implements LocationListener public int GPSCurrentStatus;public Location currentLocation;public void onLocationChanged(Location location) /

3、TODO Auto-generated method stub/-/ Called when a new location is found by the location provider.if (currentLocation != null) if (isBetterLocation(location, currentLocation) / Log.v(GPSTEST, Its a better location);currentLocation = location; else / Log.v(GPSTEST, Not very good!); else / Log.v(GPSTEST

4、, Its first location);currentLocation = location;/ 将数据通过 get 的方式发送到服务器,服务器可以根据这个数据进行跟踪用户的行走状态private void doGet(String string) / TODO Auto-generated method stub/public void onProviderDisabled(String provider) / TODO Auto-generated method stub/ if (LocationManager.NETWORK_PROVIDER).equals(provider) /

5、 locationManager.removeUpdates(this);/ else if (LocationManager.GPS_PROVIDER).equals(provider) / locationManager.removeUpdates(this);/ public void onProviderEnabled(String provider) / TODO Auto-generated method stubpublic void onStatusChanged(String provider, int status, Bundle extras) / TODO Auto-g

6、enerated method stub GPSCurrentStatus = status;private static final int CHECK_INTERVAL = 1000 * 30;protected boolean isBetterLocation(Location location,Location currentBestLocation) if (currentBestLocation = null) / A new location is always better than no locationreturn true;/ Check whether the new

7、location fix is newer or olderlong timeDelta = location.getTime() - currentBestLocation.getTime();boolean isSignificantlyNewer = timeDelta CHECK_INTERVAL;boolean isSignificantlyOlder = timeDelta 0;/ If its been more than two minutes since the current location,/ use the new location/ because the user

8、 has likely movedif (isSignificantlyNewer) return true;/ If the new location is more than two minutes older, it must/ be worse else if (isSignificantlyOlder) return false;/ Check whether the new location fix is more or less accurateint accuracyDelta = (int) (location.getAccuracy() - currentBestLocat

9、ion.getAccuracy();boolean isLessAccurate = accuracyDelta 0;boolean isMoreAccurate = accuracyDelta 200;/ Check if the old and new location are from the same providerboolean isFromSameProvider = isSameProvider(location.getProvider(),currentBestLocation.getProvider();/ Determine location quality using

10、a combination of timeliness and/ accuracyif (isMoreAccurate) return true; else if (isNewer & !isLessAccurate) return true; else if (isNewer & !isSignificantlyLessAccurate& isFromSameProvider) return true;return false;/* Checks whether two providers are the same */private boolean isSameProvider(Strin

11、g provider1, String provider2) if (provider1 = null) return provider2 = null;return provider1.equals(provider2);这样这个 LBSServiceListener 已经实现,其中我使用 isBetterLocation()方法对 Location 做判断。下面代码是 GpsSatelliteListener 实现 GpsStatus.Listener:package com.exams.demo10_lbs;import android.location.GpsStatus;import

12、 android.location.GpsStatus.Listener;public class GpsSatelliteListener implements Listener public void onGpsStatusChanged(int event) / TODO Auto-generated method stubswitch (event) / 第一次定位case GpsStatus.GPS_EVENT_FIRST_FIX:break;/ 卫星状态改变case GpsStatus.GPS_EVENT_SATELLITE_STATUS:break;/ 定位启动case GpsS

13、tatus.GPS_EVENT_STARTED:break;/ 定位结束case GpsStatus.GPS_EVENT_STOPPED:break;重点是 LBSService 它是继承了 Service.在这个类中,重点通过 onStartCommand 方法和 ACTIVITY 沟通,实现将定位的经纬度等信息返回给 activity 的 textview 文本呈现出来。package com.exams.demo10_lbs;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Iterator;

14、import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.location.GpsSate

15、llite;import android.location.GpsStatus;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Binder;import android.os.Bundle;import android.os.IBinder;import android.util.Log;public class LBSService extends Service public

16、 static final String TAG = LBSService;/ 30000ms -minimum time interval between location updates, in millisecondsprivate static final long minTime = 30000;/ 最小变更距离 10m -minimum distance between location updates, in metersprivate static final float minDistance = 10;String tag = this.toString();private LocationManager locationManager;private Location

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号