Code Ratchet
Code Ratchet

Reputation: 6029

Unable to display map on view

I've been learning Xcode (swift 1.2) for the past week, I've just started looking into the Map Kit and I've hit a brick wall.

I'm following this tutorial : MapKit Location

I've added the mapkit to my view, and added the following code into the controller:

@IBOutlet weak var mapView: MKMapView!

let regionRadius: CLLocationDistance = 1000

@IBOutlet var menuButton: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()

    let initialLocation = CLLocation(latitude: 21.282778, longitude: -157.829444)

    centerMapOnLocation(initialLocation)

    // Do any additional setup after loading the view.
}

func centerMapOnLocation(location: CLLocation) {
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
    mapView.setRegion(coordinateRegion, animated: false)
}

Yet when I run the app, I see the following image:

Can someone please shed some light into why I'm unable to see the actual map instead of the tiles?

Update

Code below afer feedback from ansers:

  @IBOutlet weak var mapView: MKMapView!

@IBOutlet var menuButton: UIBarButtonItem!

var locationManage = CLLocationManager()

var locateCoordinate = CLLocationCoordinate2D()

override func viewDidLoad() {
    super.viewDidLoad()

    let initialLocation = CLLocation(latitude: 21.282778, longitude: -157.829444)

    centerMapOnLocation(initialLocation)





}

func centerMapOnLocation(location: CLLocation) {
    var coordin: CLLocationCoordinate2D = location.coordinate

    var viewRegion: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(coordin, 500, 500)
    var adjustedRegion: MKCoordinateRegion = self.mapView.regionThatFits(viewRegion)
    self.mapView.setRegion(adjustedRegion, animated: true)
}

Upvotes: 0

Views: 130

Answers (2)

MirekE
MirekE

Reputation: 11555

Some troubleshooting ideas:

  • double check that your IBOutlet is connected
  • check if your simulator has network connection
  • make the change in viewDidAppear to get visual feedback
  • try on actual device
  • try different location, greater radius
  • double check that you are not changing the coordinates or zoom level again after viewDidLoad
  • try zooming out and panning on the map to see if the contents changes

Upvotes: 1

Iyyappan Ravi
Iyyappan Ravi

Reputation: 3245

Set the delegate and delegate methods, Use this example Code,

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var mapViewOwn: MKMapView!

    var locationManage = CLLocationManager()

    var locateCoordinate = CLLocationCoordinate2D()

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib..

    mapViewOwn.delegate = self
    locationManage.delegate = self

    if CLLocationManager.authorizationStatus() == .NotDetermined {
        self.locationManage.requestWhenInUseAuthorization()
    }

    mapViewOwn.showsUserLocation = true

    mapViewOwn.mapType = MKMapType.Standard
    mapViewOwn.zoomEnabled = true
    mapViewOwn.scrollEnabled = true

    locationManage.distanceFilter = kCLDistanceFilterNone
    locationManage.desiredAccuracy = kCLLocationAccuracyBest

   }

//delegate Methods:

  func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

  }

  func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
    let region : MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 1000, 1000)
    mapViewOwn.setRegion(mapViewOwn.regionThatFits(region), animated: true)
   }

   func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {        
       //code here
   }

   func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
      //code here
   }

Upvotes: 0

Related Questions