第十章 使用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 > {
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的更多相关文章
- swift项目实战FoodPin目录
好吧,据说写博客能够找到好工作,那我也来分享一个项目吧! 自己自学iOS开发也有半年多了,现在就来分享一个swift的小项目吧!这个项目的来源是<Beginning iOS8 programmi ...
- MapKit/CoreLocation框架 总结
MapKit/CoreLocation框架 /*英译 core:核心 track:踪迹 current:当前 statellite:卫星 hybird:混合 region:范围 annotation ...
- 读《编写可维护的JavaScript》第九、十章总结
第九章 将配置数据从代码中分离出来 9.2 抽离配置数据 这章比较好理解,也非常常见,作者给的俩个例子就能说明一切: // 将配置数据藏在代码中 function validate(value) { ...
- 0526 Sprint1个人总结 & 《构建之法》第八、九、十章
Sprint1的个人总结: 我是老人组的成员,我们是做一款四则运算训练的软件.然后我是接了界面设计的任务,所以我任务将会是sprint1中相对重一点的一方.我的感觉是,界面要做得充满童趣,毕竟我们的软 ...
- Getting Started With Hazelcast 读书笔记(第八章-第十章)
第八章到第十章就是一些介绍性的描述,吹的就是Hazelcast能使用在各种地方.. 第八章 -从外面看 1.Hazelcast做了一个memcache的java实现,方便py和php使用. 2.可 ...
- [iOS 利用MapKit和CoreLocation框架打造精简的定位和导航]
运行效果: 一.利用<CoreLocation/CoreLocation.h>定位 创建变量 CLLocationManager *locationManager , ...
- iOS开发——高级篇——地图 MapKit
一.简介 1.在移动互联网时代,移动app能解决用户的很多生活琐事,比如周边:找餐馆.找KTV.找电影院等等导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达 在上述应用中,都用到了定位 ...
- Leonbao:MapKit学习笔记
以下仅作了解, 实际使用以百度地图居多, 因为百度地图有动态路径规划等接口 MapKit学习笔记 原帖: http://www.cocoachina.com/bbs/read.php?tid-6 ...
- MapKit 添加大头针
#import "ViewController.h" #import <MapKit/MapKit.h> #import "MYAnnotation.h&qu ...
随机推荐
- 烂泥:【解决】Ubuntu下使用SSH连接centos系统很慢
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 这几天在Ubuntu下使用SSH连接centos系统,发现连接很慢.建议一个连接大约需要30s.很是坑爹,如下: 后来查询相关资料,发现这个是Ubunt ...
- Redis系列(二)—— 数据类型及其使用
Redis数据类型及其使用 参考:http://www.cnblogs.com/jackluo/p/3173436.html Redis支持五种数据类型:string(字符串),hash(哈希),li ...
- linux 程序管理
在linux下做开发,经常要用到一些管理程序的命令,如查看进程,查看内存等情况.看网络情况.如下的笔记是看书时记下一些简单常用的命令. 1)top [root@005 fsh]#top[-d数字]|t ...
- js制作倒计时效果
该程序可以计算任意指定的两个日期中间的差值. 本例子的指定日期是2017年1月1日,当前时间是2016年10月21日:计算它们之间的差值,在网页上显示如图所示: 关键部分代码: var nowtime ...
- 【温故而知新-Javascript】使用数组
Javascript 数组的工作方式与大多数编程语言的数组类似. <!DOCTYPE html> <html lang="en"> <head> ...
- 【读书笔记《Android游戏编程之从零开始》】5.Android 游戏开发常用的系统控件(ProgressBar、Seekbar)
3.7 ProgressBar ProgressBar类官方文档地址:http://developer.android.com/reference/android/widget/ProgressBar ...
- 边工作边刷题:70天一遍leetcode: day 78
Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...
- Android Studio运行程序出现Session ‘app’: Error Launching activity 解决办法
session "app":error launching activity 一下两种方法,可以轻松解决: 1. 2.把复选框去除:
- 总结Gerrit常用命令
查看分支列表,带有*的分支表示是当前所在分支[root@115~~]#git branch 查看分支详细情况 (推荐这种方式)[root@115~~]#git branch -av 创建分支(比如de ...
- UIVisualEffectView为视图添加特殊效果
在iOS 8后,苹果开放了不少创建特效的接口,其中就包括创建毛玻璃(blur)的接口. 通常要想创建一个特殊效果(如blur效果),可以创建一个UIVisualEffectView视图对象,这个对象提 ...