ios 使用 MapKit - Swift 3.0 向地图添加图钉
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40331986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding Pins to Map using MapKit - Swift 3.0
提问by roguephillips
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.
新编码员,试图弄清楚如何使用 MapKit。目标是创建一个地图,用户可以使用他们的地址添加图钉。但是,在我现在的步骤中,我根本无法弄清楚如何向地图添加图钉。
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)
}
}
回答by Mislav Javor
They're called annotations
in the MapKit and you instantiate them like so:
它们annotations
在 MapKit 中被调用,您可以像这样实例化它们:
let annotation = MKPointAnnotation()
let annotation = MKPointAnnotation()
then in the viewDidLoad()
method just set the coordinates and add them to the map like:
然后在viewDidLoad()
方法中设置坐标并将它们添加到地图中,如:
annotation.coordinate = CLLocationCoordinate2D(latitude: 11.12, longitude: 12.11)
mapView.addAnnotation(annotation)
The numbers are your coordinates
数字是你的坐标
回答by danner.tech
The way that I learned how to do this is:
我学习如何做到这一点的方法是:
In the same function as viewDidLoad(), place the following lines of code:
let annotation = MKPointAnnotation() annotation.title = "Your text here" //You can also add a subtitle that displays under the annotation such as annotation.subtitle = "One day I'll go here..." annotation.coordinate = center
在与 viewDidLoad() 相同的函数中,放置以下代码行:
let annotation = MKPointAnnotation() annotation.title = "Your text here" //You can also add a subtitle that displays under the annotation such as annotation.subtitle = "One day I'll go here..." annotation.coordinate = center
This is the only place I can see where you have a coordinate
if all else fails, just add the coordinate (search on Google if you
need to know how to create a coordinate) and set it equal to the
"annotation.coordinate" variable
如果所有其他方法都失败,这是我唯一能看到坐标的地方,只需添加坐标(如果您
需要知道如何创建坐标,请在 Google 上搜索)并将其设置为等于
“annotation.coordinate”变量
map.addAnnotation(annotation)
- Dassit!
- 混蛋!