无法在视图上显示地图

时间:2016-05-21 02:00:07

标签: ios swift mapkit

过去一周我一直在学习Xcode(swift 1.2),我刚刚开始研究Map Kit并且碰到了一堵砖墙。

我正在关注本教程:MapKit Location

我已将mapkit添加到我的视图中,并将以下代码添加到控制器中:

@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)
}

然而,当我运行应用程序时,我看到以下图像:

有人可以说明为什么我无法看到实际地图而不是瓷砖吗?

更新

以下代码来自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)
}

2 个答案:

答案 0 :(得分:1)

一些故障排除方法:

  • 仔细检查您的IBOutlet是否已连接
  • 检查您的模拟器是否有网络连接
  • viewDidAppear进行更改以获得视觉反馈
  • 试用实际设备
  • 尝试不同的位置,更大的半径
  • 仔细检查viewDidLoad
  • 后您是否再次更改坐标或缩放级别
  • 尝试在地图上缩小和平移以查看内容是否更改

答案 1 :(得分:0)

设置委托和委托方法,使用此示例代码,

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

   }

//委托方法:

  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
   }