How to get current iPhone location through code?

How do you get the current location of iPhone through code? I need to track location using GPS.

+3
source share
7 answers

You are looking for the CoreLocation API.

Here is the tutorial

+5
source

There is (as always, for basic functionality) full documentation on the developer's site, and you may find this example useful;

Key points to remember are:

1) , , . , , .

2) , ( ), .

3) , , LAST CACHED, ( ) , . , , , 10 , , . , - , . , .

, .

+17
+2

,

+1

, Core Location , , GPS , . (API- CLLocationManager , , , GPS.)

CLLocationManager , INTULocationManager, .

0

1

self.yourmapreferencename.setRegion...

//

import UIKit MapKit import CoreLocation ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!
var locationManager : CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()

    if CLLocationManager.locationServicesEnabled()
    {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()

    }

}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{

    let location = locations.last! as CLLocation
    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    self.mapView.setRegion(region, animated: true)
}

}

0

The real solution can be found here. Z5 Concepts iOS Development Code Snippet

It just requires a bit of coding.

- (IBAction)directions1_click:(id)sender
{
    NSString* address = @"118 Your Address., City, State, ZIPCODE";
    NSString* currentLocation = @"Current Location";
    NSString* url = [NSStringstringWithFormat: @"http://maps.google.com/maps?saddr=%@&daddr=%@",[currentLocation stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    UIApplication *app = [UIApplicationsharedApplication];
    [app openURL: [NSURL URLWithString: url]];
}
-2
source

Source: https://habr.com/ru/post/1737133/


All Articles