/************ 获取位置,可以通过GPS或者NETWORK **************/
private HashMap<String, Object> getLocations(Context context, int count) {
final HashMap<String, Object> rs = new HashMap<String, Object>();
final LocationManager locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Location loc = null;
boolean gpsOpen = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// boolean gpsOpen = false;
boolean netOpen = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
int i = 0;
while (i++ < count) {
if (gpsOpen) {
loc = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationManager
.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 0, locationListener);
}
// 如果GPS没获取到位置
if (null == loc) {
// network是打开的
if (netOpen) {
loc = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1000, 0,
locationListener);
}
}
// 如果都没有获取到位置,则使用wifi获得位置
if (null == loc) {
WiFiInfoManager wifiInfoManager = new WiFiInfoManager(context);
loc = wifiInfoManager.getWIFILocation(wifiInfoManager
.getWifiInfo());
} else {
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (null == loc) {
rs.put("flag", "1");
} else {
GpsData result = new GpsData();
result.Latitude = loc.getLatitude();
result.Longitude = loc.getLongitude();
Log.i("Test", "LocationAsyn---经度-->" + result.Longitude);
Log.i("Test", "LocationAsyn---纬度-->" + result.Latitude);
result.High = loc.getAltitude();
result.Direct = loc.getBearing();
result.Speed = loc.getSpeed();
long time = System.currentTimeMillis();
Date d = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
result.GpsTime = sdf.format(d);
d = null;
rs.put("gps", result);
}
locationManager.removeUpdates(locationListener);
return rs;
}
Android 根据Gps、Network、Wifi获取位置
最新推荐文章于 2014-05-26 15:27:37 发布