使用MapKit将Swins添加到Map中 - Swift 3.0 [英] Adding Pins to Map using MapKit - Swift 3.0
问题描述
新编码器,试图弄清楚如何使用MapKit。目标是创建一个用户可以使用其地址添加引脚的地图。但是,我现在的步骤,我很难搞清楚如何将地图添加到地图上。
New coder, trying to figure out how to use MapKit. The goal is to create a map that users can add pins to using their address. However, the step I am at now, I am having trouble figuring out how to add pins to the map at all.
如何在地图上添加图钉?到目前为止,我一直在努力弄清楚如何使用注释。
How can I add a pin to the map? I have been struggling to figure out how to use annotations thus far.
这就是我希望得到的帮助/指导。谢谢!
That's what I'm hoping for help/direction with. Thanks!
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate
{
@IBOutlet weak var bigMap: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.bigMap.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02))
self.bigMap.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Errors " + error.localizedDescription)
}
}
推荐答案
它们在MapKit中被称为注释
,你可以这样实例化它们:
They're called annotations
in the MapKit and you instantiate them like so:
让注释= MKPointAnnotation()
然后在 viewDidLoad()
方法只需设置坐标并将其添加到地图中,如:
then in the viewDidLoad()
method just set the coordinates and add them to the map like:
annotation.coordinate = CLLocationCoordinate2D(latitude: 11.12, longitude: 12.11)
mapView.addAnnotation(annotation)
数字是你的坐标
这篇关于使用MapKit将Swins添加到Map中 - Swift 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!