我想使用个人坐标更改 map 位置。 (当我说“个人”时,不是“用户位置”,而只是随机坐标)。
这是我的 MapView Controller :
class MapSubViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
let regionRadius: CLLocationDistance = 1000
override func viewDidLoad() {
super.viewDidLoad()
self.mapLocationUpdate(70.0, longitude: 70.0, title: "")
}
func mapLocationUpdate(latitude: Double, longitude: Double, title: String) {
let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
self.centerMapOnLocation(location, title: title)
}
func centerMapOnLocation(location: CLLocationCoordinate2D, title: String) {
let coordinateRegion = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(coordinateRegion, animated: false)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = title
self.mapView.addAnnotation(annotation)
}
}
当我想更改 map 的位置时,我告诉我的函数“mapLocationUpdate”:
例如:
func addMapView(xPosition: Double, yPosition: Double, title: String) {
self.mapSubViewController = (self.storyboard!.instantiateViewControllerWithIdentifier("MapSubViewController") as? MapSubViewController)!
self.mapSubViewController.mapLocationUpdate(xPosition, longitude: yPosition, title: clubName)
[...]
}
当执行“self.mapSubViewController.mapLocationUpdate(...)”行时,我的应用程序崩溃。当 Controller 的“viewDidLoad”告诉函数“mapLocationUpdate”时,应用程序将崩溃不会崩溃。
我认为这是因为 map 收费后无法在代码中更改 map 位置。
我该怎么办?
抱歉我的英语不好,我尽力了:-)!
谢谢!
最佳答案
为了在 map 上设置自定义位置,您应该执行与此类似的操作:
let pLat = 43.6109200
let pLong = 3.8772300
let center = CLLocationCoordinate2D(latitude: pLat, longitude: pLong)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region, animated: true)
关于ios - 更改 map 坐标(MapKit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36494181/