您的位置:首页 > 产品设计 > UI/UE

Location Services and Maps Programming Guide 纪录

2016-03-23 23:28 513 查看
1、Location-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.

2、If 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.

3、Two 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.

4、Regardless
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.

5、DeterminingWhether 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.

6、To 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方法。

7、Startingthe 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 method。To
stop the significant change locationservice, call the stopMonitoringSignificantLocationChanges method.

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

8、ReceivingLocation 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


10、If 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.

11、RegionMonitoring 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.

12、Defininga 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:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: