转载:
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;
}
注备:由于看到这篇文章极好,所以转载过来。
分享到:
相关推荐
本资源“androidgps基站与WIFI定位源码.zip”提供了一套实现GPS、基站和Wi-Fi定位功能的源代码,非常适合Android开发者学习和参考。 首先,我们要理解Android系统中的定位机制。Android提供了Location API,这是一...
Android WiFi LBS(Location-Based Services,基于位置的服务)定位是通过结合WiFi网络信号和移动通信基站信息来确定设备的位置。这种定位方式在GPS信号不可用或者信号弱的室内环境尤为实用。下面将详细介绍Android ...
2. WIFI定位:WIFI定位利用已知的WIFI热点数据库,通过比较设备当前接收到的WIFI信号,匹配数据库中记录的信号特征来确定位置。在Android中,使用`WifiManager`类和`LocationManager`服务结合,通过`addWifiListener...
在Android平台上,实现精准的位置服务是开发者们常常面临的需求,这通常涉及到三种主要的定位技术:基站定位、Wi-Fi定位和GPS(全球定位系统)定位。这篇内容将深入探讨这些定位方式以及它们在源码中的实现。 1. **...
使用GPS和WIFI定位功能需要在AndroidManifest.xml中声明相应的权限: - `<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />`: 允许访问精细(GPS)位置信息。 - `<uses-permission ...
在Android系统中,GPS(全球定位系统)和WIFI基站定位是两种常见的定位技术,用于获取设备的地理位置信息。这份“androidGPS及WIFI基站定位坐标源码.zip”压缩包包含了一些实现这两种定位方式的源代码,对于学习...
在Android系统中,基站和WIFI定位是两种重要的位置服务技术,它们可以帮助应用程序获取设备的地理位置信息,即使在没有GPS的情况下也能实现定位。对于OPhone和部分国产Android定制机,由于默认不支持基站和WIFI定位...
Android的LocationManager同样支持WiFi定位,只需指定相应的PROVIDER即可。 基站定位,也称为Cell ID定位,是通过识别手机连接的移动通信基站来确定位置。当GPS信号弱或不可用时,这种方法可以提供粗略的位置信息。...
本文将详细介绍基于Android的三种定位方式:GPS(全球定位系统)、WiFi网络定位和基站定位,并结合提供的源码资源进行深入解析。 首先,GPS是全球卫星导航系统,用于提供精确的地理位置信息。`Android实现GPS定位....
在Android开发中,GPS(全球定位系统)和基站定位是两种常见的定位技术,它们用于获取设备的地理位置信息。本文将详细讲解这两种定位方式,并结合Android Studio的开发实践,阐述如何在实际应用中实现它们。 首先,...
这是一个由百度提供的服务,它为开发者提供了丰富的API接口,用于获取用户的位置信息,包括GPS定位、基站定位以及WiFi定位等。使用该SDK,开发者可以在Android应用中轻松集成定位功能,获取到用户当前的经纬度坐标,...
在Android平台上,Google提供了一套全面的定位服务,使得开发者能够轻松实现基于GPS、Wi-Fi基站和移动网络的综合定位功能。这篇文档将详细介绍如何在Android应用中利用这些技术进行定位。 首先,我们需要理解...
在Android系统中,定位服务是应用开发者不可或缺的功能之一,它主要依赖于三种技术:基站定位、Wi-Fi定位和GPS(全球定位系统)定位。这三种技术各有特点,互相补充,共同构建了Android设备的综合定位能力。 **基站...
在Android系统中,实现基于WiFi和基站的地理信息定位是一个重要的功能,这涉及到网络定位服务的集成和使用。本文将详细解析如何通过编程实现这一功能,主要涉及的技术点包括WiFi扫描、基站信息获取以及地理定位API的...
这份"Android应用源码之androidGPS及WIFI基站定位坐标源码"提供了实现这两种定位方式的详细示例,帮助开发者更好地理解和运用到自己的项目中。 一、Android GPS定位 Android系统通过Google Play服务提供的Location ...
本资源包含的"androidGPS及WIFI基站定位坐标源码"提供了实现这些功能的具体代码实现,对于开发者来说是很好的学习和参考材料。 GPS定位是通过接收来自多个GPS卫星的信号来计算设备的位置。Android系统提供了...
总结,理解并熟练运用GPS、GPS与Java的结合、WiFi定位和基站定位是开发移动应用时的重要技能。它们各自有优缺点,但结合起来可以实现更全面、更精准的定位服务,为用户提供丰富的地理位置相关功能。
论文应涵盖GPS和WIFI定位的基本原理,Android API的使用,定位性能比较,以及可能遇到的问题和解决方案。 总的来说,这份源码提供了学习Android定位技术的良好实例,有助于提升开发者对移动定位的理解和应用能力。...
【标题】中的“安卓Android源码——androidGPS及WIFI基站定位坐标源码”表明了这是一个关于Android系统中GPS和Wi-Fi基站定位功能的源代码集合。在Android平台上,定位服务是应用程序能够获取用户当前位置的关键组件...
Android GPS+基站+WiFi热点多渠道定位模块的Demo,采用Google Geo Location API+谷歌gcm SDK/Android定位方式,请翻墙后期测试效果Alx位置管理器Android GPS+基站+WiFi热点多渠道定位的Demo,使用Google Geo ...