The OSS project sign-In function obtains the longitude and latitude of the current location in real time.

Source: Internet
Author: User

As the OMS project requires the sign-in function, you need to transmit the latitude and longitude of the current position to the background. The following describes how to obtain the latitude and longitude.

1> Import corelocation. Framework

2> introduce the header file and follow the protocol

# Import <corelocation/corelocation. h>

<Cllocationmanagerdelegate>

3> code

@ Interface yyaboutusviewcontroller () <uiwebviewdelegate, cllocationmanagerdelegate> {uiwebview * _ webview; nsstring * _ currentlatitude; // the latitude of the current position nsstring * _ currentlongdistance; // The longitude of the current location cllocationmanager * _ locmanager;} @ end @ implementation yyaboutusviewcontroller-(void) viewwillappear :( bool) animated {// instantiate a location manager _ locmanager = [[cllocationmanager alloc] init]; _ locmanager. delegate = self; // you can specify the positioning precision // Accuracy: precision: 10 meters // kcllocationaccuracyhundredmeters: precision: 100 meters // kcllocationaccuracykilometer: precision: 1000 meters // accuracy: 3000 meters // kcllocationaccuracybest: the highest precision when the device uses battery power supply // kcllocationaccuracybestfornavigation: the highest precision in navigation, usually when there is an external power supply to use _ locmanager. desiredaccuracy = kcllocationaccuracybest; // distancefilter is a distance filter. To reduce the number of polling times on the positioning device, the location change will not notify the delegate every time, but it is moving enough. When the distance is reached, the delegate program will be notified. // Its unit is meter. Here it is set to move at least 1000 and then notify the delegate to process updates; _ locmanager. distancefilter = 1000.0f; // if it is set to kcldistancefilternone, it is updated every second. // you can determine whether the mobile phone positioning function is enabled. // you can enable the function: settings> privacy> location> positioning service if ([cllocationmanager locationservicesenabled]) {// start location update // when location update is enabled, it needs to be polling with the server, so it consumes a lot of power, use the stopupdatinglocation method when not needed. [_ locmanager startupdatinglocation];} else {uialertview * Alert = [[uialertview alloc] initwithtitle: @ "Please enable positioning" message: N Il delegate: Self cancelbuttontitle: @ "OK" otherbuttontitles: Nil]; [alert show] ;}}-(void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view. [self. navigationcontroller. navigationbar parameters: [uiimage imagenamed: @ "outputs"] forbarmetrics: outputs]; If (device_is_iphone5) {_ webview = [[uiwebview alloc] initwithframe: cgrectmake (0, 64, self. view. Bounds. size. width, self. view. bounds. size. height-64)];} else {_ webview = [[uiwebview alloc] initwithframe: cgrectmake (0, 44, self. view. bounds. size. width, self. view. bounds. size. height-44)];} [self. view addsubview: _ webview] ;}# Pragma mark-pilot // triggered when the geographic location changes-(void) locationmanager :( cllocationmanager *) manager didupdatetolocation :( cllocation *) newlocation fromlocation :( cllocatio N *) oldlocation {// obtain the longitude and latitude nslog (@ "latitude: % F", newlocation. coordinate. latitude); nslog (@ "longitude: % F", newlocation. coordinate. longpolling); // load network data _ webview. delegate = self; // aboutoms.html nsurl * url = [nsurl urlwithstring: [nsstring stringwithformat: @ "% @? Latitude = % F & longpolling = % F ", kbaseurl, @" 7. aspx ", newlocation. coordinate. latitude, newlocation. coordinate. longpolling]; nsurlrequest * request = [nsurlrequest requestwithurl: url]; [_ webview loadrequest: request]; // stop location update [Manager stopupdatinglocation];} # pragma mark-webview proxy method -- (bool) webview :( uiwebview *) webview shouldstartloadwithrequest :( nsurlrequest *) Request navigationtype :( uiwebviewnavigationtype) navigationtype {return yes;}-(void) webviewdidstartload :( uiwebview *) webview {[mbprogresshud showwithtext: @ "loading... "toview: Self. view];}-(void) webviewdidfinishload :( uiwebview *) webview {[mbprogresshud hidehudforview: Self. view animated: Yes];} @ end

 

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.