注释未放置在地图上
Annotations not being placed on map
我正在尝试在地图上放置注释(图钉),但图钉未显示在模拟器上(未放置图钉)。我在代码中没有收到任何错误。下面是我的 viewDidLoad.
import UIKit
import MapKit
class MapViewController: UIViewController {
var itemStore: ItemStore!
var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView = MKMapView()
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: 40.71304, longitude: -74.0072)
annotation.title = "Test"
mapView.addAnnotation(annotation)
}
删除 mapView = MKMapView()
。
此行创建一个新的 MKMapView
实例,而您要做的是使用您在故事板上创建的实例。如果您不使用情节提要,那么您应该使用 MKMapView
的另一个初始化程序来设置它的大小。
创建一个从故事板到视图控制器的出口。另外,我可能会将 MKMapViewDelegate 添加到您的视图控制器中。
import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
var itemStore: ItemStore!
@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: 40.71304, longitude: -74.0072)
annotation.title = "Test"
self.mapView.addAnnotation(annotation)
}
初始化新的 MKMapview 会产生新的实例。这意味着什么都不会存在。这就是注释可能无法放置的原因。尝试删除该初始化并尝试连接到 IBOutlet(如果有)。否则,初始化 MKMapview 完全编程并为它定义 frame
。
我正在尝试在地图上放置注释(图钉),但图钉未显示在模拟器上(未放置图钉)。我在代码中没有收到任何错误。下面是我的 viewDidLoad.
import UIKit
import MapKit
class MapViewController: UIViewController {
var itemStore: ItemStore!
var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView = MKMapView()
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: 40.71304, longitude: -74.0072)
annotation.title = "Test"
mapView.addAnnotation(annotation)
}
删除 mapView = MKMapView()
。
此行创建一个新的 MKMapView
实例,而您要做的是使用您在故事板上创建的实例。如果您不使用情节提要,那么您应该使用 MKMapView
的另一个初始化程序来设置它的大小。
创建一个从故事板到视图控制器的出口。另外,我可能会将 MKMapViewDelegate 添加到您的视图控制器中。
import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
var itemStore: ItemStore!
@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: 40.71304, longitude: -74.0072)
annotation.title = "Test"
self.mapView.addAnnotation(annotation)
}
初始化新的 MKMapview 会产生新的实例。这意味着什么都不会存在。这就是注释可能无法放置的原因。尝试删除该初始化并尝试连接到 IBOutlet(如果有)。否则,初始化 MKMapview 完全编程并为它定义 frame
。