当前位置:操作系统 > 安卓/Android >>

Rexsee API介绍:基站定位功能,Android CellLocation源码

先提示一点,不能使用用模拟器研究Android的基站定位:基站信息是来自运营商的,仿真器只能模拟网络延迟(-netdelay)、网速(-netspeed)、以及一些电话相关的操作,gsm <call|accept|busy|cancel|data|hold|list|voice|status>。还不能模拟信号。


一段基于Rexsee( www.rexsee.com)的基本示例demo,其中cid 和 lac 为经纬度。
01 function query(){ 

02   var loction = eval('('+rexseeCellLocation.getLastKnownLocation()+')'); 

03   var type = location.type.toLowerCase(); 

04   var mcc = parseInt(location.operator.substring(0,3)); 

05   var mnc = (type=='gsm')?parseInt(location.operator.substring(3)):location.systemId; 

06   var cid= (type=='gsm')?location.cid:location.baseStationId; 

07   var lac= (type=='gsm')?location.lac:location.networkId; 

08   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}]}"; 

09   

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

11 }


返回的结果是:
  \


 

需要注意几个问题:
1. 如果直接用alert(rexseeCellLocation.getLastKnownLocation()); 这个方法得到的经纬度会是两个非常大的数字 所以需要通过ajax提交到"http://www.google.com/loc/json" 把所需要的经纬度返回来;
2. 开始监听,一旦位置发生变化,会触发事件onCellLocationChanged。所以,要在 onCellLocationChanged中写代码,调用你的query函数。而不是直接使用onclick测试。


Rexsee扩展函数介绍


【函数】  boolean isEnabled()
【说明】  是否正在监听基站定位的变化。
【返回】  true或false。
【参数】  无
【示例】 
view sourceprint?1 alert(rexseeCellLocation.isEnabled());


【函数】  boolean enable()
【说明】  开始监听,一旦位置发生变化,会触发事件onCellLocationChanged。
【返回】  true或false。
【参数】  无
【示例】 
view sourceprint?1 alert(rexseeCellLocation.enable());


【函数】  boolean disable()
【说明】  停止监听。
【返回】  true或false。
【参数】  无
【示例】 
view sourceprint?1 alert(rexseeCellLocation.disable());


【函数】  JsonObject getLastKnownLocation()
【说明】  读取基站定位数据,注意,CDMA网络和GSM网络的定位数据格式是不同的。
【返回】  JSON对象,使用eval('('+json+')')转换为JavaScript对象。
【参数】  无
【示例】 
view sourceprint?1 alert(rexseeCellLocation.getLastKnownLocation());

 


rexseeCellLocation.java源码如下:


001 /*  

002 * Copyright (C) 2011 The Rexsee Open Source Project  

003 *  

004 * Licensed under the Rexsee License, Version 1.0 (the "License");  

005 * you may not use this file except in compliance with the License.  

006 * You may obtain a copy of the License at  

007 *  

008 *      http://www.rexsee.com/CN/legal/license.html  

009 *  

010 * Unless required by applicable law or agreed to in writing, software  

011 * distributed under the License is distributed on an "AS IS" BASIS,  

012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  

013 * See the License for the specific language governing permissions and  

014 * limitations under the License.  

015 */ 

016    

017 package rexsee.location;   

018    

019 import rexsee.core.browser.JavascriptInterface;   

020 import rexsee.core.browser.RexseeBrowser;   

021 import android.content.Context;   

022 import android.telephony.CellLocation;   

023 import android.telephony.PhoneStateListener;   

024 import android.telephony.TelephonyManager;   

025 import android.telephony.cdma.CdmaCellLocation;   

026 import android.telephony.gsm.GsmCellLocation;   

027    

028 public class RexseeCellLocation implements JavascriptInterface {   

029    

030        private static final String INTERFACE_NAME = "CellLocation";   

031        @Override  

032        public String getInterfaceName() {   

033                return mBrowser.application.resources.prefix + INTERFACE_NAME;   

034        }   

035        @Override  

036        public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {   

037                return this;   

038        }   

039        @Override  

040        public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {   

041                return new RexseeCellLocation(childBrowser);   

042        }   

043    

044        public static final String EVENT_ONCELLLOCATIONCHANGED = "onCellLocationChanged";   

045    

046        public final Context mContext;   

047        private final RexseeBrowser mBrowser;   

048        private final int mPhoneType;   

049        private PhoneStateListener mListener = null;   

050        private CellLocation mLocation = null;   

051    

052        public RexseeCellLocation(RexseeBrowser browser) {   

053                mContext = browser.getContext();   

054         &nb

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,