0

Im trying to show the user's location and a map of the location using MKMapView but I get an error when doing so. I get an error for this line of code: map.delegate = self. Here's the full code, can someone tell me what I'm doing wrong. I looked at other tutorials and they have it the same way I have it setup and it works for them. Thank you!

import Foundation
import SpriteKit
import CoreLocation
import MapKit



class MapScene: SKScene, CLLocationManagerDelegate, MKMapViewDelegate {
var locationMangaer = CLLocationManager()
var map : MKMapView!

override func didMoveToView(view: SKView) {

    locationMangaer.delegate = self
    locationMangaer.desiredAccuracy = kCLLocationAccuracyBest
    locationMangaer.requestWhenInUseAuthorization()
    locationMangaer.startUpdatingLocation()
    locationMangaer.requestAlwaysAuthorization()

    map.delegate = self
    map.showsUserLocation = true


}



//DELEGATE METHODS

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {

    println("error")
}



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


    var userLocation: CLLocation = locations[0] as! CLLocation
    locationMangaer.stopUpdatingLocation()

    let location = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude)
    let span = MKCoordinateSpanMake(0.05, 0.05)
    let region = MKCoordinateRegion(center: location, span: span)

    map.setRegion(region, animated: true)
    map.centerCoordinate = userLocation.coordinate


    println("call")
}
coding22
  • 689
  • 1
  • 7
  • 28

2 Answers2

3

When you declare your map variable, you do not provide a default value, and it doesn't instantiate properly.

What you need to do is hook it up to a view in your storyboard with an @IBOutlet,

@IBOutlet weak var map : MKMapView!

or if you are manually creating your view hierarchy, instantiate a MKMapView with a default value.

var map : MKMapView! = MKMapView()

I recommend the former.

72A12F4E
  • 1,744
  • 1
  • 14
  • 28
0

Tried your code. Make it work with the following changes:

Comment out

locationMangaer.requestAlwaysAuthorization()

Comment out

mapView.centerCoordinate = userLocation.coordinate

Add the following String entry to Info.plist:

NSLocationWhenInUseUsageDescription

with any text.

rshev
  • 4,086
  • 1
  • 23
  • 32
  • I did what you said and I don't see the map. I only get a black screen. – coding22 Aug 25 '15 at 16:59
  • Does it matter that im in SpriteKit? – coding22 Aug 25 '15 at 17:01
  • @coding22 try to instantiate `MKMapView` manually as `let map = MKMapView()`. I've used `IBOutlet` for testing. Think it doesn't matter. Try to add `println(error)` in error delegate function, curious what may be there. – rshev Aug 25 '15 at 17:21
  • I don't get an error when I do this and nothing prints out in the console and there is no map. Also I tried using an IBOutlet and the map shows up but I need it to come when I press a button. How would I do that? – coding22 Aug 25 '15 at 17:25
  • @coding22 make the map with `alpha = 0.0`, and then animate it on button press via `UIView.animateWithDuration(0.5, animations: { () -> Void in map.alpha = 1.0 })` – rshev Aug 25 '15 at 17:37