Android下定位开发
来源:互联网 发布:淘宝自拍技巧 编辑:程序博客网 时间:2024/04/29 10:57
首先来看一段Android开发文档中提供的有关定位开发的介绍:
Location and maps-based applications are compelling for mobile device users. You can build these capabilities into your applications using the classes of theandroid.location
package and the Google Maps external library. The sections below provide details.
Location Services
Android gives your applications access to the location services supported by the device through the classes in the android.location
package. The central component of the location framework is the LocationManager
system service, which provides APIs to determine location and bearing of the underlying device (if available).
As with other system services, you do not instantiate a LocationManager
directly. Rather, you request an instance from the system by callinggetSystemService(Context.LOCATION_SERVICE)
. The method returns a handle to a new LocationManager
instance.
Once your application has a LocationManager
, your application is able to do three things:
- Query for the list of all
LocationProvider
s for the last known user location. - Register/unregister for periodic updates of the user's current location from a location provider (specified either by criteria or name).
- Register/unregister for a given
Intent
to be fired if the device comes within a given proximity (specified by radius in meters) of a given lat/long.
Requesting Location Updates
Before addressing some of the location errors described above, here is an introduction to how you can obtain user location on Android.
Getting user location in Android works by means of callback. You indicate that you'd like to receive location updates from the LocationManager
("Location Manager") by calling requestLocationUpdates()
, passing it a LocationListener
. Your LocationListener
must implement several callback methods that the Location Manager calls when the user location changes or when the status of the service changes.
For example, the following code shows how to define a LocationListener
and request location updates:
// Acquire a reference to the system Location ManagerLocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);// Define a listener that responds to location updatesLocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // Called when a new location is found by the network location provider. makeUseOfNewLocation(location); } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} };// Register the listener with the Location Manager to receive location updateslocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
The first parameter in requestLocationUpdates()
is the type of location provider to use (in this case, the Network Location Provider for cell tower and Wi-Fi based location). You can control the frequency at which your listener receives updates with the second and third parameter—the second is the minimum time interval between notifications and the third is the minimum change in distance between notifications—setting both to zero requests location notifications as frequently as possible. The last parameter is your LocationListener
, which receives callbacks for location updates.
To request location updates from the GPS provider, substitute GPS_PROVIDER
for NETWORK_PROVIDER
. You can also request location updates from both the GPS and the Network Location Provider by calling requestLocationUpdates()
twice—once for NETWORK_PROVIDER
and once for GPS_PROVIDER
.
Requesting User Permissions
In order to receive location updates from NETWORK_PROVIDER
or GPS_PROVIDER
, you must request user permission by declaring either the ACCESS_COARSE_LOCATION
or ACCESS_FINE_LOCATION
permission, respectively, in your Android manifest file. For example:
<manifest ... > <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ...</manifest>
Without these permissions, your application will fail at runtime when requesting location updates.
Note: If you are using both NETWORK_PROVIDER
and GPS_PROVIDER
, then you need to request only the ACCESS_FINE_LOCATION
permission, because it includes permission for both providers. (Permission forACCESS_COARSE_LOCATION
includes permission only for NETWORK_PROVIDER
.)
- Android下定位开发
- iOS8下定位
- HPUX下定位网卡位置
- Linux下定位命令总结
- iOS8 下定位失败问题
- iOS8 下定位失败问题
- iOS8 下定位失败问题
- Linux下定位内存泄漏
- iOS8 下定位失败问题
- 在VC下定位内存泄漏总结
- windows环境下定位随机崩溃
- windows环境下定位随机崩溃
- webdriver下定位frame和alert
- linux下定位问题的几个工具
- linux下定位段错误(转)
- iOS8 下定位 (CLLocationManager) 的改變
- iOS8 下定位 (CLLocationManager) 的改變
- iOS8 下定位 (CLLocationManager) 的改變
- 一个简单的linux守护进程
- RQNOJ 294 斐波那契公约数
- Zend Studio的aptana插件配置,包含jquery代码提示
- linux gcc编译器中使用gdb单步调试程序,程序不是顺序执行的。
- JDBC 多数据源同时访问框架图
- Android下定位开发
- 大家喜欢计算机的哪些方向呢?
- 临时占位图片
- 黑马程序员_面向对象
- OO中的继承分析,主要分析在编译和运行过程中子类父类的方法、字段和实例化时候在内存中分配和执行的先后,以及两个原则
- IE6的 z-index BUG
- ubuntu(内核2.6)解压rar文件
- TCP连接的建立(三次握手)和释放(四次握手)
- 读书笔记(第四讲)operator