iOS上使用地图比Android要方便,只需要新建一个MKMapView,addSubView即可。这次要实现的效果如下:

有标注(大头针),定位,地图。

1、添加地图

1.1 新一个Single View app ,选择默认项,创建后,在ViewController.h

  1. #import <UIKit/UIKit.h>
  2. #import <MapKit/MapKit.h>
  3. #import <CoreLocation/CoreLocation.h>
  4. @interface ViewController : UIViewController
  5. <MKMapViewDelegate, CLLocationManagerDelegate> {
  6. MKMapView *map;
  7. CLLocationManager *locationManager;
  8. }
  9. @end

1.2在ViewController.m中添加

  1. - (void)viewDidLoad
  2. {
  3. map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
  4. map.showsUserLocation = YES;
  5. map.mapType = MKMapTypeSatellite;
  6. [self.view addSubview:map];
  7. [super viewDidLoad];
  8. // Do any additional setup after loading the view, typically from a nib.
  9. }

运行:

OMG,看到的是世界地图。怎么定位到指定的位置呢?比如定位回来伟大的祖国首都?

这里map.mapType =MKMapTypeSatellite;我用到是卫星地图,可以使用标准的地图,

map.mapType =MKMapTypeStandard;

注意,如果此时你编译有错误,请拉到博客最后查看 :5、 遇到的问题

2、定位到指定经纬度

  1. CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);
  2. float zoomLevel = 0.02;
  3. MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));
  4. [map setRegion:[map regionThatFits:region] animated:YES];

这样,就我们就定位的了故宫了。

3、添加标注大头针

3.1 新建一个标注类:CustomAnnotation

按Command+N,继承NSObject。在CustomAnnotation.h 和CustomAnnotation.m文件添加如下代码:

  1. #import <Foundation/Foundation.h>
  2. #import <MapKit/MapKit.h>
  3. @interface CustomAnnotation : NSObject
  4. <MKAnnotation>
  5. {
  6. CLLocationCoordinate2D coordinate;
  7. NSString *title;
  8. NSString *subtitle;
  9. }
  10. -(id) initWithCoordinate:(CLLocationCoordinate2D) coords;
  11. @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
  12. @property (nonatomic, retain) NSString *title;
  13. @property (nonatomic, retain) NSString *subtitle;
  14. @end
  1. #import "CustomAnnotation.h"
  2. @implementation CustomAnnotation
  3. @synthesize coordinate, title, subtitle;
  4. -(id) initWithCoordinate:(CLLocationCoordinate2D) coords
  5. {
  6. if (self = [super init]) {
  7. coordinate = coords;
  8. }
  9. return self;
  10. }
  11. @end

3.1 使用大头针,

新建个方法添加大头针的

  1. -(void)createAnnotationWithCoords:(CLLocationCoordinate2D) coords {
  2. CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithCoordinate:
  3. coords];
  4. annotation.title = @"标题";
  5. annotation.subtitle = @"子标题";
  6. [map addAnnotation:annotation];
  7. }

调用

  1. CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);
  2. float zoomLevel = 0.02;
  3. MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));
  4. [map setRegion:[map regionThatFits:region] animated:YES];
  5. [self createAnnotationWithCoords:coords];

这样我们就把大头针定位在故宫了

4、定位到当前位置并获取当前经纬度

前面我们已经添加了locationManager,现在在DidViewLoad里直接调用

  1. locationManager = [[CLLocationManager alloc] init];
  2. locationManager.delegate = self;
  3. [locationManager startUpdatingLocation];

实现协议方法收到定位成功后的经纬度

  1. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
  2. [locationManager stopUpdatingLocation];
  3. NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
  4. NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
  5. NSLog(@"Lat: %@  Lng: %@", strLat, strLng);
  6. }
  7. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
  8. NSLog(@"locError:%@", error);
  9. }

运行,允许获取当前位置,打印log

  1. 2012-06-28 23:58:32.237 MapDemo[8202:11603] Lat: 39.9011  Lng: 116.3000

如果不允许:打印出错误日志

  1. 2012-06-28 23:25:03.109 MapDemo[7531:11603] locError:Error Domain=kCLErrorDomain Code=1 "The operation couldn’t be completed. (kCLErrorDomain error 1.)"

定位后,移动到当前位置:

  1. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
  2. [locationManager stopUpdatingLocation];
  3. NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
  4. NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
  5. NSLog(@"Lat: %@  Lng: %@", strLat, strLng);
  6. CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(newLocation.coordinate.latitude,newLocation.coordinate.longitude);
  7. float zoomLevel = 0.02;
  8. MKCoordinateRegion region = MKCoordinateRegionMake(coords,MKCoordinateSpanMake(zoomLevel, zoomLevel));
  9. [map setRegion:[map regionThatFits:region] animated:YES];
  10. }

定位到了当前位置。

5、会遇到的问题:

运行是发现了编译错误:

Undefined symbols for architecture i386:

"_CLLocationCoordinate2DMake", referenced from:

-[ViewController viewDidLoad] in ViewController.o

-[ViewController locationManager:didUpdateToLocation:fromLocation:] in ViewController.o

"_OBJC_CLASS_$_MKMapView", referenced from:

objc-class-ref in ViewController.o

"_OBJC_CLASS_$_CLLocationManager", referenced from:

objc-class-ref in ViewController.o

ld: symbol(s) not found for architecture i386

clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是为什么呢?没有添加对应的FrameWork。我使用的是4.3.2版本的XCode,添加方法如下:

选择项目,TARGETS ,点加号,添加两个framework

就好了。

如何发送IOS模拟器经纬度?

5.0以上的模拟器才能用这个功能,打开模拟:

这样就能发送模拟的当前位置了。

例子代码:http://download.csdn.net/detail/totogo2010/4400001点击打开链接

https://github.com/schelling/YcDemo/tree/master/MapDemo

iOS学习之Map,定位,标记位置的使用的更多相关文章

  1. 小程序map学习:使用map获取当前位置并显示出来

    在小程序开发的过程中,我碰到过一个做map的需求,在我开发的时候我碰到了一些问题,这里总结出来,给大家一些解决方法. 简易小程序dome下载 代码片段分享: js部分: var amapFile = ...

  2. 【iOS】7.4 定位服务->2.1.2 定位 - 官方框架CoreLocation: CLLocationManager(位置管理器)

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  3. iOS学习笔记-地图MapKit入门

    代码地址如下:http://www.demodashi.com/demo/11682.html 这篇文章还是翻译自raywenderlich,用Objective-C改写了代码.没有逐字翻译,如有错漏 ...

  4. iOS 学习资源

    这份学习资料是为 iOS 初学者所准备的, 旨在帮助 iOS 初学者们快速找到适合自己的学习资料, 节省他们搜索资料的时间, 使他们更好的规划好自己的 iOS 学习路线, 更快的入门, 更准确的定位的 ...

  5. Windows phone 8 学习笔记(8) 定位地图导航

    原文:Windows phone 8 学习笔记(8) 定位地图导航 Windows phone 8 已经不使用自家的bing地图,新地图控件可以指定制图模式.视图等.bing地图的定位误差比较大,在模 ...

  6. 【原】iOS学习48地图

    一.地图的简介 在移动互联网时代,移动app能解决用户的很多生活琐事,比如 导航:去任意陌生的地方 周边:找餐馆.找酒店.找银行.找电影院 手机软件:微信摇一摇.QQ附近的人.微博.支付宝等 在上述应 ...

  7. iOS学习——iOS项目Project 和 Targets配置详解

    最近开始学习完整iOS项目的开发流程和思路,在实际的项目开发过程中,我们通常需要对项目代码和资料进行版本控制和管理,一般比较常用的SVN或者Github进行代码版本控制和项目管理.我们iOS项目的开发 ...

  8. iOS学习——键盘弹出遮挡输入框问题解决方案

    在iOS或Android等移动端开发过程中,经常遇到很多需要我们输入信息的情况,例如登录时要输入账号密码.查询时要输入查询信息.注册或申请时需要填写一些信息等都是通过我们键盘来进行输入的,在iOS开发 ...

  9. iOS学习——UIView的研究

    在iOS开发中,我们知道有一个共同的基类——NSObject,但是对于界面视图而言,UIView是非常重要的一个类,UIView是很多视图控件的基类,因此,对于UIView的学习闲的非常有必要.在iO ...

随机推荐

  1. thinkinginjava学习笔记08_接口

    抽象类和抽象方法 抽象方法是指没有具体实现的方法,仅仅有方法的声明和没有方法体:使用abstract关键字定义一个抽象方法:包含抽象方法的类成为抽象类,如果一个类中包含抽象方法则必须使用abstrac ...

  2. CentOS7下安装MySQL并配置远程连接

    一.CentOS7下安装MySQL数据库 CentOS7默认的安装包里面已经没有 MySQL-Server安装包了,远程镜像中也没有了. 默认的是MariaDB (MySQL的一个分支,开发这个分支的 ...

  3. cobbler自动安装系统(Centos7.X)

    环境: [root@kickstart ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@kickstart ~]# unam ...

  4. 别纠结mybatis啦,赶紧来瞅瞅吧

    自从用了mybatis后,被坑的次数不下于无数次,今天我们就来说说最最头疼的错误,看看有多少人入过这个坑呢. 当程序出现了 Result Maps collection already contain ...

  5. Android查缺补漏(View篇)--事件分发机制

    事件分发机制是Android中非常重要的一个知识点,同时也是难点,相信到目前为止很多Android开发者对事件分发机制并没有一个非常系统的认识,当然也包括博主个人在内.可能在平时的开发工作中我们并没有 ...

  6. 部署 k8s Cluster(下)- 每天5分钟玩转 Docker 容器技术(119)

    上节我们通过 kubeadm 在 k8s-master 上部署了 Kubernetes,本节安装 Pod 网络并添加 k8s-node1 和 k8s-node2,完成集群部署. 安装 Pod 网络 要 ...

  7. Redis的key过期处理策略

    Redis中有三种处理策略:定时删除.惰性删除和定期删除. 定时删除:在设置键的过期时间的时候创建一个定时器,当过期时间到的时候立马执行删除操作.不过这种处理方式是即时的,不管这个时间内有多少过期键, ...

  8. UINavigationController 返回手势与 leftBarButtonItem

    UINavigationController 返回手势与 leftBarButtonItem UINavigationController 自带从屏幕左侧边缘向右滑动的返回手势,可以通过这个手势实现 ...

  9. Hadoop日志以及日志的格式和命名组成

    一.日志的格式 有两种日志,分别以log和out结尾 1)以log结尾的日志: 通过log4j日志记录格式进行记录的日志,采用的日常滚动文件后缀策略来命名日志文件,内容比较全. 2)以out结尾的日志 ...

  10. 基本命令行操作1(java编译)

    1. 设置环境变量,具体:https://www.cnblogs.com/shinge/p/5500002.html "cd + 文件名" 可进入指定文件,"cd..&q ...