MKMapView在加载注解时遇到问题
问题描述:
我在加载MapView时无法放大注解/引脚。MKMapView在加载注解时遇到问题
加载MapView时,我希望它开始缩小,然后放大注释/引脚。 “动画”设置为“true”。我不确定这里有什么问题。
当地图加载完成后,它会短暂地显示一个灰色的屏幕,然后围绕它加载卫星地图。
谢谢!
使用斯威夫特3
地图代码:
@IBOutlet weak var mapView: MKMapView!
}
override func viewDidLoad() {
super.viewDidLoad()
let distanceSpan:CLLocationDegrees = 300
let redRocks:CLLocationCoordinate2D = CLLocationCoordinate2DMake(39.665496, -105.205438)
let redRocksPin = RedRocksPin(title: "Jerry Garcia 1/2/72", subtitle: "Red Rocks Amphitheatre", coordinate: redRocks)
mapView.addAnnotation(redRocksPin)
mapView.setRegion(MKCoordinateRegionMakeWithDistance(redRocks, distanceSpan, distanceSpan), animated: true)
针类:
import MapKit
class RedRocksPin: NSObject, MKAnnotation {
var title: String?
var subtitle: String?
var coordinate: CLLocationCoordinate2D
init(title:String, subtitle:String, coordinate: CLLocationCoordinate2D) {
self.title = title
self.subtitle = subtitle
self.coordinate = coordinate
}
答
在类声明使这些类变量:
let distanceSpan:CLLocationDegrees = 300
let redRocks:CLLocationCoordinate2D = CLLocationCoordinate2DMake(39.665496, -105.205438)
在viewWillAppear中,d O此:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animted)
view.layoutIfNeeded()
let redRocksPin = RedRocksPin(title: "Jerry Garcia 1/2/72", subtitle: "Red Rocks Amphitheatre", coordinate: redRocks)
mapView.addAnnotation(redRocksPin)
}
然后将此:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
mapView.setRegion(MKCoordinateRegionMakeWithDistance(redRocks, distanceSpan, distanceSpan), animated: true)
}
那么这样做是在你的viewWillAppear中,它将绘制的MapView,所以你可以添加注释,而不会导致系统崩溃,那么在viewdidar中,它只会放大您添加的注释。
不知道这是最好的方法,但要尝试。
谢谢你的回应。为了将这些行添加到类声明中,它是否在IBOutlet所在的位置?或者在viewDidLoad之后?此外,我遇到了错误,添加了两个“覆盖”(呼叫中参数#1缺少参数)(使用未解析标识符'redRocks')。控制器已导入UIKit和MapKit。谢谢! – Miles
好,所以对于超级viewDidAppear&viewWillAppear,我会更新它们,我忘了添加动画。 对于类声明的问题,你把这些放在括号后面,类名后面。因此,以上IBOutlet的位置在哪里,取决于你如何组织你的代码。 – Jay
仍然收到那些相同的错误。 '重写func viewWillAppear(){'和'重写func viewDidAppear(){'正在显示“方法不会覆盖任何超类中的方法”。 'super.viewDidAppear(animated)'和'uper.viewWillAppear(animated)'display“使用未解析的标识符'动画”。你的代码中的viewWillAppear是否存在拼写错误?谢谢。 – Miles