Android 根据Gps、Network、Wifi获取位置

	/************ 获取位置,可以通过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;
	}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值