当前位置:操作系统 > 安卓/Android >>

Android开发--用户定位服务--UserLocation

[java] 
<span style="background-color: rgb(243, 248, 251); font-family: simsun;">用户定位介绍:</span>  
 
User Location的作用:
1.获取用户的位置
2.追踪用户的移动
 
User Location的关键API
1.Location Manager:用于管理Android的用户定位服务
2.Location Providers:提供多种定位方式供开发者选择。
  <1>GPS Provider
  <2>Network Provider
  <3>Passive
 
定位方式的分类:
1.GPS定位:使用GPS卫星进行定位,需要在AndroidManifest.xml当中声明如下权限:
    android.permission.ACCESS_FINE_LOCATION
 
2.NETWORK定位:使用信号接收塔和WIFI接入点进行定位,需要在AndroidManifest.xml当中声明如下权限:
    android.permission.ACCESS_FINE_LOCATION
  或
    android.permission.ACCESS_COARSE_LOCATION
以上两种定位方式的区别是GPS定位精度更高,但同时也更耗电
 
获取用户的当前位置:
1.在AndroidManifest.xml当中声明相应的权限;
2.获取LocationManager对象;
3.选择LocationProvider;
4.绑定LocationListener对象。
 
 
LocationListener有四个方法:
1.onLocationChanged(Location location):当设备的位置发生改变时调用
    我们可以调用location.getLongitude()和location.getLatitude()来得到设备所处的经度和纬度
2.onProviderDisabled(String provider):当提供数据Provider禁用时调用
3.onProviderEnabled(String provider):当提供数据的Provider使用时调用
4.onStatusChanged(String provider,int status,Bundle extras):当状态改变时
 
 
我们需要实现LocationListener的以上四个方法:
[java]  
<span style="font-size:18px;">private class TestLocationListener implements LocationListener{  
    @Override  
    public void onLocationChanged(Location location){  
        System.out.println(location.getLongitude());  
        System.out.println(location.getLatitude());  
    }  
    
    @Override  
    public void onProviderDisabled(String provider){  
        // do something you need  
    }  
   
    @Override  
    public void onProviderEnabled(String provider){  
        // do something you need  
    }  
  
    @Override  
    public void onStatusChanged(String provider,int status,Bundle extras){  
        // do something you need  
    }  
}</span>  
 
测试当前设备的LocationProvider
由于一般的设备存在不止一种定位方法,所以在这里给出查找定位服务的方法:
[java]  
<span style="font-size:18px;">List<String> providers=locationManager.getAllProviders();  
            for(Iterator<String> iterator=providers.iterator();iterator.hasNext();){  
                String string=(String)iterator.next();  
                Log.d("BruceZhang", string+"\n");</span>  
 
由于有多个Provider,那么就需要做出选择,在这里给出选择最好的Provider的方法:
此时需要用到一个类--Criteria
下面是在Android SDK文档上给出的解释:
A class indicating the application criteria for selecting a location provider. Providers maybe ordered according to accuracy, power usage, ability to report altitude, speed, and bearing, and monetary cost.
它提供了一系列的方法,设置用户的需求,并最终给出用户所需要的最佳的Provider,下面是文档上对设置条件的解释:
[java] view plaincopy
<span style="font-size:18px;">void   setAccuracy(int accuracy)  
Indicates the desired accuracy for latitude and longitude.</span>  
[java]  
<span style="font-size:18px;">  
void     setAltitudeRequired(boolean altitudeRequired)  
Indicates whether the provider must provide altitude information.</span>  
[java]  
<span style="font-size:18px;">  
void     setBearingAccuracy(int accuracy)  
Indicates the desired bearing accuracy.</span>  
[java 
<span style="font-size:18px;">  
void     setBearingRequired(boolean bearingRequired)  
Indicates whether the provider must provide bearing information.</span>  
[java]  
<span style="font-size:18px;">  
void     setCostAllowed(boolean costAllowed)  
Indicates whether the provider is allowed to incur monetary cost.</span>  
[java]  
<span style="font-size:18px;">  
void     setHorizontalAccuracy(int accuracy)  
Indicates the desired horizontal accuracy (latitude and longitude).</span>  
[java] 
<span style="font-size:18px;">  
void     setPowerRequirement(int level)  
Indicates the desired maximum power level.</span>  
[java]  
<span style="font-size:18px;">  
void     setSpeedAccuracy(int accuracy)  
Indicates the desired speed accuracy.</span>  
[java] view plaincopy
<span style="font-size:18px;">  
void     setSpeedRequired(boolean speedRequired)  
Indicates whether the provider must provide speed information.</span>  
[java] view plaincopy
<span style="font-size:18px;">  
void     setVerticalAccuracy(int accuracy)  
Indicates the desired vertical accuracy (altitude).</span>  
 
追踪用户的位置:
对用户的位置进行更新用到的方法和解释如下:
[java]  
//          public void requestLocationUpdates (String provider,   
//          long minTime, float minDistance, LocationListener listener)   
//          Added in API level 1  
//          Register for location updates using the named provider, and a pending intent.   
//  
//          Parameters  
//          provider  the name of the provider with which to register   
//          minTime  minimum time interval between location updates, in milliseconds   
//          minDistance  minimum distance between location updates, in meters   
/
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,