http://blog.csdn.net/yaoyeyzq/article/details/7885236
这里给大家分享下基站定位的实现,基站定位首先要通过TelephonyManager得到手机的信号信息,比如基站的国家编码,小区id等......得到这些后需要向google提供的接口提交这些参数,然后就会返回基站的相关信息。
废话不多说直接上代码吧:
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.List;
- import android.content.Context;
- import android.telephony.NeighboringCellInfo;
- import android.telephony.TelephonyManager;
- import android.telephony.cdma.CdmaCellLocation;
- import android.telephony.gsm.GsmCellLocation;
- import android.util.Log;
- /**
- * @author yangzhiqiang
- *
- */
- public class CellIdInfoManager implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 5481154371450408380L;
- private Context context;
- public CellIdInfoManager(Context context) {
- super();
- this.context = context;
- }
- public List<CellInfo> getCellInfo() {
- List<CellInfo> listInfo = new ArrayList<CellInfo>();
- int countryCode;
- int networkCode;
- int areaCode;
- CellInfo info = new CellInfo();
- GsmCellLocation gsm = null;
- TelephonyManager manager = (TelephonyManager) context
- .getSystemService(Context.TELEPHONY_SERVICE);
- if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
- gsm = (GsmCellLocation) manager.getCellLocation();
- if (gsm == null) {
- return null;
- }
- if (manager.getNetworkOperator() == null
- || manager.getNetworkOperator().length() == 0) {
- return null;
- }
- countryCode = Integer.parseInt(manager.getNetworkOperator()
- .substring(0, 3));
- networkCode = Integer.parseInt(manager.getNetworkOperator()
- .substring(3, 5));
- areaCode = gsm.getLac();
- info.cellId = gsm.getCid();
- info.mobileCountryCode = countryCode;
- info.mobileNetworkCode = networkCode;
- info.locationAreaCode = areaCode;
- info.radio_type = "gsm";
- listInfo.add(info);
- List<NeighboringCellInfo> list = manager.getNeighboringCellInfo();
- for (NeighboringCellInfo i : list) {
- CellInfo ci = new CellInfo();
- ci.cellId = i.getCid();
- ci.mobileCountryCode = countryCode;
- ci.mobileNetworkCode = networkCode;
- ci.locationAreaCode = areaCode;
- ci.radio_type = "gsm";
- listInfo.add(ci);
- }
- } else if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
- CdmaCellLocation cdma = (CdmaCellLocation) manager
- .getCellLocation();
- if (cdma == null) {
- return null;
- }
- if (manager.getNetworkOperator() == null
- || manager.getNetworkOperator().length() == 0) {
- return null;
- }
- Log.v("TAG", "CDMA");
- info.cellId = cdma.getBaseStationId();
- info.mobileCountryCode = Integer.parseInt(manager
- .getNetworkOperator());
- info.mobileNetworkCode = cdma.getSystemId();
- info.locationAreaCode = cdma.getNetworkId();
- info.radio_type = "cdma";
- listInfo.add(info);
- }
- return listInfo;
- }
- public class CellInfo {
- // 基站编号
- public int cellId;
- // 国家代码
- public int mobileCountryCode;
- // 网络代码
- public int mobileNetworkCode;
- // 区域代码
- public int locationAreaCode;
- public String radio_type;
- public CellInfo() {
- super();
- }
- }
- }
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
/**
* @author yangzhiqiang
*
*/
public class CellIdInfoManager implements Serializable {
/**
*
*/
private static final long serialVersionUID = 5481154371450408380L;
private Context context;
public CellIdInfoManager(Context context) {
super();
this.context = context;
}
public List<CellInfo> getCellInfo() {
List<CellInfo> listInfo = new ArrayList<CellInfo>();
int countryCode;
int networkCode;
int areaCode;
CellInfo info = new CellInfo();
GsmCellLocation gsm = null;
TelephonyManager manager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
gsm = (GsmCellLocation) manager.getCellLocation();
if (gsm == null) {
return null;
}
if (manager.getNetworkOperator() == null
|| manager.getNetworkOperator().length() == 0) {
return null;
}
countryCode = Integer.parseInt(manager.getNetworkOperator()
.substring(0, 3));
networkCode = Integer.parseInt(manager.getNetworkOperator()
.substring(3, 5));
areaCode = gsm.getLac();
info.cellId = gsm.getCid();
info.mobileCountryCode = countryCode;
info.mobileNetworkCode = networkCode;
info.locationAreaCode = areaCode;
info.radio_type = "gsm";
listInfo.add(info);
List<NeighboringCellInfo> list = manager.getNeighboringCellInfo();
for (NeighboringCellInfo i : list) {
CellInfo ci = new CellInfo();
ci.cellId = i.getCid();
ci.mobileCountryCode = countryCode;
ci.mobileNetworkCode = networkCode;
ci.locationAreaCode = areaCode;
ci.radio_type = "gsm";
listInfo.add(ci);
}
} else if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
CdmaCellLocation cdma = (CdmaCellLocation) manager
.getCellLocation();
if (cdma == null) {
return null;
}
if (manager.getNetworkOperator() == null
|| manager.getNetworkOperator().length() == 0) {
return null;
}
Log.v("TAG", "CDMA");
info.cellId = cdma.getBaseStationId();
info.mobileCountryCode = Integer.parseInt(manager
.getNetworkOperator());
info.mobileNetworkCode = cdma.getSystemId();
info.locationAreaCode = cdma.getNetworkId();
info.radio_type = "cdma";
listInfo.add(info);
}
return listInfo;
}
public class CellInfo {
// 基站编号
public int cellId;
// 国家代码
public int mobileCountryCode;
// 网络代码
public int mobileNetworkCode;
// 区域代码
public int locationAreaCode;
public String radio_type;
public CellInfo() {
super();
}
}
}
上面是得到手机信号的信息,下面是将这些信息发送到google服务器并解析结果:
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.Serializable;
- import java.util.List;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import android.location.Location;
- import android.util.Log;
- import com.metarnet.gps.CellIdInfoManager.CellInfo;
- /**
- * @author Administrator
- *
- */
- public class NetworkLocationManager implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 1185788569820321281L;
- public static Location getBaseStationLocation(List<CellInfo> cellID) {
- if (cellID == null) {
- Log.i("TAG", "cellId is null.");
- return null;
- }
- DefaultHttpClient client = new DefaultHttpClient();
- HttpPost post = new HttpPost("http://www.google.com/loc/json");
- JSONObject holder = new JSONObject();
- try {
- CellInfo info = cellID.get(0);
- holder.put("version", "1.1.0");
- holder.put("host", "maps.google.com");
- holder.put("home_mobile_country_code", info.mobileCountryCode);
- holder.put("home_mobile_network_code", info.mobileNetworkCode);
- holder.put("request_address", true);
- holder.put("radio_type", info.radio_type);
- if ("460".equals(info.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", info.cellId);
- current_data.put("location_area_code", info.locationAreaCode);
- current_data.put("mobile_country_code", info.mobileCountryCode);
- current_data.put("mobile_network_code", info.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", info.cellId);
- data.put("location_area_code", info.locationAreaCode);
- data.put("mobile_country_code", info.mobileCountryCode);
- data.put("mobile_network_code", info.mobileNetworkCode);
- data.put("age", 0);
- array.put(data);
- }
- }
- holder.put("cell_towers", array);
- StringEntity se = new StringEntity(holder.toString());
- post.setEntity(se);
- HttpResponse resp = client.execute(post);
- int state = resp.getStatusLine().getStatusCode();
- if (state == HttpStatus.SC_OK) {
- HttpEntity entity = resp.getEntity();
- if (entity != null) {
- BufferedReader br = new BufferedReader(
- new InputStreamReader(entity.getContent()));
- StringBuffer sb = new StringBuffer();
- String resute = "";
- while ((resute = br.readLine()) != null) {
- sb.append(resute);
- }
- br.close();
- data = new JSONObject(sb.toString());
- data = (JSONObject) data.get("location");
- Location loc = new Location(
- android.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(System.currentTimeMillis());
- return loc;
- } else {
- return null;
- }
- } else {
- Log.v("TAG", state + "");
- return null;
- }
- } catch (Exception e) {
- Log.e("TAG", e.getMessage());
- return null;
- }
- }
- }
http://blog.csdn.net/yaoyeyzq/article/details/7885474
WIFI定位其实和基站定位都差不多,只需要把WIFI的MAC地址取到传给google就行了,下面是具体实现!
- import java.io.Serializable;
- import android.content.Context;
- import android.net.wifi.WifiManager;
- import android.util.Log;
- /**
- * @author yangzhiqiang
- *
- */
- public class WiFiInfoManager implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = -4582739827003032383L;
- private Context context;
- public WiFiInfoManager(Context context) {
- super();
- this.context = context;
- }
- public WifiInfo getWifiInfo() {
- WifiManager manager = (WifiManager) context
- .getSystemService(Context.WIFI_SERVICE);
- WifiInfo info = new WifiInfo();
- info.mac = manager.getConnectionInfo().getBSSID();
- Log.i("TAG", "WIFI MAC is:" + info.mac);
- return info;
- }
- public class WifiInfo {
- public String mac;
- public WifiInfo() {
- super();
- }
- }
- }
import java.io.Serializable;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.util.Log;
/**
* @author yangzhiqiang
*
*/
public class WiFiInfoManager implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4582739827003032383L;
private Context context;
public WiFiInfoManager(Context context) {
super();
this.context = context;
}
public WifiInfo getWifiInfo() {
WifiManager manager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = new WifiInfo();
info.mac = manager.getConnectionInfo().getBSSID();
Log.i("TAG", "WIFI MAC is:" + info.mac);
return info;
}
public class WifiInfo {
public String mac;
public WifiInfo() {
super();
}
}
}
上面是取到WIFI的mac地址的方法,下面是把地址发送给google服务器,代码如下:
- public static Location getWIFILocation(WifiInfo wifi) {
- if (wifi == null) {
- Log.i("TAG", "wifi is 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");
- JSONObject data;
- JSONArray array = new JSONArray();
- if (wifi.mac != null && wifi.mac.trim().length() > 0) {
- data = new JSONObject();
- data.put("mac_address", wifi.mac);
- data.put("signal_strength", 8);
- data.put("age", 0);
- array.put(data);
- }
- holder.put("wifi_towers", array);
- Log.i("TAG", "request json:" + holder.toString());
- StringEntity se = new StringEntity(holder.toString());
- post.setEntity(se);
- HttpResponse resp = client.execute(post);
- int state = resp.getStatusLine().getStatusCode();
- if (state == HttpStatus.SC_OK) {
- HttpEntity entity = resp.getEntity();
- if (entity != null) {
- BufferedReader br = new BufferedReader(
- new InputStreamReader(entity.getContent()));
- StringBuffer sb = new StringBuffer();
- String resute = "";
- while ((resute = br.readLine()) != null) {
- sb.append(resute);
- }
- br.close();
- Log.i("TAG", "response json:" + sb.toString());
- data = new JSONObject(sb.toString());
- data = (JSONObject) data.get("location");
- Location loc = new Location(
- android.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(System.currentTimeMillis());
- return loc;
- } else {
- return null;
- }
- } else {
- Log.v("TAG", state + "");
- return null;
- }
- } catch (Exception e) {
- Log.e("TAG", e.getMessage());
- return null;
- }
- }