Android 通过基站进行定位

这是百度百科对基站定位的定义:

基站定位一般应用于手机用户,手机基站定位服务又叫做移动位置服务(LBS——Location Based Service),它是通过电信移动运营商的网络(如GSM网)获取移动终端用户的位置信息(经纬度坐标),在电子地图平台的支持下,为用户提供相应服务的一种增值业务,例如目前中国移动动感地带提供的动感位置查询服务等。

我的理解就是

如果你的手机插了手机卡,我就可以实时知道你的位置信息

那代码怎么写呢?

首先要知道得到哪些数据可以准确知道手机的位置。

mcc=460&mnc=1&lac=4301&ci=20986

有了这些数据,我们就可以得到这样的结果(看图):

Android 通过基站进行定位_第1张图片

是不是很爽?在这儿我们还有一些重要东西需要说明一下。
因为电信和移动联通有点儿不一样,所以查询时传输的数据也有点儿不一样,需要着重注意一下。
具体哪儿不一样,先看图

Android 通过基站进行定位_第2张图片

但是归根到底,我们需要这样几个数据
1、mcc 这个值是国家代码,中国就是 460
2、mnc 这个是网络类型,就是自己的手机卡是移动,联通,还是电信。
3、lac 还有 ci(cellId)

知道需要获取什么数据,Android 中怎么获取呢?我们直接上代码。

public class GetIdUtil {
    private Context mContext;
    TelephonyManager mTelephonyManager;

    public GetIdUtil(Context context){
        this.mContext = context;
    }
    /**
     * 获取 基站 信息
     * @return
     */
    public String getBaseStationInformation(){
        if(mTelephonyManager==null){
            mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        }
        // 返回值MCC + MNC (注意:电信的mnc 对应的是 sid)
        String operator = mTelephonyManager.getNetworkOperator();
        int mcc = -1;
        int mnc = -1;
        if(operator!=null&&operator.length()>3){
            mcc = Integer.parseInt(operator.substring(0, 3));
            mnc = Integer.parseInt(operator.substring(3));
        }

        // 获取邻区基站信息
        List infos = mTelephonyManager.getNeighboringCellInfo();
        StringBuffer sb = new StringBuffer("总数 : " + infos.size() + "\n");

        for (NeighboringCellInfo info1 : infos) { // 根据邻区总数进行循环
            sb.append(" LAC : " + info1.getLac()); // 取出当前邻区的LAC
            sb.append("\n CID : " + info1.getCid()); // 取出当前邻区的CID
            sb.append("\n BSSS : " + (-113 + 2 * info1.getRssi()) + "\n"); // 获取邻区基站信号强度
        }


        int type = mTelephonyManager.getNetworkType();

        Toast.makeText(mContext,"type:= "+type,Toast.LENGTH_LONG).show();
        //需要判断网络类型,因为获取数据的方法不一样
        if(type == TelephonyManager.NETWORK_TYPE_CDMA        // 电信cdma网
                || type == TelephonyManager.NETWORK_TYPE_1xRTT
                || type == TelephonyManager.NETWORK_TYPE_EVDO_0
                || type == TelephonyManager.NETWORK_TYPE_EVDO_A
                || type == TelephonyManager.NETWORK_TYPE_EVDO_B
                || type == TelephonyManager.NETWORK_TYPE_LTE){
            CdmaCellLocation cdma = (CdmaCellLocation) mTelephonyManager.getCellLocation();
            if(cdma!=null){
                sb.append(" MCC = " + mcc );
                sb.append("\n cdma.getBaseStationLatitude()"+cdma.getBaseStationLatitude()/14400 +"\n"
                +"cdma.getBaseStationLongitude() "+cdma.getBaseStationLongitude()/14400 +"\n"
                +"cdma.getBaseStationId()(cid)  "+cdma.getBaseStationId()
                +"\n  cdma.getNetworkId()(lac)   "+cdma.getNetworkId()
                +"\n  cdma.getSystemId()(mnc)   "+cdma.getSystemId());
            }else{
                sb.append("can not get the CdmaCellLocation");
            }

        }else if(type == TelephonyManager.NETWORK_TYPE_GPRS         // 移动和联通GSM网
                || type == TelephonyManager.NETWORK_TYPE_EDGE
                || type == TelephonyManager.NETWORK_TYPE_HSDPA
                || type == TelephonyManager.NETWORK_TYPE_UMTS
                || type == TelephonyManager.NETWORK_TYPE_LTE){
            GsmCellLocation gsm = (GsmCellLocation) mTelephonyManager.getCellLocation();
            if(gsm!=null){
                sb.append("  gsm.getCid()(cid)   "+gsm.getCid()+"  \n "//移动联通 cid
                +"gsm.getLac()(lac) "+gsm.getLac()+"  \n "             //移动联通 lac
                +"gsm.getPsc()  "+gsm.getPsc());
            }else{
                sb.append("can not get the GsmCellLocation");
            }
        }else if(type == TelephonyManager.NETWORK_TYPE_UNKNOWN){
            Toast.makeText(mContext,"电话卡不可用!",Toast.LENGTH_LONG).show();
        }

        Log.d("spp","mTelephonyManager.getNetworkType(); "+mTelephonyManager.getNetworkType());
        Log.i(TAG, " 获取邻区基站信息:" + sb.toString());
        return sb.toString();
    }
}

然后,爽歪歪。

你可能感兴趣的:(Android)