private void getLocation() {
// 获取位置管理服务
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);
// 查找到服务信息
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息
orientation();
listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100 * 1000, 500, listener);
}
private void orientation() {
new Thread(new Runnable() {
@Override
public void run() {
while (truevalue == true) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); // 通过GPS获取位置
Message message = new Message();
if (location == null) {
message.what = 1;
} else {
message.what = 0;
}
hand.sendMessage(message);
}
}
}).start();
}
Handler hand = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
// show("gps无法定位");
}
}
};
latitude = location.getLatitude();
longitude = location.getLongitude();