gps、wifi、基站定位

本文深入探讨了GPS、WiFi与基站定位技术的实现原理与应用,通过实例展示了如何利用GpsTask、IAddressTask等类进行定位操作,并详细解析了定位过程中涉及到的关键步骤与异常处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

集合了gps、wifi、基站定位。
其中GPS定位首先是GpsTask类异步返回GPS经纬度信息
01.GpsTask gpstask = new GpsTask(GpsActivity.this,new GpsTaskCallBack() {                        @Override                        

02.public void gpsConnectedTimeOut() {

03.gps_tip.setText("获取GPS超时了");

04.}

05.@Override

06.public void gpsConnected(GpsData gpsdata) {

07.do_gps(gpsdata);

08.}

09.}, 3000);

10.gpstask.execute();
其中3000是设置获取gps数据timeout时间。GpsTask是根据
01.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
获取最后一次GPS信息,如果返回为Null则是根据监听获取Location信息发生改变时返回的GPS信息。因为手机获取GPS信息时间比较长,所以这个类实际使用时可能还存在一些BUG。
IAddressTask封装了获取地理位置的方法,具体代码如下:
01.package com.maxtech.common.gps;

02.

03.import java.io.BufferedReader;

04.import java.io.InputStreamReader;

05.import org.apache.http.HttpEntity;

06.import org.apache.http.HttpResponse;

07.import org.json.JSONArray;

08.import org.json.JSONObject;

09.import android.app.Activity;

10.import android.content.Context;

11.import android.net.wifi.WifiManager;

12.import android.telephony.TelephonyManager;

13.import android.telephony.gsm.GsmCellLocation;

14.

15.public abstract class IAddressTask {

16. protected Activity context;

17.

18. public IAddressTask(Activity context) {

19. this.context = context;

20. }

21.

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

23.

24. public MLocation doWifiPost() throws Exception {

25. return transResponse(execute(doWifi()));

26. }

27.

28. public MLocation doApnPost() throws Exception {

29. return transResponse(execute(doApn()));

30. }

31.

32. public MLocation doGpsPost(double lat, double lng) throws Exception {

33. return transResponse(execute(doGps(lat, lng)));

34. }

35.

36. private MLocation transResponse(HttpResponse response) {

37. MLocation location = null;

38. if (response.getStatusLine().getStatusCode() == 200) {

39. location = new MLocation();

40. HttpEntity entity = response.getEntity();

41. BufferedReader br;

42. try {

43. br = new BufferedReader(new InputStreamReader(

44. entity.getContent()));

45. StringBuffer sb = new StringBuffer();

46. String result = br.readLine();

47. while (result != null) {

48. sb.append(result);

49. result = br.readLine();

50. }

51. JSONObject json = new JSONObject(sb.toString());

52. JSONObject lca = json.getJSONObject("location");

53. location.Access_token = json.getString("access_token");

54. if (lca != null) {

55. if (lca.has("accuracy"))

56. location.Accuracy = lca.getString("accuracy");

57. if (lca.has("longitude"))

58. location.Latitude = lca.getDouble("longitude");

59. if (lca.has("latitude"))

60. location.Longitude = lca.getDouble("latitude");

61. if (lca.has("address")) {

62. JSONObject address = lca.getJSONObject("address");

63. if (address != null) {

64. if (address.has("region"))

65. location.Region = address.getString("region");

66. if (address.has("street_number"))

67. location.Street_number = address

68. .getString("street_number");

69. if (address.has("country_code"))

70. location.Country_code = address

71. .getString("country_code");

72. if (address.has("street"))

73. location.Street = address.getString("street");

74. if (address.has("city"))

75. location.City = address.getString("city");

76. if (address.has("country"))

77. location.Country = address.getString("country");

78. }

79. }

80. }

81. } catch (Exception e) {

82. e.printStackTrace();

83. location = null;

84. }

85. }

86. return location;

87. }

88.

89. private JSONObject doGps(double lat, double lng) throws Exception {

90. JSONObject holder = new JSONObject();

91. holder.put("version", "1.1.0");

92. holder.put("host", "maps.google.com");

93. holder.put("address_language", "zh_CN");

94. holder.put("request_address", true);

95. JSONObject data = new JSONObject();

96. data.put("latitude", lat);

97. data.put("longitude", lng);

98. holder.put("location", data);

99. return holder;

100. }

101.

102. private JSONObject doApn() throws Exception {

103. JSONObject holder = new JSONObject();

104. holder.put("version", "1.1.0");

105. holder.put("host", "maps.google.com");

106. holder.put("address_language", "zh_CN");

107. holder.put("request_address", true);

108. TelephonyManager tm = (TelephonyManager) context

109. .getSystemService(Context.TELEPHONY_SERVICE);

110. GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();

111. int cid = gcl.getCid();

112. int lac = gcl.getLac();

113. int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0, 3));

114. int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3, 5));

115. JSONArray array = new JSONArray();

116. JSONObject data = new JSONObject();

117. data.put("cell_id", cid);

118. data.put("location_area_code", lac);

119. data.put("mobile_country_code", mcc);

120. data.put("mobile_network_code", mnc);

121. array.put(data);

122. holder.put("cell_towers", array);

123. return holder;

124. }

125.

126. private JSONObject doWifi() throws Exception {
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 {

127. public String Access_token;

128. public double Latitude;

129. public double Longitude;

130. public String Accuracy;

131. public String Region;

132. public String Street_number;

133. public String Country_code;

134. public String Street;

135. public String City;

136. public String Country;

137.

138. @Override

139. public String toString() {

140. StringBuffer buffer = new StringBuffer();

141. buffer.append("Access_token:" + Access_token + "\n");

142. buffer.append("Region:" + Region + "\n");

143. buffer.append("Accuracy:" + Accuracy + "\n");

144. buffer.append("Latitude:" + Latitude + "\n");

145. buffer.append("Longitude:" + Longitude + "\n");

146. buffer.append("Country_code:" + Country_code + "\n");

147. buffer.append("Country:" + Country + "\n");

148. buffer.append("City:" + City + "\n");

149. buffer.append("Street:" + Street + "\n");

150. buffer.append("Street_number:" + Street_number + "\n");

151. return buffer.toString();

152. }

153. }

154.}


01.package com.maxtech.common;

02.

03.import com.maxtech.common.gps.GpsTask;

04.import com.maxtech.common.gps.GpsTaskCallBack;

05.import com.maxtech.common.gps.GpsTask.GpsData;

06.import com.maxtech.common.gps.IAddressTask.MLocation;

07.import android.app.Activity;

08.import android.app.AlertDialog;

09.import android.app.ProgressDialog;

10.import android.os.AsyncTask;

11.import android.os.Bundle;

12.import android.util.Log;

13.import android.view.View;

14.import android.view.View.OnClickListener;

15.import android.widget.TextView;

16.

17.public class GpsActivity extends Activity implements OnClickListener {

18. private TextView gps_tip = null;

19. private AlertDialog dialog = null;

20.

21. @Override

22. public void onCreate(Bundle savedInstanceState) {

23. super.onCreate(savedInstanceState);

24. setContentView(R.layout.main);

25. gps_tip = (TextView) findViewById(R.id.gps_tip);

26. findViewById(R.id.do_gps).setOnClickListener(GpsActivity.this);

27. findViewById(R.id.do_apn).setOnClickListener(GpsActivity.this);

28. findViewById(R.id.do_wifi).setOnClickListener(GpsActivity.this);

29. dialog = new ProgressDialog(GpsActivity.this);

30. dialog.setTitle("请稍等...");

31. dialog.setMessage("正在定位...");

32. }

33.

34. @SuppressWarnings("unchecked")

35. @Override

36. public void onClick(View v) {

37. gps_tip.setText("");

38. switch (v.getId()) {

39. case R.id.do_apn:

40. do_apn();

41. break;

42. case R.id.do_gps:

43. GpsTask gpstask = new GpsTask(GpsActivity.this,

44. new GpsTaskCallBack() {

45. @Override

46. public void gpsConnectedTimeOut() {

47. gps_tip.setText("获取GPS超时了");

48. }

49.

50. @Override

51. public void gpsConnected(GpsData gpsdata) {

52. do_gps(gpsdata);

53. }

54. }, 3000);

55. gpstask.execute();

56. break;

57. case R.id.do_wifi:

58. do_wifi();

59. break;

60. }

61. }

62.

63. private void do_apn() {

64. new AsyncTask<Void, Void, String>() {

65. @Override

66. protected String doInBackground(Void... params) {

67. MLocation location = null;

68. try {

69. location = new AddressTask(GpsActivity.this,

70. AddressTask.DO_APN).doApnPost();

71. } catch (Exception e) {

72. e.printStackTrace();

73. }

74. if (location == null)

75. return null;

76. return location.toString();

77. }

78.

79. @Override

80. protected void onPreExecute() {

81. dialog.show();

82. super.onPreExecute();

83. }

84.

85. @Override

86. protected void onPostExecute(String result) {

87. if (result == null) {

88. gps_tip.setText("基站定位失败了...");

89. } else {

90. gps_tip.setText(result);

91. }

92. dialog.dismiss();

93. super.onPostExecute(result);

94. }

95. }.execute();

96. }

97.

98. private void do_gps(final GpsData gpsdata) {

99. new AsyncTask<Void, Void, String>() {

100. @Override

101. protected String doInBackground(Void... params) {

102. MLocation location = null;

103. try {

104. Log.i("do_gpspost", "经纬度:" + gpsdata.getLatitude() + "----"

105. + gpsdata.getLongitude());

106. location = new AddressTask(GpsActivity.this,

107. AddressTask.DO_GPS).doGpsPost(

108. gpsdata.getLatitude(), gpsdata.getLongitude());

109. } catch (Exception e) {

110. e.printStackTrace();

111. }

112. if (location == null)

113. return "GPS信息获取错误";

114. return location.toString();

115. }

116.

117. @Override

118. protected void onPreExecute() {

119. dialog.show();

120. super.onPreExecute();

121. }

122.

123. @Override

124. protected void onPostExecute(String result) {

125. gps_tip.setText(result);

126. dialog.dismiss();

127. super.onPostExecute(result);

128. }

129. }.execute();

130. }

131.

132. private void do_wifi() {

133. new AsyncTask<Void, Void, String>() {

134. @Override

135. protected String doInBackground(Void... params) {

136. MLocation location = null;

137. try {

138. location = new AddressTask(GpsActivity.this,

139. AddressTask.DO_WIFI).doWifiPost();

140. } catch (Exception e) {

141. e.printStackTrace();

142. }

143. if (location == null)

144. return null;

145. return location.toString();

146. }

147.

148. @Override

149. protected void onPreExecute() {

150. dialog.show();

151. super.onPreExecute();

152. }

153.

154. @Override

155. protected void onPostExecute(String result) {

156. if (result != null) {

157. gps_tip.setText(result);

158. } else {

159. gps_tip.setText("WIFI定位失败了...");

160. }

161. dialog.dismiss();

162. super.onPostExecute(result);

163. }

164. }.execute();

165. }

166.}



01.package com.maxtech.common;

02.import org.apache.http.HttpHost;

03.import org.apache.http.HttpResponse;

04.import org.apache.http.client.HttpClient;

05.import org.apache.http.client.methods.HttpPost;

06.import org.apache.http.conn.params.ConnRouteParams;

07.import org.apache.http.entity.StringEntity;

08.import org.apache.http.impl.client.DefaultHttpClient;

09.import org.apache.http.params.HttpConnectionParams;

10.import org.json.JSONObject;

11.import android.app.Activity;

12.import android.database.Cursor;

13.import android.net.Uri;

14.import com.maxtech.common.gps.IAddressTask;

15.public class AddressTask extends IAddressTask {

16. public static final int DO_APN = 0;

17. public static final int DO_WIFI = 1;

18. public static final int DO_GPS = 2;

19. private int postType = -1;

20. public AddressTask(Activity context, int postType) {

21. super(context);

22. this.postType = postType;

23. }

24. @Override

25. public HttpResponse execute(JSONObject params) throws Exception {

26. HttpClient httpClient = new DefaultHttpClient();

27. HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 20 * 1000);

28. HttpConnectionParams.setSoTimeout(httpClient.getParams(), 20 * 1000);

29. HttpPost post = new HttpPost("http://74.125.71.147/loc/json"); // 设置代理

30. if (postType == DO_APN) {

31. Uri uri = Uri.parse("content://telephony/carriers/preferapn"); // 获取当前正在使用的APN接入点

32. Cursor mCursor = context.getContentResolver().query(uri, null,null, null, null);

33. if (mCursor != null) {

34. if(mCursor.moveToFirst()) {

35. String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));

36. if (proxyStr != null && proxyStr.trim().length() > 0) {

37. HttpHost proxy = new HttpHost(proxyStr, 80);

38. httpClient.getParams().setParameter(

39. ConnRouteParams.DEFAULT_PROXY, proxy);

40. }

41. }

42. }

43. }

44. StringEntity se = new StringEntity(params.toString());

45. post.setEntity(se);

46. HttpResponse response = httpClient.execute(post);

47. return response;

48. }

49. }

50. }

51. }

52.}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值