zoukankan      html  css  js  c++  java
  • 第十章 使用MapKit

    本项目是《beginning iOS8 programming with swift》中的项目学习笔记==》全部笔记目录

    ------------------------------------------------------------------------------------------------------------------

    1.    打开地图功能:Target-FoodPin-Capabilities-Maps:ON。
    2.    在Detail界面的Cell右边拖一个按钮,文字为Map”,自己设置背景色和字体。
    3.    再拖一个控制器到IB,接着拖一个mapKit View进去,连线Map按钮到新的控制器(push),设置identifier为:showMap。
    4.    去掉多余的map按钮:增加一个成员变量mapButton并连线,在创建单元格的地方设置是否显示mapButton。
    5.    增加一个继承自UIViewController的控制器类MapViewController, import MapKit,设置好成员变量和类的关联:

    @IBOutlet weak var mapView: MKMapView!
    var restaurant: Restaurant!

    6. 实现地址转坐标,并显示到地图上:

    override func viewDidLoad() {
        super.viewDidLoad()
       
        // 将地址字符串转换成坐标
        let geoCoder = CLGeocoder()
        geoCoder.geocodeAddressString(restaurant.location, completionHandler: { (placemarks, error) -> Void in
            if error != nil {
                println(error)
                return
            }
           
            // 取第一个坐标
            if placemarks != nil && placemarks.count > 0 {
                let placemark = placemarks[0] as CLPlacemark
               
                // 添加Annotation
                let annotation = MKPointAnnotation()
                annotation.title = self.restaurant.name
                annotation.subtitle = self.restaurant.type
                annotation.coordinate = placemark.location.coordinate
               
                self.mapView.showAnnotations([annotation], animated: true)
                self.mapView.selectAnnotation(annotation, animated: true)
            }
        })
    }

    7. 界面跳转时传递restaurant数据:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showMap" {
            let destinationController = segue.destinationViewController as MapViewController
            destinationController.restaurant = self.restaurant
        }
    }

    8. 给Annotation View加一个图片

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
       
        // placemark和当前地址都是annotation
        if annotation .isKindOfClass(MKUserLocation) {
            // 当前地址使用默认的蓝色小点表示
            return nil
        }
       
        // annotation重用
        let identifier = "MyPin"
        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
       
        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
           
            // 设置左侧图片
            annotationView.canShowCallout = true
            let leftIconView = UIImageView(frame: CGRect(x: 0, y: 0,  47, height: 47))
            leftIconView.image = UIImage(named: restaurant.image)
            annotationView.leftCalloutAccessoryView = leftIconView
        }
       
        return annotationView
    }
  • 相关阅读:
    JAVA8时间插入mysql少了8小时的解决办法
    Kubernetes 部署 Mysql 8.0 数据库(单节点)
    Spring Boot + MyBatisPlus,简直完美!
    美团面试官:生成订单后一段时间不支付订单会自动关闭的功能该如何实现?越详细越好~
    100道Java并发和多线程基础面试题大集合(含解答),这波面试稳了~
    windows端口占用快速查询解决方法
    解决github无法连接错误 OpenSSL SSL_connect: Connection was reset in connection to github.com:443
    Mysql:好好的索引,为什么要下推?
    漫画 | 程序员的悲哀是什么?
    聊聊那些年的骚操作!!!
  • 原文地址:https://www.cnblogs.com/tangzhengyue/p/4308042.html
Copyright © 2011-2022 走看看