基站定位
16lz
2021-01-25
最近在做基站定位,移动电信的定位测试都可以。就是联通的定位不了。在这里把源码上传,希望各位帮忙找出原因。 标签: <无>
代码片段(4)
[图片]android 基站定位
[文件]GetBaseStationInfo.zip~62KB下载(276)
[代码]android 基站定位
001 | public class Get { |
002 |
003 |
public static class CellIDInfo { |
004 |
|
005 |
public int cellId; |
006 |
public String mobileCountryCode; |
007 |
public String mobileNetworkCode; |
008 |
public int locationAreaCode; |
009 |
public String radioType; |
010 |
public CellIDInfo(){}; |
011 |
} |
012 |
|
013 |
|
014 |
|
015 |
public Location callGear(ArrayList<CellIDInfo> cellID) { |
016 | // System.out.println(cellID.get(0)); |
017 |
if (cellID.isEmpty()) { |
018 |
return null; |
019 |
} |
020 |
DefaultHttpClient client = new DefaultHttpClient(); |
021 |
HttpPost post = new HttpPost( |
022 |
"http://www.google.com/loc/json"); |
023 |
JSONObject holder = new JSONObject(); |
024 |
try { |
025 |
holder.put("version", "1.1.0"); |
026 |
holder.put("host", "maps.google.com"); |
027 |
holder.put("home_mobile_country_code", cellID.get(0).mobileCountryCode); |
028 |
holder.put("home_mobile_network_code", cellID.get(0).mobileNetworkCode); |
029 |
holder.put("radio_type", cellID.get(0).radioType); |
030 |
holder.put("request_address", true); |
031 |
if ("460".equals(cellID.get(0).mobileCountryCode)) |
032 |
holder.put("address_language", "zh_CN"); |
033 |
else |
034 |
holder.put("address_language", "en_US"); |
035 |
JSONObject data,current_data; |
036 |
JSONArray array = new JSONArray(); |
037 |
current_data = new JSONObject(); |
038 |
current_data.put("cell_id", cellID.get(0).cellId); |
039 |
current_data.put("location_area_code", cellID.get(0).locationAreaCode); |
040 |
current_data.put("mobile_country_code", cellID.get(0).mobileCountryCode); |
041 |
current_data.put("mobile_network_code", cellID.get(0).mobileNetworkCode); |
042 |
current_data.put("age", 0); |
043 |
array.put(current_data); |
044 |
if (cellID.size() > 2) { |
045 |
for (int i = 1; i < cellID.size(); i++) { |
046 |
data = new JSONObject(); |
047 |
data.put("cell_id", cellID.get(i).cellId); |
048 |
data.put("location_area_code", cellID.get(i).locationAreaCode); |
049 |
data.put("mobile_country_code", cellID.get(i).mobileCountryCode); |
050 |
data.put("mobile_network_code", cellID.get(i).mobileNetworkCode); |
051 |
data.put("age", 0); |
052 |
array.put(data); |
053 |
} |
054 |
} |
055 |
holder.put("cell_towers", array); |
056 |
StringEntity se = new StringEntity(holder.toString()); |
057 |
Log.e("Location send", holder.toString()); |
058 |
post.setEntity(se); |
059 |
HttpResponse resp = client.execute(post); |
060 |
HttpEntity entity = resp.getEntity(); |
061 |
062 |
BufferedReader br = new BufferedReader( |
063 |
new InputStreamReader(entity.getContent())); |
064 |
StringBuffer sb = new StringBuffer(); |
065 |
String result = br.readLine(); |
066 |
while (result != null) { |
067 |
Log.e("Locaiton receive", result); |
068 |
sb.append(result); |
069 |
result = br.readLine(); |
070 |
} |
071 |
if(sb.length() <= 1) |
072 |
return null; |
073 |
data = new JSONObject(sb.toString()); |
074 |
data = (JSONObject) data.get("location"); |
075 |
076 |
Location loc = new Location(LocationManager.NETWORK_PROVIDER); |
077 |
loc.setLatitude((Double) data.get("latitude")); |
078 |
loc.setLongitude((Double) data.get("longitude")); |
079 |
loc.setAccuracy(Float.parseFloat(data.get("accuracy").toString())); |
080 |
loc.setTime(GetUTCTime()); |
081 |
return loc; |
082 |
} catch (JSONException e) { |
083 |
return null; |
084 |
} catch (UnsupportedEncodingException e) { |
085 |
e.printStackTrace(); |
086 |
} catch (ClientProtocolException e) { |
087 |
e.printStackTrace(); |
088 |
} catch (IOException e) { |
089 |
e.printStackTrace(); |
090 |
} |
091 |
return null; |
092 |
} |
093 |
|
094 |
/** |
095 |
* 获取地理位置 |
096 |
* |
097 |
* @throws Exception |
098 |
*/ |
099 |
public String getLocation(Location itude) throws Exception { |
100 |
String resultString = ""; |
101 |
102 |
/** 这里采用get方法,直接将参数加到URL上 */ |
103 |
String urlString = String.format("http://maps.google.cn/maps/geo?key=abcdefg&q=%s,%s", itude.getLatitude(), itude.getLongitude()); |
104 |
Log.i("URL", urlString); |
105 |
106 |
/** 新建HttpClient */ |
107 |
HttpClient client = new DefaultHttpClient(); |
108 |
/** 采用GET方法 */ |
109 |
HttpGet get = new HttpGet(urlString); |
110 |
try { |
111 |
/** 发起GET请求并获得返回数据 */ |
112 |
HttpResponse response = client.execute(get); |
113 |
HttpEntity entity = response.getEntity(); |
114 |
BufferedReader buffReader = new BufferedReader(new InputStreamReader(entity.getContent())); |
115 |
StringBuffer strBuff = new StringBuffer(); |
116 |
String result = null; |
117 |
while ((result = buffReader.readLine()) != null) { |
118 |
strBuff.append(result); |
119 |
} |
120 |
resultString = strBuff.toString(); |
121 |
122 |
/** 解析JSON数据,获得物理地址 */ |
123 |
if (resultString != null && resultString.length() > 0) { |
124 |
JSONObject jsonobject = new JSONObject(resultString); |
125 |
JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark").toString()); |
126 |
resultString = ""; |
127 |
for (int i = 0; i < jsonArray.length(); i++) { |
128 |
resultString = jsonArray.getJSONObject(i).getString("address"); |
129 |
} |
130 |
} |
131 |
} catch (Exception e) { |
132 |
throw new Exception("获取物理位置出现错误:" + e.getMessage()); |
133 |
} finally { |
134 |
get.abort(); |
135 |
client = null; |
136 |
} |
137 |
138 |
return resultString; |
139 |
} |
140 |
|
141 |
public long GetUTCTime() { |
142 |
Calendar cal = Calendar.getInstance(Locale.CHINA); |
143 |
int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET); |
144 |
int dstOffset = cal.get(java.util.Calendar.DST_OFFSET); |
145 |
cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset)); |
146 |
return cal.getTimeInMillis(); |
147 |
} |
148 |
|
149 | } |
[代码][其他]代码
view source print ? 001 | public class Test extends Activity { |
002 |
|
003 |
004 |
private CdmaCellLocation location = null; |
005 |
private Button btnGetInfo = null; |
006 |
|
007 |
|
008 |
/** Called when the activity is first created. */ |
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- android 学习 ----控件学习 选项卡。
- Android(安卓)WebView加载服务端JS进行交互
- android 反射静态方法传值
- Android拖动图片及多层图片叠加
- Android百度地图开发
- 【Android(安卓)开发教程】获取预设信息
- Android(安卓)SearchView设置与用法的那点事儿
- Drawable、Bitmap、byte[]之间的转换 (转)