Location Services and Maps Programming Guide 纪录

来源:互联网 发布:有java基础学python 编辑:程序博客网 时间:2024/05/15 23:43


1Location-based information consists oftwo pieces: location services and maps. Location services are provided by theCore Location framework. Maps are provided by the Map Kit framework.

 

2If your iOS app requires locationservices to function properly, include the UIRequiredDeviceCapabilities key in the app’s Info.plist file. The App Store uses the information in this key toprevent users from downloading apps to devices that don’t contain the listedfeatures.

 

 

3Two services can give youthe user’s current location:

  • The standard location service is a configurable, general-purpose solution for getting location data and tracking location changes for the specified level of accuracy. 
  • The significant-change location service delivers updates only when there has been a significant change in the device’s location, such as 500 meters or more.

 

 

4Regardless of the importance of location data in yourapp, you should choose the appropriate location service and use it wisely toavoid draining a device’s battery. For example:

  • If your iOS app must keep monitoring location even while it’s in the background, use the standard location service and specify the location value of the UIBackgroundModes key to continue running in the background and receiving location updates. (In this situation, you should also make sure the location manager’s pausesLocationUpdatesAutomatically property is set to YES to help conserve power.) .

 

  • If GPS-level accuracy isn’t critical for your app and you don’t need continuous tracking, you can use the significant-change location service. It’s crucial that you use the significant-change location service correctly, because it wakes the system and your app at least every 15 minutes, even if no location changes have occurred, and it runs continuously until you stop it.

 

 

5DeterminingWhether Location Services Are Available

 

For these reasons, it’s recommended that you always call the locationServicesEnabled classmethod of CLLocationManager before attempting to start either the standard orsignificant-change location services. If it returns NO and you attempt to startlocation services anyway, the system prompts the user to confirm whetherlocation services should be re-enabled.

 

 

6To use the standard location service, create an instance of the CLLocationManagerclass and configure its desiredAccuracy and distanceFilter properties. To begin receivinglocation notifications, assign a delegate to the object and call the startUpdatingLocation method.As location data becomes available, thelocation manager notifies its assigned delegate object.To stop the delivery oflocation updates, call the stopUpdatingLocation method of the location managerobject.

 

简而言之,使用标准位置服务大概流程如下:

1、创建CLLocationManager类的实例

2、配置实例的desiredAccuracy,distanceFilter属性值,即设置精度和距离过滤器。

3、指定delegate.

4、调用startUpdatingLocation方法。

5、当不需要接受位置信息时,调用stopUpdatingLocation方法。

 

7Startingthe Significant-Change Location Service

To use the significant-change location service, create an instance of the CLLocationManager class, assigna delegate to it, and callthe startMonitoringSignificantLocationChanges methodTo stop the significant change locationservice, call the stopMonitoringSignificantLocationChanges method.

总之:与标准位置服务相比,调用的函数是不同的。

 

 

 

8ReceivingLocation Data from a Service

Beginning in OS X v10.9 and iOS 6, the location manager reportsevents to the locationManager:didUpdateLocations: methodof its delegate when they become available.

If there is an error retrieving an event, the location manager callsthe locationManager:didFailWithError: methodof its delegate instead.

 

 

9 The user knows when your appstarts location services because the first time your app starts the service,the system prompts the user for permission to use it.

 

Another way to build trust is to include the NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescriptionkeyin your app’s Info.plist file and set the value of that key to a string that describeshow your app intends to use location data. If you call the requestWhenInUseAuthorization methodwithout including one of these keys, the system ignores your request.

 

即,使用位置服务时,需在Info.plist中添加NSLocationAlwaysUsageDescription或者 NSLocationWhenInUseUsageDescription

 

 

10If you are monitoring regions or usingthe significant-change location service in your app, there are situations whereyou must start location services at launch time. Apps using those services canbe terminated and subsequently relaunched when new location events arrive.Although the app itself is relaunched, location services are not startedautomatically. When an app is relaunched because of a location update, thelaunch options dictionary passed to your application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method contains the UIApplicationLaunchOptionsLocationKey key. The presence of that keysignals that new location data is waiting to be delivered to your app. Toobtain that data, you must create a new CLLocationManager object and restart the locationservices that you had running prior to your app’s termination. When you restartthose services, the location manager delivers all pending location updates toits delegate.

即,由于位置更新重启App后,the launch options dictionary 传递到application:willFinishLaunchingWithOptions:application:didFinishLaunchingWithOptions方法,包含在UIApplicationLaunchOptionsLocationKey中。这个key的存在表明有新的位置数据需要传送到你的app。此时,必须创建新的CLLocationManager对象,重启位置服务。重启后,location manager 将所有更新位置数据传递到delegate.

 

11RegionMonitoring iOS 7.0 later

CLLocationmanager类中的isMonitoringAvailableForClass:方法返回YES or NO表明是否设备是否支持,authorizationStatus方法确定App是否有权限使用位置服务。KCLAuthorizationStatusAuthorized状态表明可以使用。

 

use the locationManager:didChangeAuthorizationStatus: delegate method to detect changes in your app’s status andremove regions as appropriate.

 

Finally, if your app needs to process location updates in thebackground, be sure to check the backgroundRefreshStatus property ofthe UIApplication class. 

 

 

 

12Defininga Geographical Region to Be Monitored:

 

Defineregion:

In iOS 7.0 and later, you define geographical regions using the CLCircularRegion class.

 

Registerregion:

To register a region, call the startMonitoringForRegion: method ofyour CLLocationManager object

 

Check user in region or not:

To check whether the user is already inside the boundary of aregion, use the requestStateForRegion: method ofthe CLLocationManager class.

 

Register region unavailable:

If you attempt to register a region and space is unavailable, thelocation manager calls the locationManager:monitoringDidFailForRegion:withError: method of its delegate with the kCLErrorRegionMonitoringFailure errorcode.

 

Handling Boundary-Crossing Events for a Geographical Region:

By default, every time a user’s current location crosses aboundary region, the system generates an appropriate region event for your app.Apps can implement the following methods to handle boundary crossings:

  • locationManager:didEnterRegion:
  • locationManager:didExitRegion:


0 0
原创粉丝点击