当前位置: 首页 > 编程日记 > 正文

Swift基础 - - 高德地图实践

高德地图开发需要自己到官网http://lbs.amap.com/console/ 注册一个ak,新建一个swift工程,然后在Info.plist中添加一个NSLocationAlwaysUsageDescription或者NSLocationWhenInUseUsageDescription。

高德地图的库以及依赖库加入到项目里面

需要的库如下截图:


添加头文件

具体的方式见Swift基础--调用第三方OC项目,在Bridging-Header.h中加入如下代码,这样我们就可以调用高德地图相关的接口

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #import <MAMapKit/MAMapKit.h>  
  2. #import <AMapSearchKit/AMapSearchAPI.h>  

基础地图类实现

基础类里面编写地图相关的初始化以及功能的开发,界面如下:包含定位,数据刷新,放大缩小添加以及功能实现。


[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  BaseMapController.swift  
  3. //  SwiftMap  
  4. //  地图的基础部分  
  5. //  Created by System Administrator on 15/1/24.  
  6. //  Copyright (c) 2015年 jwzhangjie. All rights reserved.  
  7. //  
  8.   
  9. import UIKit  
  10.   
  11. class BaseMapController : UIViewController, MAMapViewDelegate, AMapSearchDelegate {  
  12.       
  13.     var mapView:MAMapView!  
  14.     var search:AMapSearchAPI!  
  15.     var centerCoordinate:CLLocationCoordinate2D!  
  16.       
  17.     var centerMarker:UIImageView!  
  18.       
  19.     func initMapView(){  
  20.         mapView = MAMapView(frame: CGRectMake(065, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-65))  
  21.         mapView.showsUserLocation = true  
  22.         mapView.setUserTrackingMode(MAUserTrackingModeFollow, animatedtrue)  
  23.         mapView.showsCompass = false  
  24.         mapView.showsScale = true  
  25.         mapView.scaleOrigin = CGPointMake(100, mapView.frame.size.height-20)  
  26.         mapView.delegate = self  
  27.         self.view.addSubview(mapView)  
  28.     }  
  29.       
  30.     func initSearchView(){  
  31.         search = AMapSearchAPI(searchKey: MAMapServices.sharedServices().apiKey, delegateself)  
  32.     }  
  33.       
  34.     func initBtns(){  
  35.         centerMarker = UIImageView(frame: CGRectMake(003850))  
  36.         centerMarker.center = mapView.center  
  37.         centerMarker.frame=CGRectMake(centerMarker.frame.origin.x, centerMarker.frame.origin.y-653850);  
  38.         centerMarker.image = UIImage(named: "green_pin.png")  
  39.         mapView.addSubview(centerMarker)  
  40.         //定位按钮  
  41.         var locationBtn:UIButton = UIButton(frame: CGRectMake(15, mapView.frame.size.height-703535))  
  42.         locationBtn.setBackgroundImage(UIImage(named: "ic_locate.png"), forState: UIControlState.Normal)  
  43.         locationBtn.setBackgroundImage(UIImage(named: "ic_locate_press.png"), forState: UIControlState.Selected)  
  44.         locationBtn.setBackgroundImage(UIImage(named: "ic_locate_press.png"), forState: UIControlState.Highlighted)  
  45.         locationBtn.tag = 1;  
  46.         locationBtn.addTarget(self, action:"btnSelector:", forControlEvents: UIControlEvents.TouchUpInside)  
  47.         mapView.addSubview(locationBtn)  
  48.         //刷新按钮  
  49.         var refreshBtn:UIButton = UIButton(frame: CGRectMake(15, mapView.frame.size.height-1103535))  
  50.         refreshBtn.setBackgroundImage(UIImage(named: "ic_refresh.png"), forState: UIControlState.Normal)  
  51.         refreshBtn.setBackgroundImage(UIImage(named: "ic_refresh_press.png"), forState: UIControlState.Selected)  
  52.         refreshBtn.setBackgroundImage(UIImage(named: "ic_refresh_press.png"), forState: UIControlState.Highlighted)  
  53.         refreshBtn.tag = 2;  
  54.         refreshBtn.addTarget(self, action"btnSelector:", forControlEvents: UIControlEvents.TouchUpInside)  
  55.         mapView.addSubview(refreshBtn)  
  56.         //缩小按钮  
  57.         var zoomOutBtn:UIButton = UIButton(frame: CGRectMake(mapView.frame.size.width - 15-35, mapView.frame.size.height-703535))  
  58.         zoomOutBtn.setBackgroundImage(UIImage(named: "ic_zoom_out.png"), forState: UIControlState.Normal)  
  59.         zoomOutBtn.setBackgroundImage(UIImage(named: "ic_zoom_out_press.png"), forState: UIControlState.Selected)  
  60.         zoomOutBtn.setBackgroundImage(UIImage(named: "ic_zoom_out_press.png"), forState: UIControlState.Highlighted)  
  61.         zoomOutBtn.tag = 3;  
  62.         zoomOutBtn.addTarget(self, action"btnSelector:", forControlEvents: UIControlEvents.TouchUpInside)  
  63.         mapView.addSubview(zoomOutBtn)  
  64.         //放大按钮  
  65.         var zoomInBtn:UIButton = UIButton(frame: CGRectMake(mapView.frame.size.width-15-35, mapView.frame.size.height-1103535))  
  66.         zoomInBtn.setBackgroundImage(UIImage(named: "ic_zoom_in.png"), forState: UIControlState.Normal)  
  67.         zoomInBtn.setBackgroundImage(UIImage(named: "ic_zoom_in_press.png"), forState: UIControlState.Selected)  
  68.         zoomInBtn.setBackgroundImage(UIImage(named: "ic_zoom_in_press.png"), forState: UIControlState.Highlighted)  
  69.         zoomInBtn.tag = 4;  
  70.         zoomInBtn.addTarget(self, action"btnSelector:", forControlEvents: UIControlEvents.TouchUpInside)  
  71.         mapView.addSubview(zoomInBtn)  
  72.     }  
  73.       
  74.       
  75.     func btnSelector(sender: UIButton) {  
  76.         switch sender.tag {  
  77.         case 1://定位  
  78.             if centerCoordinate != nil {  
  79.                 mapView.setCenterCoordinate(centerCoordinate, animatedtrue)  
  80.             }  
  81.         case 2://刷新  
  82.             getLocationRoundFlag()  
  83.             mapView.showsUserLocation = true//YES 为打开定位,NO 为关闭定位  
  84.         case 3:  
  85.             if mapView.zoomLevel >= 4 && mapView.zoomLevel <= 19{  
  86.                 mapView.setZoomLevel(mapView.zoomLevel-1, animatedtrue)  
  87.             }else if mapView.zoomLevel >= 3 && mapView.zoomLevel < 4{  
  88.                 mapView.setZoomLevel(3, animatedtrue)  
  89.             }  
  90.         case 4:  
  91.             if mapView.zoomLevel >= 3 && mapView.zoomLevel <= 18{  
  92.                 mapView.setZoomLevel(mapView.zoomLevel+1, animatedtrue)  
  93.             }else if mapView.zoomLevel > 18 && mapView.zoomLevel <= 19{  
  94.                 mapView.setZoomLevel(19, animatedtrue)  
  95.             }  
  96.         default:  
  97.             println("not known ")  
  98.         }  
  99.     }  
  100.       
  101.     func getLocationRoundFlag(){  
  102.     }  
  103.       
  104.     func mapView(mapView: MAMapView!, didUpdateUserLocation userLocation: MAUserLocation!, updatingLocation: Bool) {  
  105.         if updatingLocation {  
  106.             //取出当前位置的坐标  
  107.             println("latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);  
  108.             centerCoordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude,userLocation.coordinate.longitude);  
  109.             mapView.showsUserLocation = false;  
  110.         }  
  111.     }  
  112.       
  113.     //清除数据  
  114.     func clearMapData(){  
  115.         clearMapView()  
  116.         clearSearch()  
  117.     }  
  118.       
  119.     func clearMapView(){  
  120.         mapView.showsUserLocation = false  
  121.         mapView.delegate = nil  
  122.     }  
  123.       
  124.     func clearSearch(){  
  125.         self.search.delegate = nil  
  126.     }  
  127.       
  128.     override func didReceiveMemoryWarning() {  
  129.         super.didReceiveMemoryWarning()  
  130.         // Dispose of any resources that can be recreated.  
  131.     }  
  132. }  

具体实现类

DetailViewController继承BaseMapController

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  DetailViewController.swift  
  3. //  SwiftMap  
  4. //  
  5. //  Created by System Administrator on 15/1/22.  
  6. //  Copyright (c) 2015年 jwzhangjie. All rights reserved.  
  7. //  
  8.   
  9. import UIKit  
  10.   
  11. class DetailViewController : BaseMapController {  
  12.       
  13.     var data:NSMutableData!  
  14.       
  15.   
  16.     @IBAction func returnHome(sender: AnyObject) {  
  17.         mapView.removeFromSuperview()  
  18.         self.navigationController?.popViewControllerAnimated(true)  
  19.     }  
  20.       
  21.     @IBAction func segmentChanged(sender: AnyObject) {  
  22.         switch sender.selectedSegmentIndex{  
  23.         case 0://已认证车辆  
  24.             println("0")  
  25.         case 1://全部车辆  
  26.             println("1")  
  27.         default:  
  28.             println("default")  
  29.         }  
  30.     }  
  31.       
  32.     override func viewDidLoad() {  
  33.         super.viewDidLoad()  
  34.         initMapView()  
  35.         initSearchView()  
  36.         initBtns()  
  37.         data = NSMutableData();  
  38.     }  
  39.       
  40.       
  41.     override func getLocationRoundFlag(){  
  42.         var requestUrl:String = "http://api.map.baidu.com/geosearch/v3/nearby?ak=dcZObrBgdDD2s4qLCeC4YVOf&geotable_id=92326&location=121.613461,31.197495&radius=1000000&sortby=distance:1";  
  43. //        var request:NSURLRequest = NSURLRequest(URL:NSURL(string: requestUrl))  
  44. //        var connect:NSURLConnection = NSURLConnection(request: request, delegate: self)!  
  45. //        data = NSMutableData()  
  46.         println(requestUrl)  
  47.           
  48.     }  
  49.   
  50.     override func didReceiveMemoryWarning() {  
  51.         super.didReceiveMemoryWarning()  
  52.         // Dispose of any resources that can be recreated.  
  53.     }  
  54.   
  55.   
  56. }  
记得要在真机上测试

相关文章:

Shell脚本示例代码

1. echo_printf_usage.sh: echo和printf的用法 #! /bin/bash# echo和printf的用法# echo是用于终端打印的基本命令.在默认情况下,echo在每次调用后会添加一个换行符 echo "hello, beijing" echo "$(pwd)" echo $(pwd) # 结果并不是希望得到的,将会输出: $…

「2019嵌入式智能国际大会」 399元超值学生票来啦,帮你豪省2600元!

2019 嵌入式智能国际大会即将来袭&#xff01;购票官网&#xff1a;https://dwz.cn/z1jHouwE物联网是继计算机、互联网和移动通信之后的又一次信息产业的革命性发展&#xff0c;在互联网和移动互联网高速发展的时代&#xff0c;几乎所有行业都有数据联网的需求。无论是国外的科…

ThinkPHP学习笔记之Model操作

2019独角兽企业重金招聘Python工程师标准>>> 1. 3种实例化model模型方法 a) new 命名空间GoodsModel(); b) D([“模型标志Goods”]) (之前版本会实例化自定义model对象&#xff0c;目前都实例化Model基类对象) i. 没有参数实例化一个Model对象、有参数也实例化一…

Ubuntu 14.04 64位上配置JDK操作步骤

1. 从 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 下载jdk-8u172-linux-x64.tar.gz; 2. 解压缩&#xff1a; tar -xvzf jdk-8u172-linux-x64.tar.gz 3. 创建/usr/jdk目录&#xff1a; sudo mkdir -p /usr/jdk 4. 将解压缩后的jd…

Swift语言实现代理传值

需求&#xff1a;利用代理实现反响传值&#xff08;以下例子采用点击第二个视图控制器中的按钮来改变第一个视图控制器中的Label的内容&#xff09; 一、创建RootViewController import Foundation import UIKitclass RootViewController:UIViewController,ChangeWordDelegate{…

亚马逊马超:如何使用DGL进行大规模图神经网络训练?

演讲嘉宾 | 马超&#xff08;亚马逊应用科学家&#xff09;整理 | 刘静 出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;与传统基于张量&#xff08;Tensor&#xff09;的神经网络相比&#xff0c;图神经网络将图 (Graph) 作为输入&#xff0c;从图结构中学习潜在的知…

Python学习系列(六)(模块)

Python学习系列&#xff08;六&#xff09;(模块) Python学习系列&#xff08;五&#xff09;(文件操作及其字典) 一&#xff0c;模块的基本介绍 1&#xff0c;import引入其他标准模块 标准库&#xff1a;Python标准安装包里的模块。 引入模块的几种方式&#xff1a; i&#xf…

Ubuntu14.04 64位上配置终端显示git分支名称

之前在Ubuntu14.04上在终端上显示git分支名称基本上都使用oh-my-zsh&#xff0c;可以参考 https://blog.csdn.net/fengbingchun/article/details/77803322 &#xff0c;由于限制了权限&#xff0c;使得不能安装oh-my-zsh&#xff0c;显示错误如下&#xff1a;在 https://githu…

创建UITextField对象

//创建UITextField对象 UITextField * tf[[UITextField alloc]init];//设置UITextField的文字颜色tf.textColor[UIColor redColor];//设置UITextField的文本框背景颜色tf.backgroundColor[UIColor grayColor];//设置UITextField的边框的风格tf.borderStyleUITextBorderStyleRou…

non-local神经网络:通过非局部操作解决深度神经网络核心问题

译者 | 李杰出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;回想一下我们熟悉的CNN、RNN。如下图所示&#xff0c;这些神经网络模型都是基于局部区域进行操作&#xff0c;属于local operations。为了获得长距离依赖&#xff0c;也就是图像中非相邻像素点之间的关系&a…

fgets()用法笔记

为了避免缓冲区溢出&#xff0c;从终端读取输入时应当用fgets()代替gets()函数。但是这也将带来一个问题&#xff0c;因为fgets()的调用格式是&#xff1a; fgets (buf, MAX, fp)fgets (buf, MAX, stdin) buf是一个char数组的名称&#xff0c;MAX是字符串的最大长度&#xff0c…

iOS 上常用的两个功能:点击屏幕和return退出隐藏键盘和解决虚拟键盘

原文地址&#xff1a;http://blog.csdn.net/xiaotanyu13/article/details/7711954 iOS上面对键盘的处理很不人性化&#xff0c;所以这些功能都需要自己来实现&#xff0c; 首先是点击return和屏幕隐藏键盘 这个首先引用双子座的博客 http://my.oschina.net/plumsoft/blog/42545…

深度学习可解释性问题如何解决?图灵奖得主Bengio有一个解

作者 | Yoshua Bengio, Tristan Deleu等译者 | 刘畅&#xff0c;编辑 | Just出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;自 2012 年以来&#xff0c;深度学习的发展有目共睹&#xff0c;今年 3 月&#xff0c;为此做出巨大贡献的 Yoshua Bengio、Yann Lecun和Geo…

zepto打造一款移动端划屏插件

效果图 样式1 样式2 调用 正常情况下应该是后台生成的html代码&#xff0c;但还是写了一套操作tab页的方法 调用简便如下&#xff1a; <link rel"stylesheet" href"kslider.css" type"text/css"/> <script type"text/javascript&q…

swift使用xib绘制UIView

目标&#xff1a;用xib绘制一个UIView&#xff0c;在某个ViewController中调用。 三个文件&#xff1a;ViewController.swift DemoView.swift DemoView.xib 首先&#xff0c;可以专心将DemoView.xib画出来&#xff0c;别忘记DemoView.xib中UIView的一处设置 然后&#x…

吴恩达老师深度学习视频课笔记:深度卷积网络

Why look at case studies?&#xff1a;过去几年&#xff0c;计算机视觉研究中的大量研究都集中在如何把卷积层、池化层以及全连接层这些基本构件组合起来形成有效的卷积神经网络。找感觉最好的方法之一就是去看一些案例&#xff0c;就像很多人通过看别人的代码来学习编程一样…

测试工程师的好日子来啦?Testin发布AI测试产品,提升易用性和自动化效率

2019年10月26日&#xff0c;以"AI未来"为主题的第二届NCTS中国云测试行业峰会在北京国际会议中心正式开幕。在本次大会上&#xff0c;Testin 总裁徐琨正式发布测试业务Testin云测的全新AI产品iTestin。作为 Testin 人工智能战略中的重要一环&#xff0c;iTestin 融合…

Discuz DB层跨库映射关系表名前缀BUG修复后产生的新bug

新的逻辑引入了新的bug&#xff0c;会导致在跨多库连接时&#xff0c;产生表名前缀映射混乱&#xff0c;需要再做逻辑上的修复。 function table_name($tablename) {if(!empty($this->map) && !empty($this->map[$tablename])) {$id $this->map[$tablename];…

swift语言的Block

[cpp] view plaincopy// // blockDemo.swift // swiftDemo // // Created by apple on 14-6-29. // Copyright (c) 2014年 fengsh. All rights reserved. // import Foundation //无参无返回值 typealias funcBlock () -> () //或者 () -> …

吴恩达老师深度学习视频课笔记:目标检测

目标定位&#xff1a;如下图,图像分类任务就是算法遍历图像&#xff0c;判断其中的对象是不是car。图像定位分类(classification with localization)不仅要用算法判断图像中的是不是car&#xff0c;还要在图像中标记出car的位置。图像分类的思路可以帮助学习分类定位&#xff0…

国际顶级学界业界大咖云集,9 场技术论坛布道,2019 嵌入式智能国际大会强势来袭!...

2019 嵌入式智能国际大会即将来袭&#xff01;购票官网&#xff1a;https://dwz.cn/z1jHouwE2019年12月6日-8日&#xff0c;2019嵌入式智能国际大会将在深圳华侨城洲际大酒店举行。本次大会由哈尔滨工业大学&#xff08;深圳&#xff09;、清华大学国际研究生院、CSDN、嵌入式视…

Linux简介总结

1. Linux 就是Unix, 但是Unix 并不一定是Linux.2. 三种软件模式&#xff1a;商业软件&#xff0c;共享软件&#xff0c;自由软件。3. 为什么使用Linux? 它是一个自由软件。第一&#xff1a;可免费提供给任何用户使用。第二&#xff1a;它的源代码公开和可自…

Swift中编写单例的正确方式

本文由CocoaChina译者leon(社区ID)翻译自krakendev 原文&#xff1a;THE RIGHT WAY TO WRITE A SINGLETON 转载请保持所有内容和链接的完整性。 在之前的帖子里聊过状态管理有多痛苦&#xff0c;有时这是不可避免的。一个状态管理的例子大家都很熟悉&#xff0c;那就是单例。使…

C语言中的弱符号与强符号介绍

弱符号(Weak symbol)是链接器(ld)在生成ELF(Executable and Linkable Format,缩写为ELF&#xff0c;可执行和可链接格式&#xff0c;是一种用于可执行文件、目标文件、共享库和核心转储的标准文件格式。ELF文件有两种索引&#xff1a;程序标头中记载了运行时所需的段&#xff0…

Simple Transformer:用BERT、RoBERTa、XLNet、XLM和DistilBERT进行多类文本分类

作者 | Thilina Rajapakse译者 | Raku编辑 | 夕颜出品 | AI科技大本营&#xff08;ID: rgznai100&#xff09;【导读】本文将介绍一个简单易操作的Transformers库——Simple Transformers库。它是AI创业公司Hugging Face在Transformers库的基础上构建的。Hugging Face Transfor…

StarUML中时序图添加小人

转载于 http://blog.csdn.net/longyuhome/article/details/9011629 在看时序图的例子的时候&#xff0c;发现有些的时序图上有小人的图标&#xff0c;可是一些UML工具却没有找到小人的图标&#xff0c;这让我很闹心&#xff0c;一直没解决&#xff0c;今天终于将该问题给解…

Swift学习: 从Objective-C到Swift

作者&#xff1a;方秋枋&#xff08;GitHub&#xff09; 这篇文章是自己学习Swift的笔记与深化。希望这篇文章能够帮助已经有Objective-C经验的开发者更快地学习Swift。同时也品味到Swift的精妙之处。 结论放在开头:我认为Swift比Objective-C更优雅,更安全同时也更现代,更性感…

C/C++中static关键字用法汇总

1. 函数内static局部变量&#xff1a;变量在程序初始化时被分配&#xff0c;直到程序退出前才被释放&#xff0c;也就是static是按照程序的生命周期来分配释放变量的&#xff0c;而不是变量自己的生命周期。多次调用&#xff0c;仅需一次初始化。2. cpp内的static全局变量&…

CornerNet: 成对关键点物体检测 | CSDN博文精选

作者 | 贾小树来源 | CSDN博客文章目录1、论文总述2、使用锚定框的两个缺点3、角点检测比边界框中心或 proposals效果好的两个原因4、corner pooling5、用于Grouping Corners的 embedding vector的工作原理6、正负样本的分配方式&#xff08;改进的focal loss&#xff09;7、角…

PHP创建日志记录(已封装)

1 <?php2 3 class Logs{4 private $_filepath; //文件路径5 private $_filename; //文件名6 private $_filehandle; //文件引擎7 8 9 public function Logs($dir null,$filename null){ 10 11 $this->_filepath empty($dir) ? : $d…