Rexsee API介紹:基站定位功能,Android CellLocation源碼

先提示一點,不能使用用模擬器研究Android的基站定位:基站信息是來自運營商的,仿真器只能模擬網絡延遲(-netdelay)、網速(-netspeed)、以及一些電話相關的操做,gsm <call|accept|busy|cancel|data|hold|list|voice|status>。還不能模擬信號。

一段基於Rexsee( www.rexsee.com)的基本示例demo,其中cid 和 lac 爲經緯度。
function query(){
  var loction = eval('('+rexseeCellLocation.getLastKnownLocation()+')');
  var type = location.type.toLowerCase();
  var mcc = parseInt(location.operator.substring(0,3));
  var mnc = (type=='gsm')?parseInt(location.operator.substring(3)):location.systemId;
  var cid= (type=='gsm')?location.cid:location.baseStationId;
  var lac= (type=='gsm')?location.lac:location.networkId;
  var postData="{\version\":\"1.1.0\",\"host\":\maps.google.com\",\"access_token\";\"2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe\",\"home_mobile_country_code\":"+mcc+",\"home_mobile_network_code\":"+mnc+",\"address_language\";\"zh_CN\",\"radio_type\";\""+type+"\",\"request_address\":true,\"cell_towers\":[{\"cell_id\":+cid+",\"location_area_code\":+lac+",\"mobile_aountry_code\":"+mcc+",\"mobile_network_code\":"+mnc+",\"timing_advance\":5555}]}";

alert(rexseeAjax.syncSubmit('http://www.google.com/loc/json',postData,'utf-8'));
}

返回的結果是:


須要注意幾個問題:
1. 若是直接用alert(rexseeCellLocation.getLastKnownLocation()); 這個方法獲得的經緯度會是兩個很是大的數字 因此須要經過ajax提交到"http://www.google.com/loc/json" 把所須要的經緯度返回來;
2. 開始監聽,一旦位置發生變化,會觸發事件onCellLocationChanged。因此,要在 onCellLocationChanged中寫代碼,調用你的query函數。而不是直接使用onclick測試。

Rexsee擴展函數介紹

【函數】 boolean isEnabled()
【說明】 是否正在監聽基站定位的變化。
【返回】 true或false。
【參數】
【示例】
alert(rexseeCellLocation.isEnabled());

【函數】 boolean enable()
【說明】 開始監聽,一旦位置發生變化,會觸發事件onCellLocationChanged。
【返回】 true或false。
【參數】
【示例】
alert(rexseeCellLocation.enable());

【函數】 boolean disable()
【說明】 中止監聽。
【返回】 true或false。
【參數】
【示例】
alert(rexseeCellLocation.disable());

【函數】 JsonObject getLastKnownLocation()
【說明】 讀取基站定位數據,注意,CDMA網絡和GSM網絡的定位數據格式是不一樣的。
【返回】 JSON對象,使用eval('('+json+')')轉換爲JavaScript對象。
【參數】
【示例】
alert(rexseeCellLocation.getLastKnownLocation());


rexseeCellLocation.java源碼以下:

/* 
* Copyright (C) 2011 The Rexsee Open Source Project 
* 
* Licensed under the Rexsee License, Version 1.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*      http://www.rexsee.com/CN/legal/license.html 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 
 
package rexsee.location;  
 
import rexsee.core.browser.JavascriptInterface;  
import rexsee.core.browser.RexseeBrowser;  
import android.content.Context;  
import android.telephony.CellLocation;  
import android.telephony.PhoneStateListener;  
import android.telephony.TelephonyManager;  
import android.telephony.cdma.CdmaCellLocation;  
import android.telephony.gsm.GsmCellLocation;  
 
public class RexseeCellLocation implements JavascriptInterface {  
 
       private static final String INTERFACE_NAME = "CellLocation";  
       @Override  
       public String getInterfaceName() {  
               return mBrowser.application.resources.prefix + INTERFACE_NAME;  
       }  
       @Override  
       public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {  
               return this;  
       }  
       @Override  
       public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {  
               return new RexseeCellLocation(childBrowser);  
       }  
 
       public static final String EVENT_ONCELLLOCATIONCHANGED = "onCellLocationChanged";  
 
       public final Context mContext;  
       private final RexseeBrowser mBrowser;  
       private final int mPhoneType;  
       private PhoneStateListener mListener = null;  
       private CellLocation mLocation = null;  
 
       public RexseeCellLocation(RexseeBrowser browser) {  
               mContext = browser.getContext();  
               mBrowser = browser;  
               browser.eventList.add(EVENT_ONCELLLOCATIONCHANGED);  
               TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);  
               mPhoneType = tm.getPhoneType();  
       }  
 
       //JavaScript Interface  
       public boolean isEnabled() {  
               return mListener != null;  
       }  
       public boolean enable() {  
               try {  
                       TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);  
                       mListener = new PhoneStateListener() {  
                               @Override  
                               public void onCellLocationChanged(CellLocation location) {  
                                       mLocation = location;  
                                       mBrowser.eventList.run(EVENT_ONCELLLOCATIONCHANGED);  
                               }  
                       };  
                       tm.listen(mListener, PhoneStateListener.LISTEN_CELL_LOCATION);  
                       return true;  
               } catch (Exception e) {  
                       mBrowser.exception(getInterfaceName(), e);  
                       return false;  
               }  
       }  
       public boolean disable() {  
               if (mListener == null) return true;  
               try {  
                       TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);  
                       tm.listen(mListener, PhoneStateListener.LISTEN_NONE);  
                       mListener = null;  
                       return true;  
               } catch (Exception e) {  
                       mBrowser.exception(getInterfaceName(), e);  
                       return false;  
               }  
       }  
       public String getLastKnownLocation() {  
               if (mLocation == null) return "{}";  
               TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);  
               String rtn = "";  
               rtn += "\"operator\":\"" + tm.getNetworkOperator() + "\"";  
               rtn += ",\"operatorName\":\"" + tm.getNetworkOperatorName() + "\"";  
               if (mPhoneType == TelephonyManager.PHONE_TYPE_GSM) {  
                       GsmCellLocation gsm = (GsmCellLocation) mLocation;  
                       rtn += ",\"type\":\"GSM\"";  
                       rtn += ",\"cid\":" + gsm.getCid();  
                       rtn += ",\"lac\":" + gsm.getLac();  
               } else if (mPhoneType == TelephonyManager.PHONE_TYPE_CDMA) {  
                       CdmaCellLocation cdma = (CdmaCellLocation) mLocation;  
                       rtn += ",\"type\":\"CDMA\"";  
                       rtn += ",\"baseStationId\":" + cdma.getBaseStationId();  
                       rtn += ",\"baseStationLatitude\":" + cdma.getBaseStationLatitude();  
                       rtn += ",\"baseStationLongitude\":" + cdma.getBaseStationLongitude();  
                       rtn += ",\"networkId\":" + cdma.getNetworkId();  
                       rtn += ",\"systemId\":" + cdma.getSystemId();  
               }  
               return "{" + rtn + "}";  
       }  
 
	}

 僅對Rexsee的源碼和函數事件作了整理,相關的demo或源碼解析能夠在Rexsee社區瞭解,目前Rexsee已提供了近2000個擴展,覆蓋Android原生功能超過90%,且所有開放: http://www.rexsee.com/
相關文章
相關標籤/搜索