ITeye博客

android 基于基站,apn,gps,wifi,network 根据不同手机sim卡获取经纬度

hack_zhang · 2013-05-20 15:08 · 手机android开发 · 地图开发 · 移动开发 · Markdown

一:新建MyLocation类,本类主要管理使用各种获取经纬度的方法,由于代码比较多就不一一解释直接上代码:

 

Java代码  收藏代码

  1. package com.android.location2;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.UnsupportedEncodingException;
  6. import java.util.ArrayList;
  7. import org.apache.http.HttpEntity;
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.apache.http.client.methods.HttpPost;
  11. import org.apache.http.entity.StringEntity;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import org.json.JSONArray;
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16. import android.app.Activity;
  17. import android.content.Context;
  18. import android.location.Location;
  19. import android.location.LocationManager;
  20. import android.os.AsyncTask;
  21. import android.telephony.TelephonyManager;
  22. import android.telephony.cdma.CdmaCellLocation;
  23. import android.widget.Toast;
  24. import com.android.gps.AddressTask;
  25. import com.android.gps.CellIDInfo;
  26. import com.android.gps.GpsTask;
  27. import com.android.gps.GpsTask.GpsData;
  28. import com.android.gps.GpsTaskCallBack;
  29. import com.android.gps.IAddressTask;
  30. import com.android.gps.IAddressTask.MLocation;
  31. public class MyLocation {
  32. private LocationCallBack callBack;
  33. private boolean status = false;
  34. public MyLocation(LocationCallBack callBack, Activity cxt) {
  35. this.callBack = callBack;
  36. if (getMobileType(cxt) == 3) {
  37. TelephonyManager tm = (TelephonyManager) cxt
  38. .getSystemService(Context.TELEPHONY_SERVICE);
  39. CdmaCellLocation location = (CdmaCellLocation) tm.getCellLocation();
  40. if (location == null)
  41. return;
  42. int sid = location.getSystemId();// 系统标识 mobileNetworkCode
  43. int bid = location.getBaseStationId();// 基站小区号 cellId
  44. int nid = location.getNetworkId();// 网络标识 locationAreaCode
  45. ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>();
  46. CellIDInfo info = new CellIDInfo();
  47. info.cellId = bid;
  48. info.locationAreaCode = nid;
  49. info.mobileNetworkCode = String.valueOf(sid);
  50. info.mobileCountryCode = tm.getNetworkOperator().substring(0, 3);
  51. info.mobileCountryCode = tm.getNetworkOperator().substring(3, 5);
  52. info.radioType = "cdma";
  53. CellID.add(info);
  54. Location mLocation = callGear(CellID);
  55. if (mLocation != null) {
  56. callBack.onCurrentLocation(mLocation.getLatitude(),
  57. mLocation.getLongitude());
  58. }
  59. } else {
  60. Location mLocation = getLocation(cxt);
  61. if (mLocation != null) {
  62. callBack.onCurrentLocation(mLocation.getLatitude(),
  63. mLocation.getLongitude());
  64. } else {
  65. getMeLocation(cxt);
  66. }
  67. }
  68. }
  69. public interface LocationCallBack {
  70. void onCurrentLocation(double latitude, double longitude);
  71. }
  72. // 调用google gears的方法,该方法调用gears来获取经纬度
  73. private Location callGear(ArrayList<CellIDInfo> cellID) {
  74. if (cellID == null)
  75. return null;
  76. DefaultHttpClient client = new DefaultHttpClient();
  77. HttpPost post = new HttpPost("http://www.google.com/loc/json");
  78. JSONObject holder = new JSONObject();
  79. try {
  80. holder.put("version", "1.1.0");
  81. holder.put("host", "maps.google.com");
  82. holder.put("home_mobile_country_code",
  83. cellID.get(0).mobileCountryCode);
  84. holder.put("home_mobile_network_code",
  85. cellID.get(0).mobileNetworkCode);
  86. holder.put("radio_type", cellID.get(0).radioType);
  87. holder.put("request_address", true);
  88. if ("460".equals(cellID.get(0).mobileCountryCode))
  89. holder.put("address_language", "zh_CN");
  90. else
  91. holder.put("address_language", "en_US");
  92. JSONObject data, current_data;
  93. JSONArray array = new JSONArray();
  94. current_data = new JSONObject();
  95. current_data.put("cell_id", cellID.get(0).cellId);
  96. current_data.put("location_area_code",
  97. cellID.get(0).locationAreaCode);
  98. current_data.put("mobile_country_code",
  99. cellID.get(0).mobileCountryCode);
  100. current_data.put("mobile_network_code",
  101. cellID.get(0).mobileNetworkCode);
  102. current_data.put("age", 0);
  103. current_data.put("signal_strength", -60);
  104. current_data.put("timing_advance", 5555);
  105. array.put(current_data);
  106. holder.put("cell_towers", array);
  107. StringEntity se = new StringEntity(holder.toString());
  108. post.setEntity(se);
  109. HttpResponse resp = client.execute(post);
  110. HttpEntity entity = resp.getEntity();
  111. BufferedReader br = new BufferedReader(new InputStreamReader(
  112. entity.getContent()));
  113. StringBuffer sb = new StringBuffer();
  114. String result = br.readLine();
  115. while (result != null) {
  116. sb.append(result);
  117. result = br.readLine();
  118. }
  119. data = new JSONObject(sb.toString());
  120. data = (JSONObject) data.get("location");
  121. Location loc = new Location(LocationManager.NETWORK_PROVIDER);
  122. loc.setLatitude((Double) data.get("latitude"));
  123. loc.setLongitude((Double) data.get("longitude"));
  124. loc.setAccuracy(Float.parseFloat(data.get("accuracy").toString()));
  125. loc.setTime(System.currentTimeMillis());// AppUtil.getUTCTime());
  126. return loc;
  127. } catch (JSONException e) {
  128. e.printStackTrace();
  129. return null;
  130. } catch (UnsupportedEncodingException e) {
  131. e.printStackTrace();
  132. } catch (ClientProtocolException e) {
  133. e.printStackTrace();
  134. } catch (IOException e) {
  135. e.printStackTrace();
  136. }
  137. return null;
  138. }
  139. @SuppressWarnings("unchecked")
  140. public void getMeLocation(final Activity cxt) {
  141. do_apn(cxt);
  142. if (status) {
  143. status = false;
  144. do_wifi(cxt);
  145. } else if (status) {
  146. status = false;
  147. GpsTask gpstask = new GpsTask(cxt, new GpsTaskCallBack() {
  148. public void gpsConnectedTimeOut() {
  149. Toast.makeText(cxt, "Error", Toast.LENGTH_LONG).show();
  150. }
  151. public void gpsConnected(GpsData gpsdata) {
  152. do_gps(gpsdata, cxt);
  153. }
  154. }, 3000);
  155. gpstask.execute();
  156. }
  157. }
  158. private void do_apn(final Activity cxt) {
  159. new AsyncTask<Void, Void, MLocation>() {
  160. protected MLocation doInBackground(Void... params) {
  161. MLocation location = null;
  162. try {
  163. location = new AddressTask(cxt, IAddressTask.DO_APN)
  164. .doApnPost();
  165. } catch (Exception e) {
  166. e.printStackTrace();
  167. }
  168. if (location != null)
  169. return location;
  170. else
  171. return null;
  172. }
  173. protected void onPreExecute() {
  174. super.onPreExecute();
  175. }
  176. protected void onPostExecute(MLocation result) {
  177. if (result != null) {
  178. setData(result);
  179. status = true;
  180. } else {
  181. status = false;
  182. }
  183. super.onPostExecute(result);
  184. }
  185. }.execute();
  186. }
  187. private void do_gps(final GpsData gpsdata, final Activity cxt) {
  188. new AsyncTask<Void, Void, MLocation>() {
  189. protected MLocation doInBackground(Void... params) {
  190. MLocation location = null;
  191. try {
  192. location = new AddressTask(cxt, IAddressTask.DO_GPS)
  193. .doGpsPost(gpsdata.getLatitude(),
  194. gpsdata.getLongitude());
  195. } catch (Exception e) {
  196. e.printStackTrace();
  197. }
  198. return location;
  199. }
  200. protected void onPreExecute() {
  201. super.onPreExecute();
  202. }
  203. protected void onPostExecute(MLocation result) {
  204. // gps_tip.setText(result);
  205. if (result != null) {
  206. setData(result);
  207. status = true;
  208. } else {
  209. status = false;
  210. }
  211. super.onPostExecute(result);
  212. }
  213. }.execute();
  214. }
  215. private void do_wifi(final Activity cxt) {
  216. new AsyncTask<Void, Void, MLocation>() {
  217. protected MLocation doInBackground(Void... params) {
  218. MLocation location = null;
  219. try {
  220. location = new AddressTask(cxt, IAddressTask.DO_WIFI)
  221. .doWifiPost();
  222. } catch (Exception e) {
  223. e.printStackTrace();
  224. }
  225. if (location == null) {
  226. return null;
  227. } else
  228. return location;
  229. }
  230. protected void onPreExecute() {
  231. super.onPreExecute();
  232. }
  233. protected void onPostExecute(MLocation result) {
  234. if (result != null) {
  235. setData(result);
  236. status = true;
  237. } else {
  238. status = false;
  239. }
  240. super.onPostExecute(result);
  241. }
  242. }.execute();
  243. }
  244. private void setData(MLocation result) {
  245. callBack.onCurrentLocation(result.getLatitude(), result.getLongitude());
  246. }
  247. // Get the Location by GPS or WIFI
  248. public Location getLocation(Context context) {
  249. LocationManager locMan = (LocationManager) context
  250. .getSystemService(Context.LOCATION_SERVICE);
  251. Location location = locMan
  252. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  253. if (location == null) {
  254. location = locMan
  255. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  256. }
  257. return location;
  258. }
  259. private int getMobileType(Context context) {
  260. TelephonyManager iPhoneManager = (TelephonyManager) context
  261. .getSystemService(Context.TELEPHONY_SERVICE);
  262. String iNumeric = iPhoneManager.getSimOperator();
  263. if (iNumeric.length() > 0) {
  264. if (iNumeric.equals("46000") || iNumeric.equals("46002")) {
  265. return 1;
  266. } else if (iNumeric.equals("46001")) {
  267. return 2;
  268. } else if (iNumeric.equals("46003")) {
  269. return 3;
  270. }
  271. }
  272. return 1;
  273. }
  274. }

 

 

二:在MainActivity 中实现LocationCallBack接口,如下:

 

Java代码  收藏代码

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.widget.TextView;
  4. import com.android.location2.MyLocation.LocationCallBack;
  5. public class MainActivity extends Activity implements LocationCallBack{
  6. private TextView desText;
  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. desText = (TextView) this.findViewById(R.id.text);
  12. new MyLocation(MainActivity.this, this);
  13. }
  14. //回调方法
  15. public void onCurrentLocation(double latitude, double longitude) {
  16. desText.setText("当前经度:" + longitude + "\n当前纬度:"+ latitude);
  17. }
  18. }

 

 

三:获取经纬度使用的辅助类,这里补贴代码后面直接上传源码:

四:效果如下图:

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。