本项目是《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 > {
let placemark = placemarks[] 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: , y: , width: , height: ))
leftIconView.image = UIImage(named: restaurant.image)
annotationView.leftCalloutAccessoryView = leftIconView
} return annotationView
}

第十章 使用MapKit的更多相关文章

  1. swift项目实战FoodPin目录

    好吧,据说写博客能够找到好工作,那我也来分享一个项目吧! 自己自学iOS开发也有半年多了,现在就来分享一个swift的小项目吧!这个项目的来源是<Beginning iOS8 programmi ...

  2. MapKit/CoreLocation框架 总结

    MapKit/CoreLocation框架 /*英译 core:核心 track:踪迹 current:当前 statellite:卫星 hybird:混合  region:范围 annotation ...

  3. 读《编写可维护的JavaScript》第九、十章总结

    第九章 将配置数据从代码中分离出来 9.2 抽离配置数据 这章比较好理解,也非常常见,作者给的俩个例子就能说明一切: // 将配置数据藏在代码中 function validate(value) { ...

  4. 0526 Sprint1个人总结 & 《构建之法》第八、九、十章

    Sprint1的个人总结: 我是老人组的成员,我们是做一款四则运算训练的软件.然后我是接了界面设计的任务,所以我任务将会是sprint1中相对重一点的一方.我的感觉是,界面要做得充满童趣,毕竟我们的软 ...

  5. Getting Started With Hazelcast 读书笔记(第八章-第十章)

    第八章到第十章就是一些介绍性的描述,吹的就是Hazelcast能使用在各种地方..   第八章 -从外面看 1.Hazelcast做了一个memcache的java实现,方便py和php使用. 2.可 ...

  6. [iOS 利用MapKit和CoreLocation框架打造精简的定位和导航]

    运行效果:            一.利用<CoreLocation/CoreLocation.h>定位 创建变量 CLLocationManager *locationManager , ...

  7. iOS开发——高级篇——地图 MapKit

    一.简介 1.在移动互联网时代,移动app能解决用户的很多生活琐事,比如周边:找餐馆.找KTV.找电影院等等导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达 在上述应用中,都用到了定位 ...

  8. Leonbao:MapKit学习笔记

    以下仅作了解, 实际使用以百度地图居多, 因为百度地图有动态路径规划等接口 MapKit学习笔记    原帖: http://www.cocoachina.com/bbs/read.php?tid-6 ...

  9. MapKit 添加大头针

    #import "ViewController.h" #import <MapKit/MapKit.h> #import "MYAnnotation.h&qu ...

随机推荐

  1. HTTP 协议的头部

    转自:http://network.51cto.com/art/201509/490594.htm HTTP首部字段有四种类型:通用首部字段,请求首部字段,响应首部字段,实体首部字段. 通用首部字段: ...

  2. jquery实践案例--验证电子邮箱

    <input type="email" name="email" id="email" value="" onpa ...

  3. JavaScript 题目破解过程与解析

    题目来源 https://www.hackthissite.org/missions/javascript/ HackThisSite JavaScript mission 1-7 1 我先尝试输入  ...

  4. 怎么学习计算电磁学【QUORA】

    链接 There are several resources. But it depends on what you actually want to learn...Let me explain: ...

  5. 2014 UESTC 暑前集训队内赛(1) 解题报告

    A.Planting Trees 排序+模拟 常识问题,将耗时排一个序,时间长的先种,每次判断更新最后一天的时间. 代码: #include <iostream> #include < ...

  6. MySQL数据库学习笔记(五)----MySQL字符串函数、日期时间函数

    一.常见字符串函数: 1.CHAR_LENGTH  获取长度(字符为单位) 2.FORMAT  格式化 3.INSERT  替换的方式插入 4.INSTR  获取位置 5.LEFT/RIGHT  取左 ...

  7. 第2章 面向对象的设计原则(SOLID):3_依赖倒置原则(DIP)

    3. 依赖倒置原则(Dependence Inversion Principle,DIP) 3.1 定义 (1)要依赖抽象,不要依赖具体的实现类.简单的说就是对抽象(或接口)进行编程,不要依赖实现进行 ...

  8. Python创建Cocos2d-x 2.2方法

    把创建项目做成一个批处理,当创建项目时可以省时省力很多. 操作步骤 1.在 E:\cocos2d-x-2.2.1\tools\project-creator 目录下创建 create_project. ...

  9. Linux环境安装Jenkins

    安装环境: CenOS 6.4 JDK_1.6.0_23 一.安装前检查环境 检查是否安装JDK: java --version 二.安装Jenkins 1. 添加Jenkins的源(reposito ...

  10. java foreach 循环原理

    java foreach 语法是在jdk1.5时加入的新特性,主要是当作for语法的一个增强,那么它的底层到底是怎么实现的呢?因为面试时被问到,所以在这边做一个记录. 首先来看看foreach能够使用 ...