`
fengzhizi715
  • 浏览: 159767 次
  • 性别: Icon_minigender_1
  • 来自: 上海 苏州
社区版块
存档分类
最新评论

使用基站、wifi实现定位

阅读更多
    转载请注明出处
    android可以借助于gps实现定位,但是很多地方是使用gps无法定位比如在室内,而且gps定位的话速度慢。
   
    那么如何克服这样的缺点使得应用程序在室内也可以定位呢?办法是有的借助于基站和wifi进行定位。具体的细节可参考:
http://code.google.com/intl/zh-CN/apis/gears/geolocation_network_protocol.html


    下面的代码实现了定位的大致功能
    CellIDInfo.java 封装了cellid的信息
public class CellIDInfo {
	
	public int cellId;
	public String mobileCountryCode;
	public String mobileNetworkCode;
	public int locationAreaCode;
	public String radioType;
	
	public CellIDInfo(){}
}


        WifiInfo.java 封装了wifi的信息
public class WifiInfo {
	
	public String mac;
	
	public WifiInfo(){}
}


        CellIDInfoManager.java 可获取所有的CellIDInfo
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.telephony.NeighboringCellInfo;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;

public class CellIDInfoManager {
	private TelephonyManager manager;
	private PhoneStateListener listener;
	private GsmCellLocation gsm;
	private CdmaCellLocation cdma;
	int lac;
	String current_ci,mcc, mnc;
	
	public CellIDInfoManager(){}
	
	public ArrayList<CellIDInfo> getCellIDInfo(Context context){
		manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		listener = new PhoneStateListener();
		manager.listen(listener, 0);
		ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>();
		CellIDInfo currentCell = new CellIDInfo();
		
		int type = manager.getNetworkType();

		if (type == TelephonyManager.NETWORK_TYPE_GPRS || type ==TelephonyManager.NETWORK_TYPE_EDGE
				|| type ==TelephonyManager.NETWORK_TYPE_HSDPA) {
			gsm = ((GsmCellLocation) manager.getCellLocation());
		    if (gsm == null) return null;
		    lac  = gsm.getLac();
			mcc = manager.getNetworkOperator().substring(0, 3);
			mnc = manager.getNetworkOperator().substring(3, 5);
		    
			currentCell.cellId = gsm.getCid();
			currentCell.mobileCountryCode = mcc;
			currentCell.mobileNetworkCode = mnc;
			currentCell.locationAreaCode = lac;
			currentCell.radioType = "gsm";
			CellID.add(currentCell);
			
			List<NeighboringCellInfo> list = manager.getNeighboringCellInfo();
			int size = list.size();
			for (int i = 0; i < size; i++) {
				CellIDInfo info = new CellIDInfo();
				info.cellId = list.get(i).getCid();
				info.mobileCountryCode = mcc;
				info.mobileCountryCode = mnc;
				info.locationAreaCode = lac;
				CellID.add(info);
			}
			return CellID;
			
		} else if (type == TelephonyManager.NETWORK_TYPE_CDMA || type ==TelephonyManager.NETWORK_TYPE_1xRTT) {
			cdma = ((CdmaCellLocation) manager.getCellLocation());
			if (cdma == null) return null;
	    	
	    	if ("460".equals(manager.getSimOperator().substring(0, 3))) 
	    		return null;
		}
		return null;
	}
}



       WifiInfoManager.java 可获取wifi的信息,目前我只取了当前连接的wifi,没有获取所有能扫描到的wifi信息。
import java.util.ArrayList;

import android.content.Context;
import android.net.wifi.WifiManager;

public class WifiInfoManager {
	
	WifiManager wm;
	
	public WifiInfoManager(){}
	
	public ArrayList getWifiInfo(Context context){
		ArrayList<WifiInfo> wifi = new ArrayList();
		wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = new WifiInfo();
		info.mac = wm.getConnectionInfo().getBSSID();
		wifi.add(info);
		return wifi;
	}
}


       调用google gears的方法,该方法调用gears来获取经纬度
private Location callGear(ArrayList<WifiInfo> wifi,
			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("mobile_country_code", cellID.get(0).mobileCountryCode);
			current_data.put("mobile_network_code", cellID.get(0).mobileNetworkCode);
			current_data.put("age", 0);
			array.put(current_data);
			
			if (cellID.size() > 2) {
				for (int i = 1; i < cellID.size(); i++) {
					data = new JSONObject();
					data.put("cell_id", cellID.get(i).cellId);
					data.put("location_area_code", cellID.get(0).locationAreaCode);
					data.put("mobile_country_code", cellID.get(0).mobileCountryCode);
					data.put("mobile_network_code", cellID.get(0).mobileNetworkCode);
					data.put("age", 0);
					array.put(data);
				}
			}
			holder.put("cell_towers", array);
			
			if (wifi.get(0).mac != null) {
				data = new JSONObject();
				data.put("mac_address", wifi.get(0).mac);
				data.put("signal_strength", 8);
				data.put("age", 0);
				array = new JSONArray();
				array.put(data);
				holder.put("wifi_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());
			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(AppUtil.getUTCTime());
			return loc;
		} catch (JSONException e) {
			return null;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}


   目前已经测试过中国移动、联通的卡,以及测试过中兴的MIC,都可以准确定位。
   不支持的是cdma,将cdma的数据传入到gears后返回的经纬度显示是在美国。

   提外话,将gps和基站、wifi三者定位结合的话效果更好。基站的定位没有wifi准确,gps的定位速度比基站、wifi都来得更慢。
16
0
分享到:
评论
6 楼 zxw13651485 2012-11-09  
学习了!!!
5 楼 zmwell 2012-07-06  
楼主啊,我用你上面的程序进行定位,我在西安,却定位到了北京,这误差是不是太大了啊,能否跟你交流一下呢?
4 楼 llyrike 2012-05-11  
google不是自带了定位的api吗?网络定位也在其中吧,为什么还要自己写这么多呢,设置一个监听不就得了?请指示
3 楼 大柳树 2011-07-11  
没有例子吗?
2 楼 fengzhizi715 2010-12-02  
superhanliu 写道
如果能定位别人就爽了。

可以做啊,做一个app放到别人的手机里,然后她每次更隔一段时间更新位置或者说更新经纬度的时候,发邮件或者sms给你。
1 楼 superhanliu 2010-12-02  
如果能定位别人就爽了。

相关推荐

    androidgps基站与WIFI定位源码.zip

    android GPS和基站定位程序及源码.rar 实现基站定位,以及WIFI定位,以及GPS定位,参考使用。

    gps.rar_GPS_GPS java_wifi定位_基站定位_定位

    实现基站定位,以及WIFI定位,以及GPS定位

    在Android里完美实现基站和WIFI定位

    众所周知的,在OPhone和大部分国产的Android定制机里不支持最简单实用的基站和WIFI定位,只能使用速度慢而耗电的GPS定位,但OPhone和华为/中兴生产的一些Android定制机却占据了一定的市场,因此导致了很多使用了定位...

    android系统的wifi和基站地理信息定位完整代码实现

    这是超葛格刚刚封装好的定理信息定位代码,专门用于wifi定位、基站定位。本人是技术爱好者,如有同道中人,请联系我。本人QQ:82076424;本人邮箱mejg@163.com

    基于矿井WiFi通信系统的混合定位方法研究

    提出了一种基于矿井WiFi通信系统的混合定位方法,该方法分粗细两步进行定位:粗定位采用基于蜂窝小区标识的非测距定位方法,确定目标所在的小区范围;细定位采用基于接收信号强度的测距定位方法。该混合定位方法可以在不...

    基于MIMO的单基站混合定位算法研究

    传统定位算法实现时往往需要3个以上的基站,且定位精度易受非视距信号的影响。针对上述问题,提出一种基于MIMO的单基站混合定位算法,利用信号路径参数以及基站、移动台、散射体之间的位置几何关系将定位问题转化成...

    android定位

    《Android里完美实现基站和WIFI定位》的代码

    煤矿井下精确定位系统研究

    利用智能信息矿灯内置加速度传感器和陀螺仪数据进行时间积分得到井下人员运动轨迹和方向,并通过智能信息矿灯识别LED可见光定位基站的ID编码信息重新定位来修正惯性导航数据,从而实现精确定位。在煤矿井下布置8个...

    Android例子源码后台定时定位开机自启

    (第三方地图API的选择,具体还要根据自己的项目要求来选择)言归正传,现在就说说百度的定位API 吧 (该文章,主要是讲定位的,所以就单独说说,百度的定位API 接口)百度定位API,是 GPS 基站 WIFI IP混合定位,...

    基于 Android 实现模拟地图定位功能【100012418】

    手机定位方式目前有4种:基站定位,WIFI定位,GPS定位,AGPS定位。本工程利用手机自带的"模拟位置"功能实现运行时修改LocationManager结果。

    后台定时定位开机自启系统.zip

    (第三方地图API的选择,具体还要根据自己的项目要求来选择)言归正传,现在就说说百度的定位API 吧 (该文章,主要是讲定位的,所以就单独说说,百度的定位API 接口)百度定位API,是 GPS 基站 WIFI IP混合定位,...

    基于MEMS传感器的煤矿井下人员定位系统设计

    通过井下已有的WiFi基站,并结合行人航迹推算算法实现井下人员的精确定位:利用融合了行走频率和加速度方差的表达式来确定步长,采用四元数法估计行人方向角,并根据扩展卡尔曼滤波对方向角的原始数据进行修正,从而获得...

    android wifi信号强度等级区分的修改介绍

    您可能感兴趣的文章:Android获取当前已连接的wifi信号强度的方法在Android里完美实现基站和WIFI定位android开发教程之wifi开发示例Android开发之Wifi基础教程Android判断是Wifi还是4G网络代码Android连接指定Wifi的...

    室内定位技术分析

    室内定位是指在室内环境中实现位置定位,主要采用无线通讯、基站定位、惯导定位等多种技术集成形成一套室内位置定位体系,从而实现人员、物体等在室内空间中的位置监控。

    Android开发组之地图定位实习报告.pptx

    国内目前用得最多的地图就是百度与高德,直接调用百度地图定位SDK,准确定位 百度定位SDK GPS定位、基站定位和wifi定位 三种定位方式 没有添加经纬度的显示数据,定位的原点图标未能更换成功 不足之处 GPS定位 6 四...

    信息化矿灯及调度管理系统的研制与应用

    整个系统结合了RFID模块的快速定位和WIFI模块的通讯,实现了人员定位及其活动轨迹在电子地图上的显示、下井考勤、即时语音调度、短信调度、照相及传输功能。经过工业性试验得出:系统在人员定位、考勤、文字和语音通信...

    在iOS App中实现地理位置定位的基本方法解析

    iOS系统自带的定位服务可以实现很多需求。比如:获取当前经纬度,获取当前位置信息等等。 其定位有3种方式: 1,GPS,最精确的定位方式 2,蜂窝基站三角定位,这种定位在信号基站比较秘籍的城市比较准确。 3,Wifi,...

    安卓源码包 Android GPS 开发 地图&导航&定位&指南等设计代码合集 (45个).zip

    安卓调用百度地图,实现定位和搜索功能.rar 完美!SlidingMenu jar包版demo!不用导包!兼容2.2.zip 指南针安卓端源码.rar 水平方向伸缩的path按钮菜单,非弧形.rar 百度地图定位,显示周围的人,类似于E代驾的首页...

    移动智能终端开发技术 本文链接:https://blog.csdn.net/Candy5204/article/details/

    (1)使用GPS定位系统,可以精确定位你当前的地理位置,但由于GPS接收机需要对准天空才能工作,因此在室内环境基本无用。 (2)找到自己所在的位置的有效方法就是使用手机基站,当手机开机时会与周围的基站保持联系...

Global site tag (gtag.js) - Google Analytics