// TODO Auto-generated method stub
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//int type = tm.getNetworkType();
//if (type ==TelephonyManager.NETWORK_TYPE_CDMA) {
location = (CdmaCellLocation) tm.getCellLocation();
if(location == null)
return;
int sid = location.getSystemId();//系统标识 mobileNetworkCode
int bid = location.getBaseStationId();//基站小区号 cellId
int nid = location.getNetworkId();//网络标识 locationAreaCode
Log.i(\"sid:\", \"\" + sid);
Log.i(\"bid:\", \"\" + bid);
Log.i(\"nid:\", \"\" + nid);
ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>();
CellIDInfo info = new CellIDInfo();
info.cellId = bid;
info.locationAreaCode = nid;
info.mobileNetworkCode = String.valueOf(sid);
info.mobileCountryCode = tm.getNetworkOperator().substring(0, 3);
info.mobileCountryCode = tm.getNetworkOperator().substring(3, 5);
info.radioType = \"cdma\";
CellID.add(info);
Log.d(\"cellId:\", \"\" + info.cellId);
Log.d(\"locationAreaCode:\", \"\" + info.locationAreaCode);
Log.d(\"mobileNetworkCode:\", info.mobileNetworkCode);
Log.d(\"mobileCountryCode:\", info.mobileCountryCode);
Location loc = callGear(CellID);
if (loc !=null)
mTextView.setText(\"纬度:\" + loc.getLatitude() + \"\\n经度:\" + loc.getLongitude());
//}// end if
}// end onclick
Java code
//调用google gears的方法,该方法调用gears来获取经纬度
private Location callGear(ArrayList<CellIDInfo> cellID) {
if (cellID == null)
return null;
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
\"http://www.google.com/loc/json\");
JSONObject holder = new JSONObject();
try {
holder.put(\"version\", \"1.1.0\");
holder.put(\"host\", \"maps.google.com\");
holder.put(\"home_mobile_country_code\", cellID.get(0).mobileCountryCode);
holder.put(\"home_mobile_network_code\", cellID.get(0).mobileNetworkCode);
holder.put(\"radio_type\", cellID.get(0).radioType);
holder.put(\"request_address\", true);
if (\"460\".equals(cellID.get(0).mobileCountryCode))
holder.put(\"address_language\", \"zh_CN\");
else
holder.put(\"address_language\", \"en_US\");
JSONObject data,current_data;
JSONArray array = new JSONArray();
current_data = new JSONObject();
current_data.put(\"cell_id\", cellID.get(0).cellId);
current_data.put(\"location_area_code\", cellID.get(0).locationAreaCode);
current_data.put(\"mobile_country_code\", cellID.get(0).mobileCountryCode);
current_data.put(\"mobile_network_code\", cellID.get(0).mobileNetworkCode);
current_data.put(\"age\", 0);
current_data.put(\"signal_strength\", -60);
current_data.put(\"timing_advance\", 5555);
array.put(current_data);
holder.put(\"cell_towers\", array);
StringEntity se = new StringEntity(holder.toString());
Log.e(\"Location send\", holder.toString());
post.setEntity(se);
HttpResponse resp = client.execute(post);
HttpEntity entity = resp.getEntity();
BufferedReader br = new BufferedReader(
new InputStreamReader(entity.getContent()));
StringBuffer sb = new StringBuffer();
String result = br.readLine();
while (result != null) {
Log.e(\"Locaiton reseive\", result);
sb.append(result);
result = br.readLine();
}
data = new JSONObject(sb.toString());
Log.d(\"-\", sb.toString());
data = (JSONObject) data.get(\"location\");
Location loc = new Location(LocationManager.NETWORK_PROVIDER);
loc.setLatitude((Double) data.get(\"latitude\"));
loc.setLongitude((Double) data.get(\"longitude\"));
loc.setAccuracy(Float.parseFloat(data.get(\"accuracy\").toString()));
loc.setTime( System.currentTimeMillis());//AppUtil.getUTCTime());
return loc;
} catch (JSONException e) {
e.printStackTrace();
return null;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(\"-\", \"null 1\");
return null;
}
在弄这个的时候不要忘了加权限噢!
- Java code
- <uses-permission android:name=\"android.permission.ACCESS_COARSE_LOCATION\"></uses-permission> <uses-permission android:name=\"android.permission.READ_PHONE_STATE\"></uses-permission> <uses-permission android:name=\"android.permission.INTERNET\"></uses-permission>
对于CDMA手机其实有更方便的方法,就是利用CdmaCellLocation.getBaseStationLatitude(),但是getBaseStationLatitude()获取的是int值的纬度,在android doc里也没有详细的阐述。国外的论坛也有人说getBaseStationLatitude()返回的是垃圾信息,昨天google到一个泡菜国兄弟的帖子才找到getBaseStationLatitude()获取的是int值与真正经纬度之间的关系。
也就是CDMA层3协议的定义:
/**
* Latitude is a decimal number as specified in 3GPP2 C.S0005-A v6.0.
* It is represented in units of 0.25 seconds and ranges from -1296000
* to 1296000, both values inclusive (corresponding to a range of -90
* to +90 degrees). Integer.MAX_VALUE is considered invalid value.
*/
/**
* Longitude is a decimal number as specified in 3GPP2 C.S0005-A v6.0.
* It is represented in units of 0.25 seconds and ranges from -2592000
* to 2592000, both values inclusive (corresponding to a range of -180
* to +180 degrees). Integer.MAX_VALUE is considered invalid value.
*/
因此只要将getBaseStationLatitude()做如下处理即可:
double lat = (double)myCDMACellLoc.getBaseStationLatitude() /14400;
double lon = (double)myCDMACellLoc.getBaseStationLongitude() /14400;
/14400即 *90/1296000
这样问题就解决了!!!
关键代码:
- telephonyManager =(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
- myCDMACellLoc = (CdmaCellLocation)telephonyManager.getCellLocation();
- double lat = (double)myCDMACellLoc.getBaseStationLatitude() /14400;
- double lon = (double)myCDMACellLoc.getBaseStationLongitude() /14400;
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步