集合了gps、wifi、基站定位。
其中GPS定位首先是GpsTask类异步返回GPS经纬度信息
  1. GpsTask gpstask = new GpsTask(GpsActivity.this,new GpsTaskCallBack() { @Override
  2. public void gpsConnectedTimeOut() {
  3. gps_tip.setText("获取GPS超时了");
  4. }
  5. @Override
  6. public void gpsConnected(GpsData gpsdata) {
  7. do_gps(gpsdata);
  8. }
  9. }, 3000);
  10. gpstask.execute();
复制代码 其中3000是设置获取gps数据timeout时间。GpsTask是根据
  1. locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
复制代码
  1. 获取最后一次GPS信息,如果返回为Null则是根据监听获取Location信息发生改变时返回的GPS信息。因为手机获取GPS信息时间比较长,所以这个类实际使用时可能还存在一些BUG。
  2. IAddressTask封装了获取地理位置的方法,具体代码如下:
复制代码
  1. package com.maxtech.common.gps;

  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import org.apache.http.HttpEntity;
  5. import org.apache.http.HttpResponse;
  6. import org.json.JSONArray;
  7. import org.json.JSONObject;
  8. import android.app.Activity;
  9. import android.content.Context;
  10. import android.net.wifi.WifiManager;
  11. import android.telephony.TelephonyManager;
  12. import android.telephony.gsm.GsmCellLocation;

  13. public abstract class IAddressTask {
  14. protected Activity context;

  15. public IAddressTask(Activity context) {
  16. this.context = context;
  17. }

  18. public abstract HttpResponse execute(JSONObject params) throws Exception;

  19. public MLocation doWifiPost() throws Exception {
  20. return transResponse(execute(doWifi()));
  21. }

  22. public MLocation doApnPost() throws Exception {
  23. return transResponse(execute(doApn()));
  24. }

  25. public MLocation doGpsPost(double lat, double lng) throws Exception {
  26. return transResponse(execute(doGps(lat, lng)));
  27. }

  28. private MLocation transResponse(HttpResponse response) {
  29. MLocation location = null;
  30. if (response.getStatusLine().getStatusCode() == 200) {
  31. location = new MLocation();
  32. HttpEntity entity = response.getEntity();
  33. BufferedReader br;
  34. try {
  35. br = new BufferedReader(new InputStreamReader(
  36. entity.getContent()));
  37. StringBuffer sb = new StringBuffer();
  38. String result = br.readLine();
  39. while (result != null) {
  40. sb.append(result);
  41. result = br.readLine();
  42. }
  43. JSONObject json = new JSONObject(sb.toString());
  44. JSONObject lca = json.getJSONObject("location");
  45. location.Access_token = json.getString("access_token");
  46. if (lca != null) {
  47. if (lca.has("accuracy"))
  48. location.Accuracy = lca.getString("accuracy");
  49. if (lca.has("longitude"))
  50. location.Latitude = lca.getDouble("longitude");
  51. if (lca.has("latitude"))
  52. location.Longitude = lca.getDouble("latitude");
  53. if (lca.has("address")) {
  54. JSONObject address = lca.getJSONObject("address");
  55. if (address != null) {
  56. if (address.has("region"))
  57. location.Region = address.getString("region");
  58. if (address.has("street_number"))
  59. location.Street_number = address
  60. .getString("street_number");
  61. if (address.has("country_code"))
  62. location.Country_code = address
  63. .getString("country_code");
  64. if (address.has("street"))
  65. location.Street = address.getString("street");
  66. if (address.has("city"))
  67. location.City = address.getString("city");
  68. if (address.has("country"))
  69. location.Country = address.getString("country");
  70. }
  71. }
  72. }
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. location = null;
  76. }
  77. }
  78. return location;
  79. }

  80. private JSONObject doGps(double lat, double lng) throws Exception {
  81. JSONObject holder = new JSONObject();
  82. holder.put("version", "1.1.0");
  83. holder.put("host", "maps.google.com");
  84. holder.put("address_language", "zh_CN");
  85. holder.put("request_address", true);
  86. JSONObject data = new JSONObject();
  87. data.put("latitude", lat);
  88. data.put("longitude", lng);
  89. holder.put("location", data);
  90. return holder;
  91. }

  92. private JSONObject doApn() throws Exception {
  93. JSONObject holder = new JSONObject();
  94. holder.put("version", "1.1.0");
  95. holder.put("host", "maps.google.com");
  96. holder.put("address_language", "zh_CN");
  97. holder.put("request_address", true);
  98. TelephonyManager tm = (TelephonyManager) context
  99. .getSystemService(Context.TELEPHONY_SERVICE);
  100. GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
  101. int cid = gcl.getCid();
  102. int lac = gcl.getLac();
  103. int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0, 3));
  104. int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3, 5));
  105. JSONArray array = new JSONArray();
  106. JSONObject data = new JSONObject();
  107. data.put("cell_id", cid);
  108. data.put("location_area_code", lac);
  109. data.put("mobile_country_code", mcc);
  110. data.put("mobile_network_code", mnc);
  111. array.put(data);
  112. holder.put("cell_towers", array);
  113. return holder;
  114. }

  115. private JSONObject doWifi() throws Exception {<br /> JSONObject holder = new JSONObject(); holder.put("version", "1.1.0"); holder.put("host", "maps.google.com"); holder.put("address_language", "zh_CN"); holder.put("request_address", true); WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if(wifiManager.getConnectionInfo().getBSSID() == null) { throw new RuntimeException("bssid is null"); } JSONArray array = new JSONArray(); JSONObject data = new JSONObject(); data.put("mac_address", wifiManager.getConnectionInfo().getBSSID()); data.put("signal_strength", 8); data.put("age", 0); array.put(data); holder.put("wifi_towers", array); return holder; } public static class MLocation {
  116. public String Access_token;
  117. public double Latitude;
  118. public double Longitude;
  119. public String Accuracy;
  120. public String Region;
  121. public String Street_number;
  122. public String Country_code;
  123. public String Street;
  124. public String City;
  125. public String Country;

  126. @Override
  127. public String toString() {
  128. StringBuffer buffer = new StringBuffer();
  129. buffer.append("Access_token:" + Access_token + "\n");
  130. buffer.append("Region:" + Region + "\n");
  131. buffer.append("Accuracy:" + Accuracy + "\n");
  132. buffer.append("Latitude:" + Latitude + "\n");
  133. buffer.append("Longitude:" + Longitude + "\n");
  134. buffer.append("Country_code:" + Country_code + "\n");
  135. buffer.append("Country:" + Country + "\n");
  136. buffer.append("City:" + City + "\n");
  137. buffer.append("Street:" + Street + "\n");
  138. buffer.append("Street_number:" + Street_number + "\n");
  139. return buffer.toString();
  140. }
  141. }
  142. }

更多相关文章

  1. Android开发之如何手写代码进行页面布局
  2. Android跳转淘宝商品详情页代码
  3. android通过Intent调用手机图片,音频,视频录音拍照等代码
  4. Android开源项目:微信打飞机游戏源代码
  5. android 仿课程表,时间星期展示选择列表,简单易懂
  6. android中获得系统的时间
  7. android 时间1

随机推荐

  1. android 获取系统电量
  2. 【黑科技】钉钉自动打卡
  3. Android 获取控件高度宽度三种方法,防止0
  4. Android的webview加载本地html、本apk内h
  5. Android(安卓)Binder 分析——内存管理
  6. ANDROID工作学习笔记之ANDROID:SCALETYPE
  7. Android 屏幕常亮 背景常亮
  8. android shape使用总结
  9. 整理的一些免费课程分享
  10. Android View预估大小