现在的位置: 首页 > 综合 > 正文

获取地理地位

2013年02月07日 ⁄ 综合 ⁄ 共 13390字 ⁄ 字号 评论关闭

在研究赶集网的客户端发现,程序会打开GPS,好奇,研究了:

参考文章:下面的代码,在设置中打开开关的基础上,可以打开GPS,但一直没获取到位置。

http://dev.10086.cn/cmdn/wiki/index.php?doc-view-4283.html

 

      1 import android.app.Activity;

  2 import android.content.Context;

  3 import android.location.Criteria;

  4 import android.location.Location;

  5 import android.location.LocationListener;

  6 import android.location.LocationManager;

  7 import android.os.Bundle;

  8 import android.util.Log;

  9

  10 public class audio extends Activity

  11 {

  12

  13 /** Called when the activity is first created. */

  14 LocationManager locationManager;

  15 LocationListener llistener;

  16 String provider;

  17

  18 public void onCreate(Bundle savedInstanceState)

  19 {

  20 super.onCreate(savedInstanceState);

  21 setContentView(R.layout.main);

  22

  23 Criteria criteria = new Criteria();

  24 criteria.setAccuracy(Criteria.ACCURACY_FINE);

  25 criteria.setAltitudeRequired(false);

  26 criteria.setBearingRequired(false);

  27 criteria.setCostAllowed(true);

  28 criteria.setPowerRequirement(Criteria.POWER_LOW);

  29 String serviceName = Context.LOCATION_SERVICE;

  30 locationManager = (LocationManager) getSystemService(serviceName);

  31 locationManager.setTestProviderEnabled("gps", true);//这句在真机上运行,报异常,屏蔽掉

  32 provider = locationManager.getBestProvider(criteria, true);

  33 Log.d("provider", provider);

  34

  35 llistener = new LocationListener() {

  36 @Override

  37 public void onLocationChanged(Location location)

  38 {

  39 // TODO Auto-generated method stub

  40 Log.i("onLocationChanged", "come in");

41 if (location != null)

  42 {

  43 Log.w("Location", "Current altitude = "

  44 + location.getAltitude());

  45 Log.w("Location", "Current latitude = "

  46 + location.getLatitude());

  47 }

  48 locationManager.removeUpdates(this);

  49 locationManager.setTestProviderEnabled(provider, false);

  50 }

  51

  52 @Override

  53 public void onProviderDisabled(String provider)

  54 {

  55 // TODO Auto-generated method stub

  56 Log.i("onProviderDisabled", "come in");

  57

  58 }

  59

  60 @Override

  61 public void onProviderEnabled(String provider)

  62 {

  63 // TODO Auto-generated method stub

  64 Log.i("onProviderEnabled", "come in");

  65 }

  66

  67 @Override

  68 public void onStatusChanged(String provider, int status,

  69 Bundle extras)

  70 {

  71 // TODO Auto-generated method stub

  72 Log.i("onStatusChanged", "come in");

  73

  74 }

  75

  76 };

  77 locationManager.requestLocationUpdates(provider, 1000, (float) 1000.0,
llistener);

  78 }

  79

  80 protected void onDestroy()

  81 {

  82 locationManager.removeUpdates(llistener);

  83 locationManager.setTestProviderEnabled(provider, false);

  84 super.onDestroy();

  85 }

 

获取位置还可以通过  基站、wifi来定位,这些方法都要借助google的接口

http://www.apkcode.com/html/2011/maps_0218/326.html

 

CellIDInfo.java 封装了cellid的信息

  Java代码

  public class CellIDInfo {

  public int cellId;

  public String mobileCountryCode;

  public String mobileNetworkCode;

  public int locationAreaCode;

  public String radioType;

  public CellIDInfo(){}

  }

  public class CellIDInfo {

  public int cellId;

  public String mobileCountryCode;

  public String mobileNetworkCode;

  public int locationAreaCode;

  public String radioType;

  public CellIDInfo(){}

  }

  WifiInfo.java 封装了wifi的信息

  Java代码

  public class WifiInfo {

  public String mac;

  public WifiInfo(){}

  }

  public class WifiInfo {

  public String mac;

  public WifiInfo(){}

  }

  CellIDInfoManager.java 可获取所有的CellIDInfo

  Java代码

  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 getCellIDInfo(Context context){

  manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

  listener = new PhoneStateListener();

  manager.listen(listener, 0);

  ArrayList CellID = new ArrayList();

  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 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;

  }

  }

  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 getCellIDInfo(Context context){

  manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

  listener = new PhoneStateListener();

  manager.listen(listener, 0);

  ArrayList CellID = new ArrayList();

  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 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信息。

  Java代码

  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 wifi = new ArrayList();

  wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

  WifiInfo info = new WifiInfo();

  info.mac = wm.getConnectionInfo().getBSSID();

  wifi.add(info);

  return 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 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来获取经纬度

  Java代码

  private Location callGear(ArrayList wifi,

  ArrayList 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;

  }

  private Location callGear(ArrayList wifi,

  ArrayList 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都来得更慢。

抱歉!评论已关闭.