[英]Swift2 iOS9 - how to get custom image instead of default annotation red pin in my code
Here is my code. 这是我的代码。 I have tried all the solutions available but I am not able to put image I wish to instead of the default annotation red pin in my code.
我已经尝试了所有可用的解决方案,但无法在代码中放入希望的图像,而不是默认的注释红色图钉。
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
var location = CLLocationCoordinate2DMake(18.956449, 72.810597)
var span = MKCoordinateSpanMake(0.009, 0.009)
var region = MKCoordinateRegion(center: location, span: span)
map.setRegion(region, animated: true)
var annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Wilson College"
annotation.subtitle = "Commerce College"
map.addAnnotation(annotation)
print("showing on map")
mapView(map, viewForAnnotation: annotation)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "MyPin"
print("inside viewForAnnotation")
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)
// Reuse the annotation if possible
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil
{
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
annotationView!.canShowCallout = true
annotationView!.image = UIImage(named: "VRtest.png")
annotationView!.rightCalloutAccessoryView = detailButton
print("editing annotation")
}
else
{
annotationView!.annotation = annotation
}
return annotationView
}
Any help is appreciated. 任何帮助表示赞赏。 thank you
谢谢
What do you want is to customize the default annotation? 您要自定义默认注释吗? if YES, you can refer this post How to create Custom MKAnnotationView and custom annotation title and subtitle
如果是,则可以参考此文章如何创建自定义MKAnnotationView和自定义注释标题和副标题
Here is the example in Swift 2 这是Swift 2中的示例
class MapViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
let initialLocation = CLLocation(latitude: 34.074094, longitude: -118.396329)
let regionRadius: CLLocationDistance = 1000
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
regionRadius * 2.0, regionRadius * 2.0)
self.mapView.setRegion(coordinateRegion, animated: true)
}
override func viewDidLoad() {
self.mapView.delegate = self
self.centerMapOnLocation(initialLocation)
let location = CLLocationCoordinate2D(latitude: 34.074094, longitude: -118.396329)
let annotation = CustomAnnotation(coordinate: location, imageName: "pinIcon", title: "Test", subtitle: "Detail", enableInfoButton: false)
self.mapView.addAnnotation(annotation);
}
}
extension MapViewController: MKMapViewDelegate {
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if (annotation is MKUserLocation) {
return nil
}
let pinIdentifier = "pinIdentifier"
var view: MKAnnotationView
if (annotation.isKindOfClass(CustomAnnotation)) {
if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(pinIdentifier)
as MKAnnotationView? {
dequeuedView.annotation = annotation
view = dequeuedView
} else {
view = MKAnnotationView(annotation: annotation, reuseIdentifier: pinIdentifier)
view.canShowCallout = true
//Set custome image
let customAnnotation = annotation as! CustomAnnotation
view.image = UIImage(named:customAnnotation.imageName!)
}
return view
}
return nil
}
}
class CustomAnnotation : NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var imageName: String?
var title: String?
var subtitle: String?
var enableInfoButton : Bool
init(coordinate: CLLocationCoordinate2D, imageName: String, title: String, subtitle: String, enableInfoButton : Bool) {
self.coordinate = coordinate
self.imageName = imageName
self.title = title
self.subtitle = subtitle
self.enableInfoButton = enableInfoButton;
}
}
This is my custom image displaying on the screen 这是我在屏幕上显示的自定义图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.