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
    }
  • 相关阅读:
    prototype.js超强的javascript类库
    MySQL Server Architecture
    Know more about RBA redo block address
    MySQL无处不在
    利用Oracle Enterprise Manager Cloud Control 12c创建DataGuard Standby
    LAMP Stack
    9i中DG remote archive可能导致Primary Database挂起
    Oracle数据库升级与补丁
    Oracle为何会发生归档日志archivelog大小远小于联机重做日志online redo log size的情况?
    Oracle Ksplice如何工作?How does Ksplice work?
  • 原文地址:https://www.cnblogs.com/tangzhengyue/p/4308042.html
Copyright © 2011-2022 走看看