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以上的模拟器才能用这个功能,打开模拟:

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

iOS开发中地图开发的简单应用的更多相关文章

  1. spring注解开发中常用注解以及简单配置

    一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...

  2. cocos2dx之lua项目开发中MVC框架的简单应用

    **************************************************************************** 时间:2015-03-31 作者:Sharin ...

  3. iOS开发中地图与定位

    不管是QQ还是微信的移动client都少不了定位功能,之前在微信demo中没有加入定位功能,今天就写个定位的小demo来了解一下定位和地图的东西. 地图和定位看上去是挺高大上一东西.其有使用方法比Ta ...

  4. iOS开发系列--网络开发

    概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博.微信等,这些应用本身可能采用iOS开发,但是所有的数据支撑都是基于后台网络服务器的.如今,网络编程越来越普遍,孤立的应用通常是没有生命力 ...

  5. GIS开发离线地图应用-初识gis

    http://www.cnblogs.com/kevin-zlg/p/4611671.html 最新公司需要做一个基于gis地图的应用系统,由于之前公司项目中的电子地图模块都是我开发的,所以这个新系统 ...

  6. android定位和地图开发实例

    在android开发中地图和定位是很多软件不可或缺的内容,这些特色功能也给人们带来了很多方便. 首先介绍一下地图包中的主要类: MapController : 主要控制地图移动,伸缩,以某个GPS坐标 ...

  7. android开发中的5种存储数据方式

    数据存储在开发中是使用最频繁的,根据不同的情况选择不同的存储数据方式对于提高开发效率很有帮助.下面笔者在主要介绍Android平台中实现数据存储的5种方式. 1.使用SharedPreferences ...

  8. Android 开发中 SQLite 数据库的使用

    SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PHP, ...

  9. 在Android 开发中使用 SQLite 数据库笔记

    SQLite 介绍   SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PH ...

随机推荐

  1. 远程连接MySQL 不允许

    报错:1130-host ... is not allowed to connect to this MySql server 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在localhost ...

  2. How to configure connectingstrings in Web.config

    先来看一下默认的连接SQL Server数据库配置<connectionStrings>   <add name="LocalSqlServer" connect ...

  3. 第一个VC++ win32程序 绘制简单图形

    创建一个VC++ win32 打开VS 新工程类型中选择Win32----Win32 Project  自己取个名字(假如叫做My1stWin) 一路next 系统会自动生成好最基本的代码  然后我们 ...

  4. 从基因组可视化工具——circos说起,circos安装

      这是博客改版的第一篇博文,选择最近使用的生物信息学软件——circos开始写起.circos是用perl写的生物软件,从发表的文章来看 学习circos主要是熟悉配置文件的编辑方法,搞清楚其中的标 ...

  5. C#计算时间差。

    C#中怎么计算两时间相差多少.计算2个时间之间的差,可以计算到时分秒! <1>label1.Text = “2004-1-1 15:36:05″;label2.Text = “2004-3 ...

  6. rsync、ssh备份

    Linux系统需求 为了实现这个备份程序,需要准备一个外部驱动器.您可以将备份写到一个外部USB磁盘,但这样做效率不高.因此,我假设您将备份写到位于网络某处的服务器中.这台服务器要为带有SSH和rsy ...

  7. Android Studio 新建项目的R文件丢失的解决方法

    最近Android Studio炒的比较热,于是笔者决定赶赶时髦,从Eclipse转到了Android Studio.不幸的是,用Android Studio创建项目的时候就遇到了一个比较尖锐的问题— ...

  8. HDU 1108 最小公倍数

    #include <cstdio> int gcd(int a,int b) { ) return a; else return gcd(b,a%b); } int main() { in ...

  9. Android 实现简单天气应用

    引导页面,多个城市的天气,可以通过滑动来翻阅. 先看下截图: 1.城市天气界面 2.引导界面 应用引导页面 package org.qxj.iweather.page; import org.qxj. ...

  10. CERC 2013 Magical GCD

    题目大意如下:给定一个序列,每个序列有值xi,现给定t个数列,对于每个长n的数列,求一段[l,r]使 [r-l+1]*gcd(l,r)最大,gcd(l,r)指的是该连续区间的最大公约数. 不难想到n^ ...