1.GPS定位 2.基站定位 此类位置的获取有赖于手机无线通讯信号,当手机处在信号覆盖范围内,手机可以获得该区域(即通讯术语中的“小区”)的识别号。因为这些识别号是惟一的,因此可以将识别号和地理坐标对应起来,因此根据识别号就可以知道地理位置。但是误差比较大。 MCC(Mobile Country Code)、MNC(Mobile Network Code)、LAC(Location Aera Code)、CID(Cell Tower ID)是通讯业内的名词。MCC标识国家,MNC标识网络,两者组合起来则唯一标识一家通讯运营商。从维基百科上了解到,一个国家的MCC不唯一,例如中国有460和461,一家运营商也不只一个MNC,例如中国移动有00、02、07。LAC标识区域,类似于行政区域,运营商将大区域划分成若干小区域,每个区域分配一个LAC。CID标识基站,若手机处在工作状态,则必须要和一个通讯基站进行通讯,通过CID就可以确定手机所在的地理范围。 在Android当中,大部分和通讯网络相关的信息都需要经过一项系统服务,即TelephoneManager来获得。

TelephonyManager mTelMan = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String operator = mTelMan.getNetworkOperator();
String mcc = operator.substring(0, 3);
String mnc = operator.substring(3);
GsmCellLocation location = (GsmCellLocation) mTelMan.getCellLocation();
int cid = location.getCid();
int lac = location.getLac();

通过上面的方法,可获得MCC、MNC、CID、LAC,对照Geolocation API Network Protocol,剩下不多的参数也可以获得,发起请求后根据响应内容即可得到地理位置信息。 请求(Request)的信息如下:

{"cell_towers":[{"mobile_network_code":"00","location_area_code":9733,
"mobile_country_code":"460","cell_id":17267},{"mobile_network_code":"00","location_area_code":9733,
"mobile_country_code":"460","cell_id":27852},{"mobile_network_code":"00","location_area_code":9733,
"mobile_country_code":"460","cell_id":27215},{"mobile_network_code":"00","location_area_code":9733,
"mobile_country_code":"460","cell_id":27198},{"mobile_network_code":"00","location_area_code":9484,
"mobile_country_code":"460","cell_id":27869},{"mobile_network_code":"00","location_area_code":9508,
"mobile_country_code":"460","cell_id":37297},{"mobile_network_code":"00","location_area_code":9733,
"mobile_country_code":"460","cell_id":27888}],
"host":"maps.google.com",
"version":"1.1.0"}

响应(Response)的信息如下:

{"location":{"latitude":23.12488,"longitude":113.271907,"accuracy":630.0},"access_token":"2:61tEAW-rONCT1_W-:JVpp2_jq5a0L-5JK"}

3.WIFI定位 其原理是首先收集每个WIFI无线接入点的位置,对每个无线路由器进行唯一的标识,在数据库中注明这些接入点的具体位置。 使用时,一旦发现有WI-FI接入点,则进入到数据中查看匹配的记录,进而得到位置信息。 WIFI定位主要取决于节点(node)的物理地址(mac address)。与提供TelephoneManager一样,Android也提供了获取WIFI信息的接口:WifiManager。

WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiMan.getConnectionInfo();
String mac = info.getMacAddress();
String ssid = info.getSSID();

通过上面的方法,即可获得必要的请求参数。 发出的请求(Request)信息如下:

{"wifi_towers":[{"mac_address":"00:23:76:AC:41:5D","ssid":"Aspire-NETGEAR"}],"host":"maps.google.com","version":"1.1.0"}

响应(Response)的信息如下:

{"location":{"latitude":23.129075,"longitude":113.264423,"accuracy":140000.0},"access_token":"2:WRr36ynOz_d9mbw5:pRErDAmJXI8l76MU"}

以上简单介绍了android中获取位置的三种定位方式。 实际开发中可利用android.location中的LocationManager类,该类封装了地理位置信息的接口,提供了GPS_PROVIDER和 NETWORK_PROVIDER。 如果开发的应用需要高精确性,那么可使用GPS_PROVIDER,但这也意味着应用无法在室内使用,待机时间缩短,响应时间稍长等问题; 如果开发的应用需要快速反应,对精度要求不怎么高,并且要尽可能节省电量,那么使用NETWORK_PROVIDER是不错的选择。 这里提一下,还有一个 PASSIVE_PROVIDER,在实际应用中较少使用。 1.如下代码就是设置从Network中获取位置:

LocationManager mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);mLocMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, mLocLis);

需要对应的权限:android.permission.ACCESS_COARSE_LOCATION 2.如下代码就是设置从GPS获取位置:

LocationManager mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, mLocLis);

需要对应的权限:android.permission.ACCESS_FINE_LOCATION 如果代码里使用了两个 PROVIDER,则只需要一个权限即可:android.permission.ACCESS_FINE_LOCATION。 以下是整个过程的代码:

public class DemoActivity extends Activity {private static final String TAG = "DemoActivity";@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}public void onRequestLocation(View view) {
switch (view.getId()){
case R.id.gpsBtn:
Log.d(TAG, "GPS button is clicked");
requestGPSLocation();
break;
case R.id.telBtn:
Log.d(TAG, "CellID button is clicked");
requestTelLocation();
break;
case R.id.wifiBtn:
Log.d(TAG, "WI-FI button is clicked");
requestWIFILocation();
break;
case R.id.netBtn:
Log.d(TAG, "Network button is clicked");
requestNetworkLocation();
break;
}
}private void requestTelLocation() {
TelephonyManager mTelMan = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// MCC+MNC. Unreliable on CDMA networks
String operator = mTelMan.getNetworkOperator();
String mcc = operator.substring(0, 3);
String mnc = operator.substring(3);GsmCellLocation location = (GsmCellLocation) mTelMan.getCellLocation();
int cid = location.getCid();
int lac = location.getLac();JSONObject tower = new JSONObject();
try {
tower.put("cell_id", cid);
tower.put("location_area_code", lac);
tower.put("mobile_country_code", mcc);
tower.put("mobile_network_code", mnc);
} catch (JSONException e) {
Log.e(TAG, "call JSONObject's put failed", e);
}JSONArray array = new JSONArray();
array.put(tower);List<NeighboringCellInfo> list = mTelMan.getNeighboringCellInfo();
Iterator<NeighboringCellInfo> iter = list.iterator();
NeighboringCellInfo cellInfo;
JSONObject tempTower;
while (iter.hasNext()) {
cellInfo = iter.next();
tempTower = new JSONObject();
try {
tempTower.put("cell_id", cellInfo.getCid());
tempTower.put("location_area_code", cellInfo.getLac());
tempTower.put("mobile_country_code", mcc);
tempTower.put("mobile_network_code", mnc);
} catch (JSONException e) {
Log.e(TAG, "call JSONObject's put failed", e);
}
array.put(tempTower);
}JSONObject object = createJSONObject("cell_towers", array);
requestLocation(object);
}private void requestWIFILocation() {
WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiMan.getConnectionInfo();
String mac = info.getMacAddress();
String ssid = info.getSSID();JSONObject wifi = new JSONObject();
try {
wifi.put("mac_address", mac);
wifi.put("ssid", ssid);
} catch (JSONException e) {
e.printStackTrace();
}JSONArray array = new JSONArray();
array.put(wifi);JSONObject object = createJSONObject("wifi_towers", array);
requestLocation(object);
}private void requestLocation(JSONObject object) {
Log.d(TAG, "requestLocation: " + object.toString());
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.google.com/loc/json");
try {
StringEntity entity = new StringEntity(object.toString());
post.setEntity(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}try {
HttpResponse resp = client.execute(post);
HttpEntity entity = resp.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer buffer = new StringBuffer();
String result = br.readLine();
while (result != null) {
buffer.append(result);
result = br.readLine();
}Log.d(TAG, buffer.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}private JSONObject createJSONObject(String arrayName, JSONArray array) {
JSONObject object = new JSONObject();
try {
object.put("version", "1.1.0");
object.put("host", "maps.google.com");
object.put(arrayName, array);
} catch (JSONException e) {
Log.e(TAG, "call JSONObject's put failed", e);
}
return object;
}private void requestGPSLocation() {
LocationManager mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60, 100, mLocLis);
}private void requestNetworkLocation() {
LocationManager mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 60, 100, mLocLis);
}private LocationListener mLocLis = new LocationListener() {@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, "onStatusChanged, provider = " + provider);
}@Override
public void onProviderEnabled(String provider) {
Log.d(TAG, "onProviderEnabled, provider = " + provider);
}@Override
public void onProviderDisabled(String provider) {
Log.d(TAG, "onProviderDisabled, provider = " + provider);
}@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Log.d(TAG, "latitude: " + latitude + ", longitude: " + longitude);
}
};
}

本篇文章来源于网络,尚未对其进行实践测试,因为实际应用的关系,这里仅对NetWork方式获取位置做了实践,请稳步 Android使用NetWork方式获取当前的地理位置 一文查看相关内容

Android获取当前位置的三种方式及其使用方法相关推荐

  1. 获取Class对象的三种方式

    获取Class对象的三种方式 Object --> getClass() 通过对象.getclass 任何数据类型(包括基本数据类型)都有一个"静态"的class属性 通过类 ...

  2. 获取class文件对象三种方式

    Class类 阅读API的Class类得知,Class 没有公共构造方法.Class 对象是在加载类时由 Java 虚拟机以及通过调用类加载器中的 defineClass 方法自动构造的 获取Clas ...

  3. 反射应用和获取Class对象的三种方式

    一.写一个"框架",可以创建任何对象运行任何方法 1.配置文件 2.使用类加载器ClassLoader,Properties集合是可以和IO流结合使用完成读取和写入数据的集合,方法 ...

  4. java反射之获取class对象,Java之反射机制(获取Class对象的三种方式)

    Java之反射机制(获取Class对象的三种方式) 开发工具与关键技术:MyEclipse 10,java 作者:刘东标 撰写时间:2019-06-14 如何得到各个字节码对应的实例对象? 每个类被加 ...

  5. 使用git下载项目到本地,指定本地文件夹位置的三种方式

    使用git下载项目到本地,指定本地文件夹位置的三种方式 使用VSCODE里的"克隆"功能直接粘贴项目链接即可选择本地想保存的位置. 使用git bash窗口下载项目之前,先切换到你 ...

  6. JS基础-Java Class类以及获取Class实例的三种方式

    JS基础-Java Class类以及获取Class实例的三种方式 由于JVM为每个加载的class创建了对应的Class实例,并在实例中保存了该class的所有信息,包括类名.包名.父类.实现的接口. ...

  7. Android获取电池电量信息的几种方式

    Android获取电池电量电量信息的几种方式 本文仅为新手提供知识学习,不喜勿喷. 关于Android获取电量信息我汇总了当前采用的比较多且通俗易懂的三种方式,如果对你有用的话请在评论区留言并给我点个 ...

  8. webservice服务器端获取request对象的三种方式

    有的时候在webservice里我们需要获取request对象和response对象,比如想要获得客户端的访问ip的时候就需要这么做,下面说三种方式,当然三种方式可能是针对不同方式部署webservi ...

  9. Struts2-从值栈获取list集合数据(三种方式)

    创建User封装数据类 public class User {private String username;private String password;public String getPass ...

最新文章

  1. SAP MM 工序委外流程初探
  2. BZOJ3172 [Tjoi2013]单词 字符串 SA ST表
  3. linux下scp远程拷贝文件无需输入密码工具之expect
  4. es6学习笔记(一)
  5. PostgreSQL Huge Page 使用建议 - 大内存主机、实例注意
  6. WhateverOrigin –与Heroku和Play对抗相同的原产地政策! 构架
  7. 对数据仓库进行数据建模_确定是否可以对您的数据进行建模
  8. 贪心算法 -- 最小延迟调度
  9. 在拦截器里放入参数 controller_干货|SpringMVC拦截器的使用详解
  10. shell 免杀aspx_记一次aspx网站渗透
  11. 敏捷开发框架_测试经理必知必会:敏捷开发3355原则
  12. python邮件发送 STMP
  13. (openCV 十二)图像增强(对数变换/伽马变换/分段线性变换)
  14. HTML5软件设计大赛,我院成功举行第十七届山东省大学生软件设计大赛 HTML5创意应用命题决赛...
  15. 泡泡堂、QQ堂游戏通信架构分析
  16. 初创企业购买企业邮箱_应用创意可为2019年及以后的初创企业带来收入
  17. 无法访问windows安装服务_最好用的内外网测速工具, speedtest 服务器搭建指南
  18. 上半年、你学到了什么?
  19. 通信电子电路(4)----高频功率放大器(2)
  20. GFP-GAN论文阅读笔记

热门文章

  1. c语言 游戏程序,C语言做的推箱子游戏源程序
  2. adcclk最大_TMS320F28xxADC配置说明中文版
  3. failover.mysql_mysqlfailover测试
  4. mysql系列问答题_(2)MySQL运维基础知识面试问答题
  5. linux用户组建立,查看等
  6. mysql插入timeStamp类型数据时间相差8小时的解决办法
  7. 婚纱照嘴巴有点凸好p吗_丑拒80寸奢华大片挂床头,压箱底的婚纱照还能这样摆?...
  8. <深入剖析Tomcat>摘抄
  9. 取出响应头中包含多个set-cookie的值
  10. C#winform定时器的两种使用方法