IPhone GPS Positioning System instance operation

Source: Internet
Author: User

IPhone GPSLocating System instances is what we will introduce in this article. First, let's take a look at the details of this article. Today,GPSMore and more functional mobile devices are used.GPSThe positioning system can precisely locate your current geographic location, but becauseGPSThe receiver must be aligned with the sky to work, so it is useless in the indoor environment.

Another effective way to find your location is to use the mobile phone base station. When the mobile phone is turned on, it will keep in touch with the surrounding base station. If you know the identity of these base stations, you can use a variety of databases (including the identity of the base station and their exact geographic location) to calculate the physical location of the mobile phone. The base station does not need satellites, andGPSDifferent, it works in the same way for indoor environments. But it does notGPSSo accurate, its precision depends on the density of the base station, which has the highest accuracy in base station-intensive areas.

Tip: First GenerationIPhoneNot ConfiguredGPSThe receiver and base station method cannot be applied to the iPod Touch because it is not a mobile phone.

The third method is to rely on Wi-Fi. When using this method, the device connects to the Wi-Fi network and determines the location by checking the data of the service provider. It neither depends on satellites nor on base stations, therefore, this method is effective for areas that can be connected to Wi-Fi networks, but its accuracy is also the worst among the three methods.

Locating framework Kernel

On the iPhone, Apple provides the positioning framework kernel to help you determine your physical location. The beauty of this framework is that it uses all the three methods mentioned above, which method is used is transparent to developers. Developers only need to specify the required precision, and the positioning kernel will determine the positioning result in the best way.

Are you surprised ?! The rest of this article will show you how this is done.

Obtain coordinates

Use Xcode to create a new view-based application project named LBS. in the new project, double-click the LBSViewController. xib file and edit it in the interface design tool. Fill the View window with the following components, as shown in 1.

Label

TextField

Figure 1 Location view instance: Fill the window with Label and TextFiled

Right-click the Framework Group in Xcode, select "add" * "existing Framework", and select "Framework/CoreLocation. to LBSViewController. add the following code in bold text to the H file:

 
 
  1. #import   
  2. #import   
  3. @interface LBSViewController : UIViewController  
  4.  {  
  5. IBOutlet UITextField *latitudeTextField;  
  6.  
  7. IBOutlet UITextField *longitudeTextField;  
  8. IBOutlet UITextField *accuracyTextField;  
  9. CLLocationManager *lm;  
  10. }  
  11. @property (retain, nonatomic) UITextField *latitudeTextField;  
  12. @property (retain, nonatomic) UITextField *longitudeTextField;  
  13. @property (retain, nonatomic) UITextField *accuracyTextField;  
  14. @end 

To use the CLLocationManager class, you need to implement the CLLocationManagerDelegate protocol in your view controller class. You also need to create three egresses to connect the three TextFiled views in the View window.

Return to the interface editor, click and drag the document owner project to the three TextField views, and select latitudeTextField, longitudeTextField, and accuracyTextField respectively.

Query the bold Section in the following code in the LBSViewController. m file:

 
 
  1. #import "LBSViewController.h"  
  2. @implementation LBSViewController  
  3. @synthesize latitudeTextField, longitudeTextField, accuracyTextField;  
  4. - (void) viewDidLoad {  
  5. lm = [[CLLocationManager alloc] init];  
  6. if ([lm locationServicesEnabled]) {  
  7. lm.delegate = self;  
  8. lm.desiredAccuracy = kCLLocationAccuracyBest;  
  9. lm.distanceFilter = 1000.0f;  
  10. [lm startUpdatingLocation];  
  11. }  
  12. }  
  13. - (void) locationManager: (CLLocationManager *) manager  
  14. didUpdateToLocation: (CLLocation *) newLocation  
  15. fromLocation: (CLLocation *) oldLocation{  
  16. NSString *lat = [[NSString alloc] initWithFormat:@"%g",  
  17. newLocation.coordinate.latitude];  
  18. latlatitudeTextField.text = lat;  
  19. NSString *lng = [[NSString alloc] initWithFormat:@"%g",  
  20. newLocation.coordinate.longitude];  
  21. longitudeTextField.text = lng;  
  22. NSString *acc = [[NSString alloc] initWithFormat:@"%g",  
  23. newLocation.horizontalAccuracy];  
  24. accaccuracyTextField.text = acc;  
  25. [acc release];  
  26. [lat release];  
  27. [lng release]   
  28. }  
  29. - (void) locationManager: (CLLocationManager *) manager  
  30. didFailWithError: (NSError *) error {  
  31. NSString *msg = [[NSString alloc]  
  32. initWithString:@"Error obtaining location"];  
  33. UIAlertView *alert = [[UIAlertView alloc]  
  34. initWithTitle:@"Error"  
  35. message:msg  
  36. delegate:nil  
  37. cancelButtonTitle: @"Done"  
  38. otherButtonTitles:nil];  
  39. [alert show];  
  40. [msg release];  
  41. [alert release];  
  42. }  
  43. (void) dealloc{  
  44.  [lm release];  
  45. [latitudeTextField release];  
  46. [longitudeTextField release];  
  47. [accuracyTextField release];  
  48. [super dealloc];  

The previous Code creates an instance of the CLLocationManager class. Before using an object, you should check whether the user has enabled the device location service. If yes, you can use the desiredAccuracy attribute to specify the expected precision and use the following constant to specify the expected precision:

 
 
  1.  kCLLocationAccuracyBest  
  2. kCLLocationAccuracyNearestTenMeters  
  3.  kCLLocationAccuracyHundredMeters  
  4.  kCLLocationAccuracyKilometer  
  5. kCLLocationAccuracyThreeKilometers 

The distanceFilter attribute allows you to specify the distance and location information that the device must move to update. The unit of this attribute is meter. If you want to get all the moving notifications, you can use the kCLDistanceFilterNone constant and start the Location Manager using the startUpdatingLocation method.

To obtain location information, you must handle the following two events:

 
 
  1. locationManager:didUpdateToLocation:fromLocation:  
  2. locationManager:didFailWithError: 

When a new location value is obtained, the device triggers the locationManager: didUpdateToLocation: fromLocation: event. If the location manager cannot determine the location information, it triggers the locationManager: didFailWithError: event.

When the device can determine the location, you may want to display the longitude and latitude values and precision. In this case, you can use the CLLocation object. Its horizontalAccuracy attribute can specify the precision range, in meters.

Run the Command-r Command to test the program on the iPhone simulator. Figure 2 shows the latitude and longitude values of the simulator and the accuracy.

Figure 2 positioning test: These fixed values are always displayed when you test the sample program on the iPhone Simulator

Show Map

It will be even more interesting to locate the location coordinates on the Map. Fortunately, the iPhone 3.0 SDK includes the Map Kit API, which allows you to display Google Map in your program, the following is an example.

Or use the project created earlier to add a button in the visual window in the LBSViewController. xib file, as shown in figure 3.

Figure 3 View Map button: After adding a button

Right-click the framework Group in Xcode and add a new framework MapKit. framework. Add the bold part in the following code in the LBSViewController. h file:

 
 
  1. #import   
  2. #import   
  3. #import   
  4. @interface LBSViewController : UIViewController  
  5.  {  
  6. IBOutlet UITextField *accuracyTextField;  
  7. IBOutlet UITextField *latitudeTextField;  
  8. IBOutlet UITextField *longitudeTextField;  
  9. CLLocationManager *lm;  
  10. MKMapView *mapView;  
  11. }  
  12. @property (retain, nonatomic) UITextField *accuracyTextField;  
  13. @property (retain, nonatomic) UITextField *latitudeTextField;  
  14. @property (retain, nonatomic) UITextField *longitudeTextField;  
  15. -(IBAction) btnViewMap: (id) sender;  
  16. @end 

Return to the interface editor, drag the button to the file owner project, and select btnViewMap :.

In the LBSViewController. m file, add the bold Section in the following code:

 
 
  1. -(IBAction) btnViewMap: (id) sender {  
  2. [self.view addSubview:mapView];  
  3. }  
  4.   (void) viewDidLoad {  
  5. lm = [[CLLocationManager alloc] init];  
  6. lm.delegate = self;  
  7. lm.desiredAccuracy = kCLLocationAccuracyBest;  
  8.  
  9. lm.distanceFilter = 1000.0f;  
  10. [lm startUpdatingLocation];  
  11. mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];  
  12. mapView.mapType = MKMapTypeHybrid;  
  13. }  
  14. - (void) locationManager: (CLLocationManager *) manager  
  15. didUpdateToLocation: (CLLocation *) newLocation  
  16. fromLocation: (CLLocation *) oldLocation{  
  17. NSString *lat = [[NSString alloc] initWithFormat:@"%g",  
  18. newLocation.coordinate.latitude];  
  19. latlatitudeTextField.text = lat;  
  20. NSString *lng = [[NSString alloc] initWithFormat:@"%g",  
  21. newLocation.coordinate.longitude];  
  22. longitudeTextField.text = lng;  
  23. NSString *acc = [[NSString alloc] initWithFormat:@"%g",  
  24. newLocation.horizontalAccuracy];  
  25. accaccuracyTextField.text = acc;  
  26. [acc release];  
  27. [lat release];  
  28. [lng release];  
  29. MKCoordinateSpan span;  
  30. span.latitudeDelta=.005;  
  31. span.longitudeDelta=.005;  
  32. MKCoordinateRegion region;  
  33. region.center = newLocation.coordinate;  
  34. region.span=span;  
  35. [mapView setRegion:region animated:TRUE];  
  36. }  
  37. - (void) dealloc{  
  38. [mapView release];  
  39. [lm release];  
  40. [latitudeTextField release];  
  41. [longitudeTextField release];  
  42. [accuracyTextField release];  
  43. [super dealloc];  

Code explanation:

When a view is loaded, an instance of the MKMapView class is created and the displayed map type is set.

When you click the View Map Button, add a mapView object to the current View.

When the location information is updated, use the setRegion: Method of the mapView object to enlarge the map.

In the iPhone simulator, press Command-r to test the program. Click View Map to display a Map containing the location returned by the location manager. 4.

Figure 4 map location: display the map location by locating the kernel framework

Because the simulator always displays the same location, if you haveIPhoneThe mobile phone can also feel it. When you move the location, you will see that the map is automatically updated. Set the distanceFilter attribute to a smaller value to enhance the tracking experience.

As you can see,IPhoneThe SDK positioning kernel framework allows you to easily implement location-based devices. In addition, MapKit (includingIPhoneSDK) to display location information on the map. If you haven't created a location-based application, I think you should try it after reading this article. What are you waiting? Via: http://ming-fanglin.javaeye.com/blog/703744

Summary:IPhone GPSThe operations on Locating System instances have been introduced. I hope this article will help you! For more information, see edit recommendations!

This article is from the blacksoft base-China's largest hacker software security tutorial download site! Mobile phone information) original link: http://www.hackvip.com/mobiwen/html/Mobile_220397_3.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.