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 } }
相关文章推荐
- 地图与定位(三)自定义大头针视图
- swift地图定位(十二)大头针的基本使用
- swift地图定位(十三)大头针系统视图
- ios 地图大头针自定义显示图片 MKAnnotationView
- iOS地图和定位服务(自定义大头针)
- 天天记录 - 自定义ViewGroup使用Adapter提供视图
- MapKit地图使用和自定义大头针,画线,导航
- 使用 Swift 构建自定义的ActivityIndicator View
- Swift实现自定义AlertView,灵活添加自定义视图到AlertView,可响应事件
- 【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等
- ios 实现地图的定位,查询,自定义大头针
- Swift - 使用网格(UICollectionView)的自定义布局实现复杂页面
- IOS 地图定位,标记位置,获取经纬度,MKMapView的使用
- iOS 大头针 MKAnnotationView 自定义
- iOS:UIMapView地图视图控件的简单使用
- 地图的使用和自定义大头针
- IOS 地图定位,标记位置,获取经纬度,MKMapView的使用
- Android 使用百度地图SDK 定位和显示基础地图常见错误 locationclient.start()和mapview is null等等
- 地图与定位之大头针视图