.Android定位的三种方式
(1)网络:wifi、2G、3G、4G、5G,网上的时候会分配给你一个IP地址,每个IP与实际地址是对应的;
(2)基站:基站越多定位越准,精确度为30米-1公里;
(3)GPS:美国的GPS卫星定位,全球有24颗卫星在为GPS定位服务,实际上有27颗,其中3个是备用的;
至少需要3个以上才能定位;精确度在15米左右;容易被高、厚、大的物体屏蔽GPS信号;
中国的北斗卫星,主要用于大型国企、科研、军事;民用的为美国的GPS卫星;
GPS设置的特点:不需要网络,定位时间长,开机启动慢;
(4)AGPS 网络辅助定位系统:结合了网络快速和GPS准确的优点;
在代码中使用LocationManager可以得到三种位置提供者;
packagecom.itheima.mobilesafe.service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
importandroid.content.SharedPreferences;
importandroid.content.SharedPreferences.Editor;
import android.location.Location;
importandroid.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
public class GPSService extendsService {
privateLocationManager lm;
privateString a;
privateString j;
privateString w;
privateSharedPreferences sp;
privateMyLocationListener ml;
@Override
publicIBinder onBind(Intent intent) {
returnnull;
}
@Override
publicvoid onCreate() {
super.onCreate();
sp= getSharedPreferences("config", Context.MODE_PRIVATE);
lm= (LocationManager) getSystemService(LOCATION_SERVICE);
//更新地理位置信息
//provider 定位方式:GPS、网络、基站
//minTime 更新地理位置间隔的最小时间
//minDistance更新地理位置间隔的最小移动距离
//listener 位置变化的监听器
ml= new MyLocationListener();
// lm.getBestProvider(criteria, enabledOnly)
lm.requestLocationUpdates("gps",0, 0, ml);
}
@Override
publicvoid onDestroy() {
super.onDestroy();
//移除获得地理位置信息的监听器
lm.removeUpdates(ml);
lm= null;
}
/**
* 自定义一个位置监听器
* @author Administrator
*
*/
privateclass MyLocationListener implements LocationListener{
/**
* 位置发生变化回调这个方法
* location 位置对象
*/
@Override
publicvoid onLocationChanged(Location location) {
a= location.getAccuracy() + "/";
j= location.getLongitude() + "/";
w= location.getLatitude() + "";
Editoreditor = sp.edit();
editor.putString("location",a + j + w);
editor.commit();
}
/**
* 位置发生变化回调这个方法
* 定位方式的状态的变化:GPS 关闭-打开,打开-关闭
*/
@Override
publicvoid onStatusChanged(String provider, int status, Bundle extras) {
}
/**
*
* 定位方式可用时回调这个方法
*/
@Override
publicvoid onProviderEnabled(String provider) {
}
/**
*
* 定位方式不可用时回调这个方法
*/
@Override
publicvoid onProviderDisabled(String provider) {
}
}
}