1. 定位
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minTime, minDistance, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
locationManager.removeUpdates(this);
stopSelf();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
});
以上是定位一次的例子,通过getSystemService(LOCATION_SERVICE)获取定位服务,使用requestLocationUpdates添加监听,监听有两种类型:网络和GPS。onLocationChanged获取定位结果,本例子中在获取位置后移除监听并停止了服务;onProviderEnabled和onProviderDisabled获取系统设置中当前provirder是否可用,可以在这里提示用户进行设置。
2. 定位方式优化
网络定位较快,但精度不够,室内的时候通常误差在几十米左右,GPS精度在几米,但需要在室外,受环境影响较大。
参考这篇文章,但发现当从室外移到室内的时候onStatusChanged不能及时监听到状态变化,所以我选择添加计时器的方式来自动添加网络定位。
整个定位的策略:
1. 添加网络和GPS两个定位监听;
2. 首先接收网络定位结果;
3. GPS定位结果返回后,移除网络定位监听并开始计时;
4. 当GPS超过一定时间(监听最小时间间隔的两倍)没有返回结果时,添加网络定位监听;
5. 当GPS定位恢复时,返回步骤3。
代码:
public class MainActivity extends AppCompatActivity {
private MapView mMapView;
private Button mButton;
private TextView mTextView1;
private TextView mTextView2;
LocationManager locationManager;
GraphicsLayer mGraphicsLayer;
int minTime = 1000;
int minDistance = 0;
Time oldTime = new Time();
Handler mHandler;
private boolean isRemove = false;//判断网络监听是否移除
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.dingwei_bt);
mTextView1 = (TextView) findViewById(R.id.tv1);
mTextView2 = (TextView) findViewById(R.id.tv2);
mMapView = (MapView) findViewById(R.id.map);
mMapView.addLayer(TLayerFactory.Tdt_Vect_base());
mGraphicsLayer = new GraphicsLayer();
mMapView.addLayer(mGraphicsLayer);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minTime, minDistance, NETLocationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, GPSLocationListener);
}
});
mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == 1) {
Log.i("hhhhh", "handleMessage: "+msg);
if (oldTime == null) {
return false;
}
Time now = new Time();
now.setToNow();
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return false;
}
if (isRemove && now.toMillis(false) - oldTime.toMillis(false) > minTime*2) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minTime, minDistance, NETLocationListener);
isRemove=false;
}else {
new Thread(new TimerThread()).start();
}
}
return false;
}
});
}
LocationListener GPSLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i("hhhhh", "onLocationChanged: ");
if (location != null) {
oldTime.setToNow();
mTextView1.setText("GPS\n" + location.toString());
mMapView.centerAt(location.getLatitude(),location.getLongitude(),true);
mMapView.setScale(TLayer.SCALES[17]);
mGraphicsLayer.removeAll();
mGraphicsLayer.addGraphic(new Graphic(new Point(location.getLongitude(),location.getLatitude()),
new SimpleMarkerSymbol(Color.RED,10, SimpleMarkerSymbol.STYLE.CIRCLE)));
if (!isRemove) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;
}
locationManager.removeUpdates(NETLocationListener);
isRemove=true;
mTextView2.setText("");
new Thread(new TimerThread()).start();
}
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this, "GPS"+provider+"定位不可用", Toast.LENGTH_SHORT).show();
}
};
LocationListener NETLocationListener=new LocationListener() {
@Override
public void onLocationChanged(Location location) {
mTextView1.setText("");
mTextView2.setText("NET\n"+location.toString());
mMapView.centerAt(location.getLatitude(),location.getLongitude(),true);
mMapView.setScale(TLayer.SCALES[17]);
mGraphicsLayer.removeAll();
mGraphicsLayer.addGraphic(new Graphic(new Point(location.getLongitude(),location.getLatitude()),
new SimpleMarkerSymbol(Color.RED,10, SimpleMarkerSymbol.STYLE.CIRCLE)));
Toast.makeText(MainActivity.this, ""+location.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this, "NET"+provider+"定位不可用", Toast.LENGTH_SHORT).show();
}
};
class TimerThread implements Runnable{
@Override
public void run() {
try {
Thread.sleep(minTime*2);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
}
}
}