更改地图坐标 (MapKit)
Change coordinate of map (MapKit)
我想用个人坐标更改我的地图的位置。
(当我说 "personnal" 时,不是 "user location" 而是随机坐标)。
这是我的 MapView 控制器:
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)
}
}
我告诉我的函数 "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(...)" 时崩溃。 当控制器的 "viewDidLoad" 告诉函数 "mapLocationUpdate" 时,应用程序没有崩溃。
我认为是地图收费后无法在代码中更改地图位置。
我该怎么做?
对不起我的英语,我尽力了:-)!
谢谢!
要在地图上设置自定义位置,您应该执行与此类似的操作:
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)
我想用个人坐标更改我的地图的位置。 (当我说 "personnal" 时,不是 "user location" 而是随机坐标)。
这是我的 MapView 控制器:
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)
}
}
我告诉我的函数 "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(...)" 时崩溃。 当控制器的 "viewDidLoad" 告诉函数 "mapLocationUpdate" 时,应用程序没有崩溃。
我认为是地图收费后无法在代码中更改地图位置。
我该怎么做?
对不起我的英语,我尽力了:-)!
谢谢!
要在地图上设置自定义位置,您应该执行与此类似的操作:
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)