IPhone and Location(1)Documents User Location
1. Introduce Location and Map
Getting the User's Location
Region Monitoring
Geocoding Location Data
iBeacons Enhance the User's Experience of a Location
Heading Information Indicates the User's Current Orientation - magnetometer
1.1 Getting the User's Location
Core Location Framework
a. The significant-change location service provides a low-power way to get the current location and be notified.
b. The standard location service
c. Region monitoring, monitor boundary crossings for defined geographical regions and Bluetooth low energy beacon regions.
Linking to a Library or Framework
Select the Project-----> Select Targets -----> Build Phases-----> Link Binary With Libraries----> Click on +
#import <CoreLocation/CoreLocation.h>
Requiring the Presence of Location Services in Order to Run
Info.plist
UIRequiredDeviceCapabilities= array of strings?
a. location-services string if you require location services in general.
b. gps string if your app requires the accuracy offered only by GPS hardware.
Getting the User's Current Location
The core location framework uses information obtained from the built-in cellular, Wi-Fi or GPS hardware to triangulate a location.
standard location service
significant-change location service
Gathering location data is power-intensive operation.?
Power up the onboard radios and querying the available cell towers, Wi-Fi hotspots, or GPS satellites. Leaving the standard location service running for extended periods can drain the device's battery.
The significant-change location service reduces battery drain by monitoring only cell tower changes.
Determining Whether Location Services Are Available
a. The user can disable location Services in the App Settings
b. The user can deny location services for a specific app.
c. The device might be in Airplane mode and unable to power up the necessary hardware.
We call CLLocationManager.locationServicesEnabled to check that.
Starting the Standard Location Service
Create an instance of CLLocationManager, configure desiredAccuracy and distanceFilter.
Assign a delegate to the object and call the startUpdatingLocation.
To stop the delivery of location updates, call stopUpdatingLocation.
-(void)startStandardUpdates{
? ? ?if(nil == locationManager)
? ? ? ? ? locationManager = [[CLLocationManager alloc] init];
? ? ?locationManager.delegate = self;
? ? ?locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
? ? ?locationManager.distanceFilter = 500; //meters
? ? ?[locationManager startUpdatingLocation];
}
Starting the Significant-Change Location Service
create an instance of the CLLocationManager, assign a delegate to it, call the startMonitoringSignificantLocationChanges
-(void) startSignificantChangeUpdates{
? ? ?if(nil == locationManager){
? ? ? ? ? locationManager = [[CLLocationManager alloc]init];
? ? ?}
? ? ?locationManager.delegate = self;
? ? ?[locationManager startMonitoringSignificantLocationChanges];
}
stopMonitoringSignificantLocationChanges to stop the service.
If we leave this service running and our app is suspended or terminated, the service automatically wakes up our app when new location data arrives.?
At wake-up time, our app is put into the background and given a small amount of time(10 seconds) to process the location data. Our app is in the background and should do minimal work and avoid any tasks(such as querying the networks).If the time expires, our app may be terminated.
If we need more time, we need to request more time using the beginBackgroundTaskWithExpirationHandler, end the task by calling the endBackgroundTask
Receiving Location Data from a Service
locationManager:didUpdateLocations
Sometimes, location manager returns cached events, so we need to check the timestamp of any location events we receive.
-(void) locationManager:(CLLocationManager *)manager
? ? ?didUpdateLocations:(NSArray *)locations {
? ? ?CLLocation* location = [locations lastObject];
? ? ?NSDate* eventDate = location.timestamp;
? ? ?NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
? ? ?if(abs(howRecent) < 15.0){
? ? ? ? ? NSLog(@"latitude %+.6f, longitude %+.6f\n", location.coordinate.latitude, location.coordinate.longitude);
? ? ?}
}
Getting Location Events in the Background
Declare our app as needing background location services by including the UIBackgroundModes in Info.plist.
When an app is in the background and the location data is unlikely to change, in iOS 6.0 and later the location manager pauses the delivery of location updates to its delegate and powers down the hardware. After the user launches the app or when the user begins the activity again, the location manager resumes delivering location updates.
How to pause is decided by activityType, for example CLActivityTypeAutomotiveNavigation.
When the location manager pauses location updates, it calls the locationManagerDidPauseLocationUpdates
When the location manager resume location updates, it calls the locationManagerDidResumeLocationUpdates
If your paused, background app is terminated by the user or if it is terminated by the system to free up memory for the current foreground app, location updates will not resume automatically the next time the app is launched.
Deferring Location Updates While in the Background
deferredLocationUpdatesAvailable on CLLocationManager to see if the device support this.
-(void)locationManager:(CLLocationManager *)manager
? ? ?didUpdateLocations:(NSArray *)locations {
? ? ?[self.hike addLocations:locations];
? ? ?
? ? ?if(!self.defferingUpdates){
? ? ? ? ? CLLocationDistance distance = self.hike.goal - self.hike.distance;
? ? ? ? ? NSTimeInterval time = [self.nextAudible timeIntervalSinceNow];
? ? ? ? ? [locationManager allowDeferredLocationUpdatesUntilTraveled:distance
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?timeout:time];
? ? ? ? ? self.defferingUpdates = YES;
? ? ?}
}
Tips for Conserving Battery Power
…snip...
References:
Location and Maps Guide
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009497-CH1-SW1
API book
https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CoreLocation_Framework/_index.html#//apple_ref/doc/uid/TP40007123
sample
https://developer.apple.com/library/ios/search/?q=location+sample
tips
http://www.cocoachina.com/special/fornew.html
https://developer.apple.com/library/ios/recipes/xcode_help-project_editor/Articles/AddingaLibrarytoaTarget.html#//apple_ref/doc/uid/TP40010155-CH17
Guide
https://developer.apple.com/library/ios/navigation/#section=Resource%20Types&topic=Getting%20Started
详细解决方案
IPhone and Location(一)Documents User Location
热度:282 发布时间:2016-04-25 05:56:00.0
相关解决方案
- 如何用window.location.href上载文件
- 新人第一帖!JSF有关问题:<h:inputText value="{user.name}"/>
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password,该如何处理
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password解决思路
- 使用window.location.reload()的有关问题
- 网页展示Access denied for user 'root'@'localhost'
- java.lang.UnsupportedOperationException: The user must supply a JDBC connection,该怎么处理
- window.location.href错误
- Error reading resource: com/user/User.hbm.xml,该怎么解决
- jsp页面post到servlet发生异常:java.lang.NoSuchMethodError: bean.User.<init>(Ljava/lang/String;Ljava/lang/Str
- 火狐浏览器request.getSession().getAttribute("User")为空。解答
- iphone 模拟器,该如何处理
- 關于Header中的 User-Agent 屬性,請幫忙,该怎么解决
- 问一个httpconnection.setRequestProperty("User-Agent" "Profile/MIDP-1.0 Configuration/CLDC-1.0");的有关问题
- List<User> userList = new ArrayList<User>();解决思路
- c#c++,android,ios(iphone),php,java视屏课程 散分
- User control的施用
- 关键字 'user' 附近有语法异常
- window.location.href不跳转有关问题
- @(ViewBag.Location ? "视频点播") 什么意思解决办法
- IIS 调用Microsoft.Office.Interop.Word.Documents.Open 回到为null
- 当 user control 改变了,怎么隐藏parent page中的一个button
- 有哪位高手用过JPlayer ,进来一下,IPHONE 等设备播放有关问题
- User.Identity.Name 为空的有关问题
- 访问IIS的有关问题, 报"User credentials cannot be used for local connections" 100分回报
- c#调用js中window.location.href进行页面传值?大有关问题!郁闷.
- iframe javascript:location.replace(url)路径有关问题
- 在上传文件时提示对路径“C:\Documents and Settings\Administrator\桌面\6b.jpg”的访问被拒绝解决方法
- 关于if (!Context.User.Identity.IsAuthenticated )用法解决办法
- window.location.href 在 FireFox 3.0.3 下却不管用,有什么好的解决方法没有