您的位置:首页 > 移动开发 > Swift

swift地图定位(十四)使用 MKAnnotationView自定义大头针视图

2016-11-15 03:29 676 查看
import UIKit
import MapKit

class ViewController: UIViewController {

@IBOutlet weak var mapView: MKMapView!

lazy var geoCoder: CLGeocoder = {
return CLGeocoder()
}()

override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
let point = touches.first?.location(in: mapView)
let coordinate = mapView.convert(point!, toCoordinateFrom: mapView)
let annotation = addAnnotation(coordinate, title: "title", subTitle: "subTitle")
let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
geoCoder.reverseGeocodeLocation(location) { (pls: [CLPlacemark]?, error: Error?) -> Void in
if error == nil {
let pl = pls?.first
print(pl)//Optional(祖冲之路705号, 中国上海市浦东新区张江镇祖冲之路705号 @ <+31.20264860,+121.59036900> +/- 100.00m, region CLCircularRegion (identifier:'<+31.20264850,+121.59036900> radius 49.30', center:<+31.20264850,+121.59036900>, radius:49.30m))
annotation.title = pl?.locality
annotation.subtitle = pl?.name
}
}
}

func addAnnotation(_ coordinate: CLLocationCoordinate2D, title: String, subTitle: String) -> TGAnnotation {
let annotation: TGAnnotation = TGAnnotation()
annotation.coordinate = coordinate
annotation.title = title
annotation.subtitle = subTitle
mapView.addAnnotation(annotation)
return annotation
}
}

class TGAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2DMake(0, 0)
var title: String?
var subtitle: String?
}

extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// 使用 MKAnnotationView自定义大头针
let identifier = "item"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
}
annotationView?.annotation = annotation// 重要
annotationView?.image = UIImage(named: "category_1")// 设置打头针的图片
annotationView?.centerOffset = CGPoint(x: 0, y: 0)// 设置大头针中心偏移量
annotationView?.canShowCallout = true// 设置弹框
annotationView?.calloutOffset = CGPoint(x: 10, y: 0)// 设置弹框的偏移量

let leftIV = UIImageView(frame: CGRect(x: 0, y: 0, width: 38, height: 44))
let leftImage = UIImage(named: "category_2")
leftIV.image = leftImage
annotationView?.leftCalloutAccessoryView = leftIV// 设置弹框的左侧视图

let rightIV = UIImageView(frame: CGRect(x: 0, y: 0, width: 38, height: 44))
let rightImage = UIImage(named: "category_3")
rightIV.image = rightImage
annotationView?.rightCalloutAccessoryView = rightIV// 设置弹框的右侧视图

if #available(iOS 9.0, *) {
//            let detailIV = UIImageView(frame: CGRect(x: 0, y: 0, width: 38, height: 44))
//            let detailImage = UIImage(named: "category_4")
//            detailIV.image = detailImage
//            annotationView?.detailCalloutAccessoryView = detailIV// 设置下部视图
}
annotationView?.isDraggable = true// 设置大头针可以拖动
return annotationView
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息