如何从注释中获取属性数据? 我有:
listSelected = [Properties]()
我有问题从list属性获取图像以将其设置为pin 还有另一个问题是让我的列表在alertcontrollr中用来显示我的属性。
那怎么办呢?
func drowPropertyOnMap(list: [Properties]) {
self.spinner.isHidden = false
self.spinner.startAnimating()
var annotations = [MKPointAnnotation]()
for (index, property) in list.enumerated() {
let propertyLat: CLLocationDegrees = property.Latitude as! CLLocationDegrees
let propertyLong: CLLocationDegrees = property.Longitude as! CLLocationDegrees
let coordinate = CLLocationCoordinate2D(latitude: propertyLat, longitude: propertyLong)
let span: MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1)
let center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(center, span)
self.mapView.setRegion(region, animated: true)
let pointAnnotation = MKPointAnnotation()
pointAnnotation.coordinate = coordinate
pointAnnotation.title = property.Title
pointAnnotation.subtitle = property.Description
annotations.append(pointAnnotation)
}
// stop spinner
self.spinner.stopAnimating()
self.spinner.isHidden = true
self.mapView.addAnnotations(annotations)
}
// MARK: - MapViewDelegate
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if (annotation is MKUserLocation) {
//if annotation is not an MKPointAnnotation (eg. MKUserLocation),
//return nil so map draws default view for it (eg. blue dot)...
return nil
}
let reuseId = "test"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
annotationView?.image = UIImage(named:"homeIconPin")
annotationView?.isEnabled = true
annotationView?.canShowCallout = true
let leftIconView = UIImageView(frame: CGRect(x: 0, y: 0, width: 53, height: 53))
leftIconView.image = UIImage(named: "IMG_3")
annotationView?.leftCalloutAccessoryView = leftIconView
let btn = UIButton(type: .infoLight)
annotationView?.rightCalloutAccessoryView = btn
return annotationView
} else {
//we are re-using a view, update its annotation reference...
annotationView?.annotation = annotation
}
return annotationView
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let property = view.annotation as! Properties
let placeName = property.Title! as String
let placeInfo = property.Description! as String
let ac = UIAlertController(title: placeName, message: placeInfo, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
所以我需要调用属性:
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
}
如何制作?
答案 0 :(得分:3)
我正在解决问题,因此我们需要从 MKPointAnnotation
添加新的 Class从注释获取索引路径
import Foundation
import MapKit
class Annotation: MKPointAnnotation {
var index = 0
}
之后,我们将进入控制器并添加代码
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var listSelected = [Properties]()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
drowPropertyOnMap(list: listSelected)
}
// MARK: - drowPropertyOnMap
func drowPropertyOnMap(list: [Properties]) {
self.spinner.isHidden = false
self.spinner.startAnimating()
var annotations = [MKPointAnnotation]()
for (index, property) in list.enumerated() {
let propertyLat: CLLocationDegrees = property.Latitude as! CLLocationDegrees
let propertyLong: CLLocationDegrees = property.Longitude as! CLLocationDegrees
let coordinate = CLLocationCoordinate2D(latitude: propertyLat, longitude: propertyLong)
let span: MKCoordinateSpan = MKCoordinateSpanMake(0.5, 0.5)
let center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(center, span)
self.mapView.setRegion(region, animated: true)
let pointAnnotation = Annotation()
pointAnnotation.index = index
pointAnnotation.coordinate = coordinate
pointAnnotation.title = property.Title
pointAnnotation.subtitle = property.Description
annotations.append(pointAnnotation)
}
// stop spinner
self.spinner.stopAnimating()
self.spinner.isHidden = true
self.mapView.addAnnotations(annotations)
}
// MARK: - MapViewDelegate
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is Annotation)
{
return nil
}
let reuseId = "test"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
annotationView?.image = UIImage(named:"homeIconPin")
annotationView?.isEnabled = true
annotationView?.canShowCallout = true
if let myAnnotation = annotation as? Annotation {
let selectedProp = listSelected[myAnnotation.index]
let imageString = selectedProp.Image
let leftIconView = UIImageView(frame: CGRect(x: 0, y: 0, width: 53, height: 53))
leftIconView.sd_setImage(with: URL(string: imageString ?? ""), placeholderImage: UIImage(named: "placeholderProperty.png"))
annotationView?.leftCalloutAccessoryView = leftIconView
}
let btn = UIButton(type: .infoLight)
annotationView?.rightCalloutAccessoryView = btn
return annotationView
} else {
//we are re-using a view, update its annotation reference...
annotationView?.annotation = annotation
}
return annotationView
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
homeView.isHidden = false
if let myAnnotation = view.annotation as? Annotation {
let selectedProp = listSelected[myAnnotation.index]
self.selectedTableData = selectedProp
self.homeTitleLabel.text = selectedProp.Title
self.homeSubtitleLabel.text = selectedProp.Description
let imageString = selectedProp.Image
self.homeImageView.sd_setImage(with: URL(string: imageString ?? ""), placeholderImage: UIImage(named: "placeholderProperty.png"))
}
}
}