Adding GPS to your iOS application – Part 1
Share
1 of this series, I will introduce you to the basic concepts of location services Core and get the location of the device in use today. Core Location
If
Core Location
Location provided in two main ways this framework:.
Default Services Location – This service provides the most accurate location information using a variety of methods, including Celluar, wi-fi and GPS. more accurate. consume more energy and can take longer to acquire. can only be used when the application is in the foreground. significant change Location Services – This service uses data from mobile to establish a general idea of where the device is located. faster and uses less energy to determine placement. can also be used to notify your application when a significant change in location occurred even if your application is not the focus files Project:. Adding frames LocationThe first step is to add location services for your project is to add the Core Location framework.
Site Manager To begin, open the app delegate header file: Add # import to top Set your app delegate protocol following Core Location Delegate changing its @ interface line with CFAAppDelegate @ interface: UIResponder Enter a capacity CLLocationManager adding @ property (strong, nonatomic) lease CLLocationManager * before the end @ statement.your application delegate header file should now look something like the following:
class = " code "> style = # # / / Add the local frame import # "> / / Set this app compatible with delegate agents lease @ interface & lt; CLLocationManagerDelegate UIApplicationDelegate, & gt; property @ ( ) UIWindow * Windows / / add a property manager about this app delegate property @ ( ) CLLocationManager * lease, a61390, "color: # @ end Managing create objectswitch to app delegate implementation file (. m), and let's create our object manager. The first thing we should do, because he created it as a feature of our app delegate is to synthesize the property to add @ synthesize lease _locationManager = to the line that reads @ synthesize window = _window
class = "code"> style = # # import" CFAAppDelegate.h " @ implementation = _window, @ synthesize _locationManager, method: . manager An object is created like any other object, but there are three important features that you should be asking when you alloc'd / init'd. . Goal - The purpose of this property is displayed to the user in your application when prompted to allow your application to use your location. It gives you a chance to explain what your application will do with its location information desiredAccuracy - .. Accuracy desired property allows you say the drive exactly how you would like the information to be located. This should be based on the needs of your application. not set this property to kCLLocationAccuracyBest if you just need to know what city they are in. NOTE: This is precisely the "desirable" is not guaranteed. unit will determine the best available information and give it to you, but you are not guaranteed any precision filters distance - .. The distance property manager informs the filter as a unit needs to move (horizontally at a height change) before firing a new event location.'s measured in meters. You can set the property kCLDistanceFilterNone to be notified of all events (this is the default value).finished creating our location address looks like this:
class = "code"> class = - ( BOOL ) : ( * ) Programs didFinishLaunchingWithOptions: ( * ) launch options { = [ [ UIWindow Alloc =" style ] initWithFrame: [ [ ] ] ] . / / Override point for customization after app = [ 002200, "color: #] [ ] , if ( == null ) { = [ [ CLLocationManager Alloc] ] , / / I use ARC with this project so no need to let go = = @ " We will try to tell you who you are if you lose ", = kCLLocationAccuracyBest, _locationManager. = 500 , = , } return , } departure Services [CLLocationManager locationServicesEnabled] and then set up our branch manager and above before returning YES , add the following code: class = "code"> "> ( [ #] ) { [ self.locationManager location startups dating ] , }NOTE: If services location are not enabled, and you start to update the location services, the user is prompted to enable location services. This can be annoying for the end user, whether they should answer "no" every time your application starts .
If you run the application now, you will notice that the user is prompted to enable location services (and include communication. Prompt object of our manager site).
reception site changes CLLocationManagerDelegate protocol. A number of methods available to implement this delegate, but the three most important are probably: - lease: didUpdateToLocation: Location: - lease: didFailWithError: - lease: didChangeAuthorizationStatus: - lease: didUpdateToLocation: Location: @ ... statements and above - (BOOL) application : (*) UIApplication application didFinishLaunchingWithOptions: (*) NSDictionary boot options : - ( void ) ( CLLocationManager * ) ( CLLocation * ) location span style ( # * ) { } class = "code"> style = NSDate * span , = [ event, date timeIntervalSinceNow ] , if ( abs ( ) & lt; 15.0 ) { Location / / timestamp is the end of 15.0 seconds, let's use it! } . HorizontalAccuracy . This property shows how accurate your location information is. As I said above, you may request that the places very accurate, but you are not guaranteed that. In the illustration below, newLocation.coordinate local authority considers it, but actually orange circle. HorizontalAccuracy say he could be anywhere in this circle. src="https://img.skitch.com/20120603-enfpceaw8jgsqw1hieenx48jmj.preview.jpg" alt="horizontalAccuracy"needs of your application will determine how exactly a place you need. Again, if you only need the city, the user is, you do not want it to be as accurate as if you're trying to find something within walking distance . In the code below I put my accuracy limit of 35.0 meters. This is pretty broad, but I found that it works well inside and outside. Insert the following code after the comment / Location / timestamp is in final 15.0 seconds, let's use it!
class = "code"> style = Location / / is in the final 15.0 seconds, let's use it! is ( and lt; 35.0 ) { / / place seems pretty accurate, let's use it! ( @ "+ 0, 6% f latitude, longitude% + 0.6 f n ", newLocation.coordinate.latitude newLocation.coordinate.longitude, ) , NSLog ( @ "Horizontal Accuracy:% f" ) / / Optional: Turn location services when we have a good location [ ] , }This is our full delegate method looks like:.
class = "code"> - ( void ) ( # * ) ( CLLocation * ) location : ( 002200; "cor: # * ) { * = newLocation.timestamp howRecent NSTimeInterval, = [ event, date timeIntervalSinceNow ] , if ( abs ( ) & lt; 15.0 ) # { / Location / timestamp is in the final 15.0 seconds, let's use it ( newLocation.horizontalAccuracy and lt; 35.0 ) # { / / place seems pretty accurate, let's use it NSLog ( @ " + 0.6% f latitude, longitude% + 0.6 = f style n ", newLocation.coordinate.latitude newLocation. coordinate.longitude, #) ; NSLog ( @ " Horizontal Accuracy:% f " ) / / Optional: Turn location services when we have a good location [ 002200; "color: #] , } } #}If you run the project in the simulator and not an entity, you can not see any results in console window. This is because the simulator does not have location services turned on by default. You can simulate a location by selecting services simulation mode in the console window:
class="thumbnail" # import" CFAAppDelegate.h " / / Make sure location services are turned on before leaving the site if ( [ ] ) { * = ( * ) [ ] delegate CLLocation, . * = appDelegate.locationManager.location, / / Do what you want with the place ... }: Shawn Grimes
Shawn is an application developer for mobile ELC Technologies. He is co-author of the book, iOS 5 Recipes: A Problem-Solution Approach (Recipes Apress). You can follow him on Twitter @ shawng. Shawn also works with high school students to teach them skills for application development by Applied club program.
UIKit / UIKit.hp
::: http://www .icodeblog.com/2012/06/04/how-to-add-gps-to-your-ios-app- part-1 / :::
Ditulis oleh Unknown
Rating Blog 5 dari 5
0 komentar:
Posting Komentar