通过Wifi获取经纬度

第一步:
Java代码
  1. //获取wifi管理对象  
  2. WifiManager mainWifi (WifiManager) getSystemService(Context.WIFI_SERVICE);  
  3. //判断wifi是否开启  
  4. if (mainWifi.isWifiEnabled())  
  5.  
  6. //发送接入点的扫描请求,返回true成功。否则失败  
  7. mainWifi.startScan();  
  8. //启动一个线程执行第二步中的代码  
  9.  
  10.    


第二步:这一步比较耗时,最好写在线程中。
Java代码
  1. public Location setWeather()   
  2.  
  3. BufferedReader br null 
  4. try   
  5.  
  6. //接收请求结果,它会将所有链接wifi热点的链接信息返回  
  7. List<ScanResult> wifiList mainWifi.getScanResults();  
  8. HttpPost httpRequest new HttpPost("http://www.google.com/loc/json");  
  9. //封装请求的参数  
  10. JSONObject holder new JSONObject();  
  11. JSONArray array new JSONArray();  
  12. holder.put("version""1.1.0");  
  13. holder.put("host""maps.google.com");  
  14. holder.put("request_address"true);  
  15. for (int 0wifiList.size(); i++)   
  16.  
  17. //只取当前链接信息。通过mac地址进行匹配  
  18. //mac地址可以用macAddress mainWifi.getConnectionInfo().getMacAddress();获得  
  19. if (wifiList.get(i).BSSID.equals(macAddress))  
  20.  
  21. JSONObject current_data new JSONObject();  
  22. current_data.put("mac_address"wifiList.get(i).BSSID);  
  23. current_data.put("ssid"wifiList.get(i).SSID);  
  24. current_data.put("signal_strength"wifiList.get(i).level);  
  25. array.put(current_data);  
  26.  
  27.  
  28. holder.put("wifi_towers"array);  
  29. StringEntity se new StringEntity(holder.toString());  
  30. httpRequest.setEntity(se);  
  31. HttpResponse resp new DefaultHttpClient().execute(httpRequest);  
  32. if (resp.getStatusLine().getStatusCode() == 200  
  33.  
  34. HttpEntity entity resp.getEntity();  
  35. br new BufferedReader(new InputStreamReader(entity.getContent()));  
  36. StringBuffer sb new StringBuffer();  
  37. String result br.readLine();  
  38. while (result != null  
  39.  
  40. sb.append(result);  
  41. result br.readLine();  
  42.  
  43. JSONObject location new JSONObject(sb.toString());  
  44. location (JSONObject) location.get("location");  
  45. Location loc new Location(LocationManager.NETWORK_PROVIDER);  
  46. loc.setLatitude((Double) location.get("latitude"));  
  47. loc.setLongitude((Double) location.get("longitude"));  
  48. return loc;  
  49.  
  50. return null 
  51.   
  52. catch (JSONException e)   
  53.  
  54. Log.e(e.toString());  
  55.   
  56. catch (ClientProtocolException e)   
  57.  
  58. Log.e(e.toString());  
  59.   
  60. catch (IOException e)   
  61.  
  62. Log.e(e.toString());  
  63.   
  64. catch (Exception e)   
  65.  
  66. Log.e(e.toString());  
  67.  
  68. finally  
  69.  
  70. if (null != br)  
  71.  
  72. try   
  73.  
  74. br.close();  
  75.   
  76. catch (IOException e)   
  77.  
  78. Log.e(e.toString());  
  79.  
  80.  
  81.  
  82. return null 
  83.  
  84.  
  85.    


ok. 到此就可以获取经纬度了。当然如果你所在的 WIFI从来没有通过其他设备定位过,及 google数据库中没有该wifi热点的位置信息,那就获取不到经纬度。如果各位大侠有更好的办法,共同讨论
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值