Locate the target instance and find the target instance.

Source: Internet
Author: User

Locate the target instance and find the target instance.


# Import "ViewController. h"


// Import the CoreLocation framework before positioning

# Import <CoreLocation/CoreLocation. h>


@ Interface ViewController () <CLLocationManagerDelegate>

{

// Locate the class CLLocationManager used

CLLocationManager * manager;

}

@ End



/**

*

1. LBS: Location-Based Service (Location-Based Service) Location Service

2. Apple provides the CoreLocation framework for positioning and can be used independently. CoreLocation mainly includes the positioning, Geographic Encoding, and anti-Geographic Encoding functions;

3. Geographic Encoding: Obtain the location-related information (longitude, dimension, and altitude) through the geographical location string)

 

Anti-Geographic Encoding: obtains the place name of the specific string location through latitude and longitude.

 

4. The CLCoreLocation class encapsulates location and Height Information.

 

5. prime meridian: it passes through the Greenwich mean solar observatory in London. The eastern longitude ranges from 180 to 360, and the eastern longitude is positive. The western longitude is negative. The Equator ranges from 0 to 90, with a total of 180;

 

6. Use CLLocationCoordinate2D in iOS code to represent the longitude and latitude

Typedef struct {

CLLocationDegrees latitude; latitude

CLLocationDegrees longpolling; longitude

} CLLocationCoordinate2D;

 

 

 

7. before using location, determine whether or not the location service locationServicesEnabled is enabled. Explain to the user why the location service is used. (a pop-up box is automatically displayed for Apple, prompt the user to use the positioning service (when using the software for the first time ));

 

Under iOS8, because the location information is the user's privacy, the authorization system automatically displays a dialog box for user authorization.

In Info. plist, set NSLocationUsageDescription to describe the target (Privacy-Location Usage Description). Why do you want to enable the Location?

 

To locate ios8, add NSLocationWhenInUseUsageDescription to pilst.

NSLocationAlwaysUsageDescription

Get user authorization

 

 

*/

@ Implementation ViewController


-(Void) viewDidLoad {

[Super viewDidLoad];

// Whether the user has enabled the location service

BOOL isOpen = [CLLocationManager locationServicesEnabled];

If (! IsOpen ){

UIAlertView * alert = [[UIAlertView alloc] initWithTitle: nil message: @ "enable locating service again" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil];

[Alert show];

Return;

}

// Initialize the targeted service object

Manager = [[CLLocationManager alloc] init];

// When setting the key value in infoPlist, it must be consistent with that in the code.

// Keep using the positioning function

// [Manager requestAlwaysAuthorization];

// Enable the location service when using it

[Manager requestWhenInUseAuthorization];

// Use the positioning service, causing high power consumption. After using the positioning service, you need to disable the positioning service (saving power)

// Enables the system to help manage the positioning Switch

Manager. pausesLocationUpdatesAutomatically = YES;

// Set the accuracy of the positioning service, and the more accurate the more power consumption

// 1.GPS 2. WIFI 3. Cellular Data Collection, encapsulated by Apple to the underlying layer

/* Extern const CLLocationAccuracy kCLLocationAccuracyBest;

Extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;

Extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;

Extern const CLLocationAccuracy kCLLocationAccuracyKilometer;

Extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers ;*/

 

Manager. desiredAccuracy = kCLLocationAccuracyBest;

// How many meters to update the Location once

Manager. distanceFilter = 50; // centered on its own Location, the Location will be updated when it is out of the Set range.

// When the location is obtained or updated, the proxy location is called.

Manager. delegate = self;

}


# Pragma mark --- proxy method

-(Void) locationManager :( CLLocationManager *) manager

DidUpdateLocations :( NSArray *) locations

{

// Locations contains the updated position. Obtain the new position by getting the last array element of the array.

NSLog (@ "% @", locations );

CLLocation * curLocation = [locations lastObject];

// Obtain the longitude and latitude of the current position through Location

CLLocationCoordinate2D curCoordinate2D = curLocation. coordinate;

// CurCoordinate2D. latitude

// CurCoordinate2D. longpolling longitude


NSLog (@ "longitude: % f latitude: % f", curCoordinate2D. longpolling, curCoordinate2D. latitude );

// Obtain the update Location time (NSdate) through Location)

NSDate * updateTime = curLocation. timestamp;

NSLog (@ "% @", updateTime );

// Obtain the current speed

Double speed = curLocation. speed;

NSLog (@ "current driving speed: % f m/s", speed );

// Indoors, you can obtain the floor number (ios8.0 or above)

CLFloor * curFloor = curLocation. floor;

NSLog (@ "@ % ld floor", (long) curFloor. level );

// Obtain the course direction of the current device. 0 ° indicates the north, 90 ° indicates the east, 180 ° indicates the South, 270 ° indicates the West

CLLocationDirection curDiraction = curLocation. course;

NSLog (@ "% f", curDiraction );

// Get the accuracy; get the accuracy of the altitude verticalAccuracy (when negative values are not available, the altitude accuracy is not obtained)

CLLocationAccuracy curAccuracy = curLocation. verticalAccuracy;

NSLog (@ "% f", curAccuracy );

// Obtain the altitude

Double altitude = curLocation. altitude;

NSLog (@ "altitude: % f", altitude );

// Obtain the average speed of the trip. You need to get two or more

}


-(Void) locationManager :( CLLocationManager *) manager

DidFailWithError :( NSError *) error

{

NSLog (@ "% @", error );

}



-(Void) viewWillAppear :( BOOL) animated

{

// Enable Positioning

[Manager startUpdatingLocation];

}



-(Void) viewWillDisappear :( BOOL) animated

{

// Close Positioning

[Manager stopUpdatingLocation];

}



-(Void) didReceiveMemoryWarning {

[Super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


@ End


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.