Antonio Lipiz
Antonio Lipiz

Reputation: 82

Current Location not showing in iOS Simulator

I'm trying to show the user's current location in an MKMapView, mapView. I ask for permission and I believe all my bases are covered but for some reason the current location does not show up on the map.

Below is the code. Do not mind the segment controls, I just use these to change the map type.

import UIKit
import MapKit

let annotationId = "annotationID";

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var segControls: UISegmentedControl!

let locationManager = CLLocationManager();
var zoomToUserLocation = false;

// Switch statements for the seg controls.

@IBAction func mapType(sender: UISegmentedControl) {
    switch segControls.selectedSegmentIndex {
    case 0:
        mapView.mapType = MKMapType.Standard
    case 1:
        mapView.mapType = MKMapType.Satellite
    case 2:
        mapView.mapType = MKMapType.Hybrid
    default:
        mapView.mapType = MKMapType.Standard 
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // We are setting the delegate equal to self here to be able to use the indentifier that deques the annotations.
    locationManager.delegate = self

    // Status of user is variable that speaks directly to (in this case) authorization status
    let status = CLLocationManager.authorizationStatus()

    if status == CLAuthorizationStatus.NotDetermined {
        locationManager.requestWhenInUseAuthorization()
    } else if  status != .Denied {
        locationManager.startUpdatingLocation()
    }

    // let annotation = MKPointAnnotation();
}

// If the status changes to authorize when in use or always authorize
// then start updating the location, if not don't do anything.

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == CLAuthorizationStatus.AuthorizedWhenInUse || status == CLAuthorizationStatus.AuthorizedAlways {
        locationManager.startUpdatingLocation()
    }
}

// If the location failed when trying to get users location execute this function
func  locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println("Error: \(error)")
    }
}

extension ViewController: MKMapViewDelegate {
    func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {

    let coordinate = userLocation.coordinate; // this sets the var coordinates to the location of the user.

    println("User Location = (\(coordinate.latitude), \(coordinate.longitude))");

    if zoomToUserLocation == true {
        let locationOfDevice: CLLocation = userLocation.location // this determines the location of the device using the users location

        let deviceCoordinate: CLLocationCoordinate2D = locationOfDevice.coordinate // determines the coordinates of the device using the location device variabel which has in it the user location.

        let span = MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1) // this determines the span in which its determined that 1 degree is 69 miles.

        let region = MKCoordinateRegion(center: deviceCoordinate, span: span) // set the center equal to the device coordinates and the span equal to the span variable we have created this will give you the region.

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

Upvotes: 3

Views: 6038

Answers (2)

Sam Spencer
Sam Spencer

Reputation: 8609

You need to set the MKMapView's showsUserLocation property to true. It's a boolean property that will display the current location (that pulsing blue dot you refer to - which, can be tinted and is not always blue).

Here's how Apple sums it up in their documentation

This property does not indicate whether the user’s position is actually visible on the map, only whether the map view should try to display it. Setting this property to true causes the map view to use the Core Location framework to find the current location and try to display it on the map. As long as this property is true, the map view continues to track the user’s location and update it periodically. The default value of this property is false.

Showing the user’s location does not guarantee that the location is visible on the map. The user might have scrolled the map to a different point, causing the current location to be offscreen. To determine whether the user’s current location is currently displayed on the map, use the userLocationVisible property.

So in your viewDidLoad function, you should set this property:

mapView.showsUserLocation = true

The zoomToUserLocation property does not control the current location dot.

Upvotes: 4

tskulbru
tskulbru

Reputation: 2346

Sometimes you need to set a custom location first to get the simulator to update and actually set a location (no idea why). What I do is go to Debug->Location->Apple just to check that the map location stuff actually works. Then later i set a custom location with coordinates which i find from google maps or similar.

Upvotes: 0

Related Questions