第十章 使用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, width: 47, height: 47))
        leftIconView.image = UIImage(named: restaurant.image)
        annotationView.leftCalloutAccessoryView = leftIconView
    }
   
    return annotationView
}
复制代码

 

posted @   唐小喵  阅读(495)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示