Android 的三种定位方式
转载自:http://blog.csdn.net/luosiye312/article/details/50562309#comments
Android 定位大致分为三大类:GPS定位;Network定位;AGPS定位。而Network又细分为WIFI定位和基站定位。下面详细讲解每种定位:
Android GPS:需要GPS硬件支持,直接和卫星交互来获取当前经纬度。
优点:速度快、精度高、可在无网络情况下使用。
缺点:首次连接时间长、只能在户外已经开阔地使用,设备上方有遮挡物就不行了、比较耗电。
代码:
- /**
- * 通过LocationListener来获取Location信息
- */
- public static void formListenerGetLocation(){
- locationManager = (LocationManager)mActivity.getSystemService(Context.LOCATION_SERVICE);
- locationListener = new LocationListener() {
- @Override
- public void onLocationChanged(Location location) {
- //位置信息变化时触发
- Log4Lsy.i(TAG, ”纬度:”+location.getLatitude());
- Log4Lsy.i(TAG, ”经度:”+location.getLongitude());
- Log4Lsy.i(TAG, ”海拔:”+location.getAltitude());
- Log4Lsy.i(TAG, ”时间:”+location.getTime());
- }
- @Override
- public void onStatusChanged(String provider, int status,Bundle extras) {
- //GPS状态变化时触发
- }
- @Override
- public void onProviderEnabled(String provider) {
- //GPS禁用时触发
- }
- @Override
- public void onProviderDisabled(String provider) {
- //GPS开启时触发
- }
- };
- /**
- * 绑定监听
- * 参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种,前者是GPS,后者是GPRS以及WIFI定位
- * 参数2,位置信息更新周期.单位是毫秒
- * 参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息
- * 参数4,监听
- * 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新
- */
- locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
- }
/*** 通过LocationListener来获取Location信息*/public static void formListenerGetLocation(){locationManager = (LocationManager)mActivity.getSystemService(Context.LOCATION_SERVICE);locationListener = new LocationListener() {@Overridepublic void onLocationChanged(Location location) {//位置信息变化时触发Log4Lsy.i(TAG, "纬度:"+location.getLatitude());Log4Lsy.i(TAG, "经度:"+location.getLongitude());Log4Lsy.i(TAG, "海拔:"+location.getAltitude());Log4Lsy.i(TAG, "时间:"+location.getTime());}@Overridepublic void onStatusChanged(String provider, int status,Bundle extras) {//GPS状态变化时触发}@Overridepublic void onProviderEnabled(String provider) {//GPS禁用时触发}@Overridepublic void onProviderDisabled(String provider) {//GPS开启时触发 }};/*** 绑定监听* 参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种,前者是GPS,后者是GPRS以及WIFI定位* 参数2,位置信息更新周期.单位是毫秒* 参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息* 参数4,监听* 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新*/locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);}
基站定位:基站定位的方式有多种,一般手机附近的三个基站进行三角定位,由于每个基站的位置是固定的,利用电磁波在这三个基站间中转所需要时间来算出手机所在的坐标,还有一种则是,利用电磁波在这三个基站间中转所需要时间来算出手机所在的坐标;第二种则是利用获取最近的基站的信息,其中包括基站 id,location area code、mobile country code、mobile network code和信号强度,将这些数据发送到google的定位web服务里,就能拿到当前所在的位置信息。
优点:受环境的影响情况较小,不管在室内还是人烟稀少的地方都能用,只要有基站。
缺点: 首先需要消耗流量、其实精度没有GPS那么准确,大概在十几米到几十米之间、
代码:
- /**
- * 主动获取Location,通过以下方法获取到的是最后一次定位信息。
- * 注意:Location location=new Location(LocationManager.GPS_PROVIDER)方式获取的location的各个参数值都是为0。
- */
- public static void getLocation(){
- locationManager = (LocationManager)mActivity.getSystemService(Context.LOCATION_SERVICE);
- Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- Log4Lsy.i(TAG, ”纬度:”+location.getLatitude());
- Log4Lsy.i(TAG, ”经度:”+location.getLongitude());
- Log4Lsy.i(TAG, ”海拔:”+location.getAltitude());
- Log4Lsy.i(TAG, ”时间:”+location.getTime());
- }
/*** 主动获取Location,通过以下方法获取到的是最后一次定位信息。* 注意:Location location=new Location(LocationManager.GPS_PROVIDER)方式获取的location的各个参数值都是为0。*/public static void getLocation(){locationManager = (LocationManager)mActivity.getSystemService(Context.LOCATION_SERVICE);Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);Log4Lsy.i(TAG, "纬度:"+location.getLatitude());Log4Lsy.i(TAG, "经度:"+location.getLongitude());Log4Lsy.i(TAG, "海拔:"+location.getAltitude());Log4Lsy.i(TAG, "时间:"+location.getTime());}
WIFI定位:Wifi定位是根据一个固定的WifiMAC地址,通过收集到的该Wifi热点的位置,然后访问网络上的定位服务以获得经纬度坐标。
优点:和基站定位一样,它的优势在于收环境影响较小,只要有Wifi的地方可以使用。
缺点:需要有wifi、精度不准。
代码:
- /**
- * 通过WIFI获取定位信息
- */
- public static void fromWIFILocation(){
- //WIFI的MAC地址
- WifiManager manager = (WifiManager)mActivity.getSystemService(Context.WIFI_SERVICE);
- String wifiAddress = manager.getConnectionInfo().getBSSID();
- //根据WIFI信息定位
- 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(wifiAddress!=null&&!wifiAddress.equals(“”)){
- data = new JSONObject();
- data.put(”mac_address”, wifiAddress);
- data.put(”signal_strength”, 8);
- data.put(”age”, 0);
- array.put(data);
- }
- holder.put(”wifi_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(LocationManager.NETWORK_PROVIDER);
- loc.setLatitude((Double)data.get(”latitude”));
- loc.setLongitude((Double)data.get(”longitude”));
- //其他信息同样获取方法
- Log4Lsy.i(TAG, ”latitude ===== ”+(Double)data.get(“latitude”));
- Log4Lsy.i(TAG, ”longitude ===== ”+(Double)data.get(“longitude”));
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
/*** 通过WIFI获取定位信息*/public static void fromWIFILocation(){//WIFI的MAC地址WifiManager manager = (WifiManager)mActivity.getSystemService(Context.WIFI_SERVICE); String wifiAddress = manager.getConnectionInfo().getBSSID(); //根据WIFI信息定位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(wifiAddress!=null&&!wifiAddress.equals("")){data = new JSONObject(); data.put("mac_address", wifiAddress); data.put("signal_strength", 8); data.put("age", 0); array.put(data); }holder.put("wifi_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(LocationManager.NETWORK_PROVIDER);loc.setLatitude((Double)data.get("latitude")); loc.setLongitude((Double)data.get("longitude"));//其他信息同样获取方法Log4Lsy.i(TAG, "latitude ===== "+(Double)data.get("latitude"));Log4Lsy.i(TAG, "longitude ===== "+(Double)data.get("longitude"));}}} catch (Exception e) {e.printStackTrace();}}
关于WIFI定位,此处有一篇非常详尽的文章,大家可参考此文章: http://www.jb51.net/article/52673.htm
AGPS定位:AssistedGPS(辅助全球卫星定位系统), 是结合GSM或GPRS与传统卫星定位,利用基地台代送辅助卫星信息,以缩减GPS芯片获取卫星信号的延迟时间,受遮盖的室内也能借基地台讯号弥补,减轻GPS芯片对卫星的依赖度。和纯GPS、基地台三角定位比较,AGPS能提供范围更广、更省电、速度更快的定位服务,理想误差范围在10公尺以内,日本和美国都已经成熟运用AGPS于LBS服务(Location Based Service,基于位置的服务)。AGPS技术是一种结合了网络基站信息和GPS信息对移动台进行定位的技术,可以在GSM/GPRS、WCDMA和CDMA2000网络中使进行用。该技术需要在手机内增加GPS接收机模块,并改造手机的天线,同时要在移动网络上加建位置服务器、差分GPS基准站等设备。AGPS解决方案的优势主要体现在其定位精度上,在室外等空旷地区,其精度在正常的GPS工作环境下,可以达到10米左右,堪称目前定位精度最高的一种定位技术。该技术的另一优点为:首次捕获GPS信号的时间一般仅需几秒,不像GPS的首次捕获时间可能要1分多钟,但很明显,他的硬件要求很高,造价自然高。
代码:AGPS的定位原理和GPS原理是差不多的,所以代码其实也是用GPS的定位方式就OK了。
最后,把我写的LocationUtil类贴上。
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Iterator;
- 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.annotation.SuppressLint;
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.location.GpsSatellite;
- import android.location.GpsStatus;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.net.wifi.WifiManager;
- import android.os.Build;
- import android.os.Bundle;
- import android.provider.Settings;
- import com.function.utils.Log4Lsy;
- /**
- * Android API本身提供的定位功能,也是GPS定位。
- * GPS定位,是基于卫星定位。它受环境影响很大。并且是单向定位,也就是只有你自己知道你的地理坐标。
- * @author LuoSiye
- */
- @TargetApi(Build.VERSION_CODES.CUPCAKE)
- @SuppressLint(“NewApi”)
- public class LocationUtil{
- private static final String TAG = “LocationUtil”;
- private static LocationUtil instance;
- private static Activity mActivity;
- private static LocationManager locationManager;
- private static LocationListener locationListener;
- public static LocationUtil getInstance(Activity activity){
- mActivity = activity;
- if(instance==null){
- instance = new LocationUtil();
- }
- locationManager = (LocationManager)mActivity.getSystemService(Context.LOCATION_SERVICE);
- return instance;
- }
- /**
- * 判断GPS导航是否打开.
- * false:弹窗提示打开,不建议采用在后台强行开启的方式。
- * true:不做任何处理
- * @return
- */
- public static void isOpenGPS(){
- if (!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)){
- AlertDialog.Builder dialog = new AlertDialog.Builder(mActivity);
- dialog.setMessage(”GPS未打开,是否打开?”);
- dialog.setPositiveButton(”确定”, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- // 设置完成后返回到原来的界面
- mActivity.startActivityForResult(intent, 0);
- }
- });
- dialog.setNegativeButton(”取消”, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- dialog.show();
- }
- }
- /**
- * 通过LocationListener来获取Location信息
- */
- public static void formListenerGetLocation(){
- locationListener = new LocationListener() {
- @Override
- public void onLocationChanged(Location location) {
- //位置信息变化时触发
- Log4Lsy.i(TAG, ”纬度:”+location.getLatitude());
- Log4Lsy.i(TAG, ”经度:”+location.getLongitude());
- Log4Lsy.i(TAG, ”海拔:”+location.getAltitude());
- Log4Lsy.i(TAG, ”时间:”+location.getTime());
- }
- @Override
- public void onStatusChanged(String provider, int status,Bundle extras) {
- //GPS状态变化时触发
- }
- @Override
- public void onProviderEnabled(String provider) {
- //GPS禁用时触发
- }
- @Override
- public void onProviderDisabled(String provider) {
- //GPS开启时触发
- }
- };
- /**
- * 绑定监听
- * 参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种,前者是GPS,后者是GPRS以及WIFI定位
- * 参数2,位置信息更新周期.单位是毫秒
- * 参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息
- * 参数4,监听
- * 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新
- */
- locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
- }
- /**
- * 主动获取Location,通过以下方法获取到的是最后一次定位信息。
- * 注意:Location location=new Location(LocationManager.GPS_PROVIDER)方式获取的location的各个参数值都是为0。
- */
- public static void getLocation(){
- Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- Log4Lsy.i(TAG, ”纬度:”+location.getLatitude());
- Log4Lsy.i(TAG, ”经度:”+location.getLongitude());
- Log4Lsy.i(TAG, ”海拔:”+location.getAltitude());
- Log4Lsy.i(TAG, ”时间:”+location.getTime());
- }
- /**
- * 获取GPS状态监听,包括GPS启动、停止、第一次定位、卫星变化等事件。
- */
- public static void getStatusListener(){
- GpsStatus.Listener listener = new GpsStatus.Listener(){
- @Override
- public void onGpsStatusChanged(int event) {
- if(event==GpsStatus.GPS_EVENT_FIRST_FIX){
- //第一次定位
- }else if(event==GpsStatus.GPS_EVENT_SATELLITE_STATUS){
- //卫星状态改变
- GpsStatus gpsStauts= locationManager.getGpsStatus(null); // 取当前状态
- int maxSatellites = gpsStauts.getMaxSatellites(); //获取卫星颗数的默认最大值
- Iterator<GpsSatellite> it = gpsStauts.getSatellites().iterator();//创建一个迭代器保存所有卫星
- int count = 0;
- while (it.hasNext() && count <= maxSatellites) {
- count++;
- GpsSatellite s = it.next();
- }
- Log4Lsy.i(TAG, ”搜索到:”+count+“颗卫星”);
- }else if(event==GpsStatus.GPS_EVENT_STARTED){
- //定位启动
- }else if(event==GpsStatus.GPS_EVENT_STOPPED){
- //定位结束
- }
- }
- };
- //绑定
- locationManager.addGpsStatusListener(listener);
- }
- /**
- * 获取所有卫星状态
- * @return
- */
- public static List<GpsSatellite> getGpsStatus(){
- List<GpsSatellite> result = new ArrayList<GpsSatellite>();
- GpsStatus gpsStatus = locationManager.getGpsStatus(null); // 取当前状态
- //获取默认最大卫星数
- int maxSatellites = gpsStatus.getMaxSatellites();
- //获取第一次定位时间(启动到第一次定位)
- int costTime=gpsStatus.getTimeToFirstFix();
- Log4Lsy.i(TAG, ”第一次定位时间:”+costTime);
- //获取卫星
- Iterable<GpsSatellite> iterable=gpsStatus.getSatellites();
- //一般再次转换成Iterator
- Iterator<GpsSatellite> itrator=iterable.iterator();
- int count = 0;
- while (itrator.hasNext() && count <= maxSatellites){
- count++;
- GpsSatellite s = itrator.next();
- result.add(s);
- }
- return result;
- }
- /**
- * 某一个卫星的信息.
- * @param gpssatellite
- */
- public static void getGpsStatelliteInfo(GpsSatellite gpssatellite){
- //卫星的方位角,浮点型数据
- Log4Lsy.i(TAG, ”卫星的方位角:”+gpssatellite.getAzimuth());
- //卫星的高度,浮点型数据
- Log4Lsy.i(TAG, ”卫星的高度:”+gpssatellite.getElevation());
- //卫星的伪随机噪声码,整形数据
- Log4Lsy.i(TAG, ”卫星的伪随机噪声码:”+gpssatellite.getPrn());
- //卫星的信噪比,浮点型数据
- Log4Lsy.i(TAG, ”卫星的信噪比:”+gpssatellite.getSnr());
- //卫星是否有年历表,布尔型数据
- Log4Lsy.i(TAG, ”卫星是否有年历表:”+gpssatellite.hasAlmanac());
- //卫星是否有星历表,布尔型数据
- Log4Lsy.i(TAG, ”卫星是否有星历表:”+gpssatellite.hasEphemeris());
- //卫星是否被用于近期的GPS修正计算
- Log4Lsy.i(TAG, ”卫星是否被用于近期的GPS修正计算:”+gpssatellite.hasAlmanac());
- }
- /**
- * 通过WIFI获取定位信息
- */
- public static void fromWIFILocation(){
- //WIFI的MAC地址
- WifiManager manager = (WifiManager)mActivity.getSystemService(Context.WIFI_SERVICE);
- String wifiAddress = manager.getConnectionInfo().getBSSID();
- //根据WIFI信息定位
- 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(wifiAddress!=null&&!wifiAddress.equals(“”)){
- data = new JSONObject();
- data.put(”mac_address”, wifiAddress);
- data.put(”signal_strength”, 8);
- data.put(”age”, 0);
- array.put(data);
- }
- holder.put(”wifi_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(LocationManager.NETWORK_PROVIDER);
- loc.setLatitude((Double)data.get(”latitude”));
- loc.setLongitude((Double)data.get(”longitude”));
- //其他信息同样获取方法
- Log4Lsy.i(TAG, ”latitude ===== ”+(Double)data.get(“latitude”));
- Log4Lsy.i(TAG, ”longitude ===== ”+(Double)data.get(“longitude”));
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
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.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;import com.function.utils.Log4Lsy;/*** Android API本身提供的定位功能,也是GPS定位。* GPS定位,是基于卫星定位。它受环境影响很大。并且是单向定位,也就是只有你自己知道你的地理坐标。* @author LuoSiye*/
@TargetApi(Build.VERSION_CODES.CUPCAKE)
@SuppressLint("NewApi")
public class LocationUtil{private static final String TAG = "LocationUtil";private static LocationUtil instance;private static Activity mActivity;private static LocationManager locationManager;private static LocationListener locationListener;public static LocationUtil getInstance(Activity activity){mActivity = activity;if(instance==null){instance = new LocationUtil();}locationManager = (LocationManager)mActivity.getSystemService(Context.LOCATION_SERVICE);return instance;}/*** 判断GPS导航是否打开.* false:弹窗提示打开,不建议采用在后台强行开启的方式。* true:不做任何处理* @return*/public static void isOpenGPS(){if (!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)){AlertDialog.Builder dialog = new AlertDialog.Builder(mActivity);dialog.setMessage("GPS未打开,是否打开?");dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);// 设置完成后返回到原来的界面mActivity.startActivityForResult(intent, 0); }});dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});dialog.show();}}/*** 通过LocationListener来获取Location信息*/public static void formListenerGetLocation(){locationListener = new LocationListener() {@Overridepublic void onLocationChanged(Location location) {//位置信息变化时触发Log4Lsy.i(TAG, "纬度:"+location.getLatitude());Log4Lsy.i(TAG, "经度:"+location.getLongitude());Log4Lsy.i(TAG, "海拔:"+location.getAltitude());Log4Lsy.i(TAG, "时间:"+location.getTime());}@Overridepublic void onStatusChanged(String provider, int status,Bundle extras) {//GPS状态变化时触发}@Overridepublic void onProviderEnabled(String provider) {//GPS禁用时触发}@Overridepublic void onProviderDisabled(String provider) {//GPS开启时触发 }};/*** 绑定监听* 参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种,前者是GPS,后者是GPRS以及WIFI定位* 参数2,位置信息更新周期.单位是毫秒* 参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息* 参数4,监听* 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新*/locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);}/*** 主动获取Location,通过以下方法获取到的是最后一次定位信息。* 注意:Location location=new Location(LocationManager.GPS_PROVIDER)方式获取的location的各个参数值都是为0。*/public static void getLocation(){Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);Log4Lsy.i(TAG, "纬度:"+location.getLatitude());Log4Lsy.i(TAG, "经度:"+location.getLongitude());Log4Lsy.i(TAG, "海拔:"+location.getAltitude());Log4Lsy.i(TAG, "时间:"+location.getTime());}/*** 获取GPS状态监听,包括GPS启动、停止、第一次定位、卫星变化等事件。*/public static void getStatusListener(){GpsStatus.Listener listener = new GpsStatus.Listener(){@Overridepublic void onGpsStatusChanged(int event) {if(event==GpsStatus.GPS_EVENT_FIRST_FIX){//第一次定位}else if(event==GpsStatus.GPS_EVENT_SATELLITE_STATUS){//卫星状态改变GpsStatus gpsStauts= locationManager.getGpsStatus(null); // 取当前状态int maxSatellites = gpsStauts.getMaxSatellites(); //获取卫星颗数的默认最大值Iterator<GpsSatellite> it = gpsStauts.getSatellites().iterator();//创建一个迭代器保存所有卫星int count = 0;while (it.hasNext() && count <= maxSatellites) { count++; GpsSatellite s = it.next(); }Log4Lsy.i(TAG, "搜索到:"+count+"颗卫星");}else if(event==GpsStatus.GPS_EVENT_STARTED){//定位启动}else if(event==GpsStatus.GPS_EVENT_STOPPED){//定位结束}}};//绑定locationManager.addGpsStatusListener(listener); }/*** 获取所有卫星状态* @return*/public static List<GpsSatellite> getGpsStatus(){List<GpsSatellite> result = new ArrayList<GpsSatellite>();GpsStatus gpsStatus = locationManager.getGpsStatus(null); // 取当前状态//获取默认最大卫星数int maxSatellites = gpsStatus.getMaxSatellites();//获取第一次定位时间(启动到第一次定位)int costTime=gpsStatus.getTimeToFirstFix();Log4Lsy.i(TAG, "第一次定位时间:"+costTime);//获取卫星Iterable<GpsSatellite> iterable=gpsStatus.getSatellites();//一般再次转换成IteratorIterator<GpsSatellite> itrator=iterable.iterator();int count = 0;while (itrator.hasNext() && count <= maxSatellites){count++;GpsSatellite s = itrator.next();result.add(s);}return result;}/*** 某一个卫星的信息.* @param gpssatellite*/public static void getGpsStatelliteInfo(GpsSatellite gpssatellite){//卫星的方位角,浮点型数据 Log4Lsy.i(TAG, "卫星的方位角:"+gpssatellite.getAzimuth());//卫星的高度,浮点型数据 Log4Lsy.i(TAG, "卫星的高度:"+gpssatellite.getElevation());//卫星的伪随机噪声码,整形数据 Log4Lsy.i(TAG, "卫星的伪随机噪声码:"+gpssatellite.getPrn());//卫星的信噪比,浮点型数据 Log4Lsy.i(TAG, "卫星的信噪比:"+gpssatellite.getSnr());//卫星是否有年历表,布尔型数据 Log4Lsy.i(TAG, "卫星是否有年历表:"+gpssatellite.hasAlmanac());//卫星是否有星历表,布尔型数据 Log4Lsy.i(TAG, "卫星是否有星历表:"+gpssatellite.hasEphemeris());//卫星是否被用于近期的GPS修正计算 Log4Lsy.i(TAG, "卫星是否被用于近期的GPS修正计算:"+gpssatellite.hasAlmanac());}/*** 通过WIFI获取定位信息*/public static void fromWIFILocation(){//WIFI的MAC地址WifiManager manager = (WifiManager)mActivity.getSystemService(Context.WIFI_SERVICE); String wifiAddress = manager.getConnectionInfo().getBSSID(); //根据WIFI信息定位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(wifiAddress!=null&&!wifiAddress.equals("")){data = new JSONObject(); data.put("mac_address", wifiAddress); data.put("signal_strength", 8); data.put("age", 0); array.put(data); }holder.put("wifi_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(LocationManager.NETWORK_PROVIDER);loc.setLatitude((Double)data.get("latitude")); loc.setLongitude((Double)data.get("longitude"));//其他信息同样获取方法Log4Lsy.i(TAG, "latitude ===== "+(Double)data.get("latitude"));Log4Lsy.i(TAG, "longitude ===== "+(Double)data.get("longitude"));}}} catch (Exception e) {e.printStackTrace();}}
}
Android 的三种定位方式相关推荐
- 百度地图三种定位方式测试(高精度、低功耗、仅用设备)
百度地图三种定位方式测试(高精度.低功耗.仅用设备) Android定位SDK自v7.0版本起,按照附加功能不同,向开发者提供了四种不同类型的定位开发包,可根据不同需求,自有选择所需类型的开发包使用. ...
- android中几种定位方式详解
目录 前言: 1.GPS定位 2.NETWORK定位 3.AGPS定位 4.基站定位 5.WIFI定位 6.混合定位 目前,移动端大致通过三种方式来进行设备定位:GPS.基站.wifi.本文就详细的讲 ...
- html5边框顶格,CSS 三种定位方式以及格式化上下文详解 》 html5jscss
常规流( Normal flow ) 之称之为常规流,是因为这是相对于后面的浮动和绝对定位的一个概念,浮动和绝对定位元素都脱离了当前的常规流. 在 CSS2.1中,常规流包括块框( block box ...
- ios wifi 定位_iOS中三种定位方式
手机基站定位 原理 每个手机基站都有一个标识符,iPhone或3G iPad可以搜集周围所有收到信号的基站和它们的标识符,通过联网发送到苹果云端服务器,再由服务器根据这些基站的的位置信息查询并计算出当 ...
- android 三种定位方式
最近在看android关于定位的方式,查了很多资料,也做了相关实验,在手机上做了测试,下面总结: 一共有三种定位方式,一种是GPS,一种是通过网络的方式,一种则是在基于基站的方式,但是,不管哪种方式, ...
- android 三种定位方式 介绍
三种获取手机的位置的方式_20 1.网络定位(network).前提是必须连上网络:wifi.3G.2G: 获取到IP地址 例如:传美版QQ,彩虹版QQ,珊瑚虫版QQ,就有一个功能显示对方的IP: 根 ...
- css的三种定位方式使用探讨
css 3种类型定位方式,进行控制页面布局:普通定位,浮动定位,绝对定位. 默认使用普通流技术再页面中布局元素,希望表现与普通流不同,另外两个特性position和float 具体实例 复制代码 代码 ...
- ios wifi 定位_iOS 中的三种定位方式
1.手机基站定位 原理: 每个手机基站都有一个标识符,iPhone或3G iPad可以搜集周围所有收到信号的基站和它们的标识符,通过联网发送到苹果云端服务器,再由服务器根据这些基站的的位置信息查询并计 ...
- android画面数据存储方式,Android的三种储存方式
SharePreferences SharePreferences适合用来保存相对较小的键值集合,比如应用程序的配置文件,它本质是存放在/data/data/[包名]/shared_prefs文件夹下 ...
最新文章
- 深度理解目标检测(MMdetection)-HOOK机制
- Mysql悲观锁乐观锁区别使用区别
- 《算法竞赛入门经典》 例题5-2 木块问题(The Blocks Problem,UVa 101)
- 微信、QQ都在用的腾讯云EB级对象存储架构剖析
- LINUX 下mysql主从安装与同步
- opencv cv.findContours 函数详解
- python基础数据结构,Python基本数据结构
- oracle sql索引查询,Oracle查询数据库的索引字段以及查询用索引
- ABAQUS混凝土CDP插件,一键生成混凝土CDP本构曲线
- C++ 数据结构实战:快速查找
- 『SnowFlake』雪花算法的详解及时间回拨解决方案
- 常用分子对接软件简介
- Google guava之SortedMultiset简介说明
- TLS协议分析------
- jxl 删除excel重复的行
- 使用哈尔滨工业大学SCIR的开源代码训练自己的ELMo
- css 设置body的最小高度是100%,满屏显示
- table中英文值显示其对应中文
- 【Eelectron-vue】构建桌面应用(20)-electron的退出quit和exit
- 网站页面布局的原则有哪些呢?