First, we should remind you that you cannot use a simulator to study the location of the base station in Android: The base station information is from the operator, and the simulator can only simulate network latency (-netdelay) and network speed (-netspeed) and some telephone-related operations, gsm <call | accept | busy | cancel | data | hold | list | voice | status>. The signal cannot be simulated.
A basic demo Based on Rexsee (www.rexsee.com), where cid and lac are longitude and latitude.
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: k7j1_6lal6u_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,'00008 '));
11}
The returned result is:
Pay attention to the following issues:
1. if you directly use alert (rexseeCellLocation. getLastKnownLocation (); the latitude and longitude obtained by this method will be two very large numbers, so we need to submit to the "http://www.google.com/loc/json" Through ajax to return the required latitude and longitude;
2. Start listening. Once the location changes, the event onCellLocationChanged will be triggered. Therefore, you need to write code in onCellLocationChanged to call your query function. Instead of using onclick for testing.
Rexsee extended functions
[Function] boolean isEnabled ()
[Description] whether the system is listening for changes in the location of the base station.
Return Value: true or false.
[Parameter] None
[Example]
View sourceprint? 1 alert (rexseeCellLocation. isEnabled ());
[Function] boolean enable ()
[Description] starts listening. Once the location changes, the onCellLocationChanged event is triggered.
Return Value: true or false.
[Parameter] None
[Example]
View sourceprint? 1 alert (rexseeCellLocation. enable ());
[Function] boolean disable ()
[Note] stop listening.
Return Value: true or false.
[Parameter] None
[Example]
View sourceprint? 1 alert (rexseeCellLocation. disable ());
[Function] JsonObject getLastKnownLocation ()
[Description] Read the location data of the base station. Note that the location data formats of the CDMA network and the GSM network are different.
[Return] JSON object, which is converted to a JavaScript Object using eval ('+ json +.
[Parameter] None
[Example]
View sourceprint? 1 alert (rexseeCellLocation. getLastKnownLocation ());
The source code of rexseeCellLocation. java is as follows:
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 T in compliance with the License.
006 * You may obtain a copy of the License
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 extends criptinterface {
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 mBrowser = browser;
055 browser. eventList. add (EVENT_ONCELLLOCATIONCHANGED );
056 TelephonyManager tm = (TelephonyManager) mContext. getSystemService (Context. TELEPHONY_SERVICE );
057 mPhoneType = tm. getPhoneType ();
058}
059
060 // JavaScript Interface
061 public boolean isEnabled (){
062 return mListener! = Null;
063}
064 public boolean enable (){
065 try {
066 TelephonyManager tm = (TelephonyManager) mContext. getSystemService (Context. TELEPHONY_SERVICE );
067 mListener = new PhoneStateListener (){
068 @ Override
069 public void onCellLocationChanged (CellLocation location ){
070 mLocation = location;
071 mBrowser. eventList. run (EVENT_ONCELLLOCATIONCHANGED );
072}
073 };
074 tm. listen (mListener, PhoneStateListener. LISTEN_CELL_LOCATION );
075 return true;
076} catch (Exception e ){
077 mBrowser. exception (getInterfaceName (), e );
078 return false;
079}
080}
081 public boolean disable (){
082 if (mListener = null) return true;
083 try {
084 TelephonyManager tm = (TelephonyManager) mContext. getSystemService (Context. TELEPHONY_SERVICE );
085 tm. listen (mListener, PhoneStateListener. LISTEN_NONE );
086 mListener = null;
087 return true;
088} catch (Exception e ){
089 mBrowser. exception (getInterfaceName (), e );
090 return false;
091}
092}
093 public String getLastKnownLocation (){
094 if (mLocation = null) return "{}";
095 TelephonyManager tm = (TelephonyManager) mContext. getSystemService (Context. TELEPHONY_SERVICE );
096 String rtn = "";
097 rtn + = "\" operator \ ": \" "+ tm. getNetworkOperator () + "\"";
098 rtn + = ", \" operatorName \ ": \" "+ tm. getNetworkOperatorName () + "\"";
099 if (mPhoneType = TelephonyManager. PHONE_TYPE_GSM ){
100 GsmCellLocation gsm = (GsmCellLocation) mLocation;
101 rtn + = ", \" type \ ": \" GSM \"";
102 rtn + = ", \" cid \ ":" + gsm. getCid ();
103 rtn + = ", \" lac \ ":" + gsm. getLac ();
104} else if (mPhoneType = TelephonyManager. PHONE_TYPE_CDMA ){
105 CdmaCellLocation cdma = (CdmaCellLocation) mLocation;
106 rtn + = ", \" type \ ": \" CDMA \"";
107 rtn + = ", \" baseStationId \ ":" + cdma. getBaseStationId ();
108 rtn + = ", \" baseStationLatitude \ ":" + cdma. getBaseStationLatitude ();
109 rtn + = ", \" basestationlongpolling \ ":" + cdma. getbasestationlongpolling ();
110 rtn + = ", \" networkId \ ":" + cdma. getNetworkId ();
111 rtn + = ", \" systemId \ ":" + cdma. getSystemId ();
112}
113 return "{" + rtn + "}";
114}
115
116}
Only the source code and function events of Rexsee are organized. The related demo or source code analysis can be found in the Rexsee community. Currently, Rexsee has provided nearly 2000 extensions, covers Android native features over 90% and all open: http://www.rexsee.com/
From yejiang