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

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

1、添加地图

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

[cpp]  view plain copy
  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中添加

[cpp]  view plain copy
  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、定位到指定经纬度

[cpp]  view plain copy
  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文件添加如下代码:

[cpp]  view plain copy
  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
[cpp]  view plain copy
  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 使用大头针,

新建个方法添加大头针的

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

调用

[cpp]  view plain copy
  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里直接调用

[cpp]  view plain copy
  1. locationManager = [[CLLocationManager alloc] init];
  2. locationManager.delegate = self;
  3. [locationManager startUpdatingLocation];

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

[cpp]  view plain copy
  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

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

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

[cpp]  view plain copy
  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.)"

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

[cpp]  view plain copy
  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

著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!
更多 7
  • 上一篇:iOS学习之第二个View使用UITabBarViewController
  • 下一篇:iOS学习之sqlite的创建数据库,表,插入查看数据

iOS学习之Map,定位,标记位置的使用相关推荐

  1. OpenCV for Ios 学习笔记(4)-标记检测1

    本文原始地址:OpenCV for Ios 学习笔记(4)-标记检测1 简单的标记经常是以白色块和黑色块构成的规则图形.因为我们预先知道这些因素,所以我们可以很容易检测标记. 如图: 首先,我们需要找 ...

  2. [IOS]整合google map并获取当前位置

    1.下载Google map的sdk,详情参考: Get Started 2.准备好SDK后,展示map这些问题都不大,稍微难点的是定位当前位置 可以参考google的git tutorial: ht ...

  3. vue-amap实现实现初始化并定位当前位置,搜索,定位,增加点标记

    实现如图展示,初始化并定位当前位置,搜索,定位,增加点标记 下载vue 1.vue页设置 如果是新版web-js的密钥,有配套的安全秘钥,则需要另外在created加入,否则会报undefined w ...

  4. 基于深度学习 利用目标检测的方法定位瑕疵位置

    利用目标检测的方法定位瑕疵位置 通过语义分割的方法分割瑕疵(使用传统方法,如二值化等方法分割亦可) 图像分类的方法判断类别 问题:检测手机屏幕表面的瑕疵,在图中画出瑕疵位置并标明瑕疵种类.Screen ...

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

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

  6. ios学习--iphone开发笔记和技巧总结(原址持续更新)

    ios学习--iphone开发笔记和技巧总结(原址持续更新) 分类: ios Object-C2012-04-18 10:16 2716人阅读 评论(1) 收藏 举报 uiviewiphonelist ...

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

    Windows phone 8 学习笔记(8) 定位地图导航 原文:Windows phone 8 学习笔记(8) 定位地图导航 Windows phone 8 已经不使用自家的bing地图,新地图控 ...

  8. [android] 百度地图开发 (二).定位城市位置和城市POI搜索

    一. 百度地图城市定位和POI搜索知识       上一篇文章"百度地图开发(一)"中讲述了如何申请百度APIKey及解决显示空白网格的问题.该篇文章主要讲述如何定位城市位置.定位 ...

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

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

最新文章

  1. SharePoint 2010、2013多个域之间互信(Domain Trust)的设计与实施
  2. JavaScript与 HTML表单的交互过程,想要学习动态网页但是无从下手的新手看看。...
  3. x61 linux 驱动 无线网卡,Linux环境Thinkpad X61 4G内存Mtrr表错误
  4. 基于 Elasticsearch 存储的HBase二级索引方案
  5. Ubuntu: 使用U盘拷贝文件
  6. 微信H5开发,怎样禁止手机横屏
  7. Vue.js:事件修饰符stop,once,prevent的使用
  8. 【转】图像视觉开源代码
  9. Android HTTP网络详解
  10. Windows 上安装 Bugzilla 详解
  11. 如何用uni-app做一个领优惠券H5、小程序商城(一)
  12. 1st Competition of Datawhale: the car price prediction
  13. Redis简介(01)历史与发展
  14. 【8. Redis 的设计、实现】
  15. 710. Random Pick with Blacklist 黑名单中的随机数(Hard)
  16. 不要让你的习以为常,用余生去懊悔!
  17. 建筑学计算机交叉学科BIM,BIM有区分专业吗
  18. 2023全网最新微信支付宝QQ三合一支付生成PHP源码
  19. “首届温州鞋服产业AIGC设计大赛”已经开始!参赛指南看这里!
  20. EDA开源仿真工具verilator入门8:verilator 5.0 最新版本仿真玄铁性能对比

热门文章

  1. 【排序算法】常见排序算法总结
  2. 使用APP支付返回ALIN10070
  3. 用Python写一个打字软件代码
  4. 学了一天java,我总结了这些知识点
  5. 纯css画菱形,三角形,梯形,平行四边形
  6. Netfilter数据包流向图
  7. mac云显卡服务器_云显卡玩吃鸡 NV GeForce NOW登陆Mac
  8. 2c网站php源码程序 家居饰品网上商城源码_Typecho小程序搭建不成功各类问题总结...
  9. Spring全家桶面试题
  10. 全面拥抱 K8s,ApacheDolphinScheduler 应用与支持 K8s 任务的探索