过去一周我一直在学习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)
}
答案 0 :(得分:1)
一些故障排除方法:
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
}