0

I've tried the following solutions:

But all I get is a map zoomed in on some other location, like San Francisco or somewhere in the middle east.

The top answer for solution3 looks like it shows how to print the users current location but it doesn't show how to use it with a map.

My ViewController.swift file

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!
    let locationManager = CLLocationManager()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.locationManager.requestAlwaysAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            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)
    }
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Sam
  • 1,765
  • 11
  • 82
  • 176
  • 4
    Are you testing it on a simulator? – Malik Sep 21 '18 at 06:24
  • 1
    If you are testing on the simulator, it's not going to show your current location. Are you testing on a real device? – Jay Mayu Sep 21 '18 at 06:28
  • Yes I'm testing on the simulator, I didn't know I couldn't do that – Sam Sep 21 '18 at 06:31
  • 1
    Check this [location on simulator](https://stackoverflow.com/questions/214416/set-the-location-in-iphone-simulator) – Pratik Sodha Sep 21 '18 at 07:05
  • 1
    You can replicate your current location on the simulator by going to `Debug`, `Location`, `Custom location` and entering in your coordinate on the simulator (not Xcode). To get your current coordinate, just go to `maps.google.com` and get the coordinate off the URL. – trndjc Sep 21 '18 at 07:06

0 Answers0