我使用Swift 3和Xcode 10 beta 3,我需要在地图上为我的引脚使用自定义图像。我需要避免红色注解引脚,并使用一些自定义的标志,我为这一点。我尝试了堆叠中找到的所有解决方案,但没有任何帮助。这是我的密码:
import UIKit
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager = CLLocationManager()
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
//let span:MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1)
let locationANTOMI:CLLocationCoordinate2D = CLLocationCoordinate2DMake(45.4509339, 9.1713609)
let locationANTOTO:CLLocationCoordinate2D = CLLocationCoordinate2DMake(45.06666, 7.68826)
//let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
//map.setRegion(region, animated: true)
let annotationANTOMI = MKPointAnnotation()
annotationANTOMI.coordinate = locationANTOMI
annotationANTOMI.title = "ANTONIOLI MILANO"
map.addAnnotation(annotationANTOMI)
let annotationANTOTO = MKPointAnnotation()
annotationANTOTO.coordinate = locationANTOTO
annotationANTOTO.title = "ANTONIOLI TORINO"
map.addAnnotation(annotationANTOTO)
}
}我怎么发动汽车呢?
发布于 2018-07-08 23:50:36
我通常的做法是创建一个新的快速文件,它将是您从MKAnnoatation继承的自定义注释。下面的例子
import MapKit
class MyAnnotation: NSObject, MKAnnotation {
let title: String?
let subtitle: String?
let coordinate: CLLocationCoordinate2D
var image: UIImage? = nil
init(title: String, subtitle: String, coordinate: CLLocationCoordinate2D) {
self.title = title
self.subtitle = subtitle
self.coordinate = coordinate
//self.image
super.init()
}
}在必须使用CLCoordinate的地方,您需要初始化注释。然后,使用自定义图像设置图像属性。MyAnnotation.image = "myImage.png". You will then need to add your annotation to your map viewmapView.addAnnotations(MyAnnotation).我还从MKMapViewDelegate实现了下面的方法(确保您在类中继承了这个方法)。这样用户就可以点击注释并接收有关它的信息。希望这能有所帮助。
在您的视图中,控制器:
let marker = MyAnnotation(title: "title" as! String, subtitle: "subtitle" as! String, coordinate: CLLocationCoordinate2D(latitude: latitude, longitude: longitude))
marker.image = UIImage("my image.png")
self.mapView.addAnnotations(marker)
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? MyAnnotation {
let identifier = "identifier"
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.image = annotation.image //add this
annotationView?.canShowCallout = true
annotationView?.calloutOffset = CGPoint(x: -5, y: 5)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView
return annotationView
}
return nil
}https://stackoverflow.com/questions/51236577
复制相似问题