iOS地图开发

IOS地图开发


简介

IOS地图帮助我们定位位置,IOS地图使用 MapKit 框架。

实例步骤

1.创建一个简单的 View based application

2.选择项目文件,然后选择目标,然后添加MapKit.framework.

3.添加 Corelocation.framework

4.向 ViewController.xib 添加地图查看和创建 ibOutlet 并且命名为mapView。

5.通过File-> New -> File… -> 选择 Objective C class创建一个新的文件,单击下一步

6.sub class of为 NSObject,类作命名为MapAnnotation

7.选择创建

8.更新MapAnnotation.h ,如下所示

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapAnnotation : NSObject<MKAnnotation>
@property (nonatomic, strong) NSString *title;
@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;

- (id)initWithTitle:(NSString *)title andCoordinate:
  (CLLocationCoordinate2D)coordinate2d;

@end

9.更新MapAnnotation.m ,如下所示

#import "MapAnnotation.h"

@implementation MapAnnotation
-(id)initWithTitle:(NSString *)title andCoordinate:
 (CLLocationCoordinate2D)coordinate2d{    
    self.title = title;
    self.coordinate =coordinate2d;
    return self;
}
@end

10.更新ViewController.h ,如下所示

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<MKMapViewDelegate>
{
    MKMapView *mapView;
}
@end

11.更新ViewController.m ,如下所示

#import "ViewController.h"
#import "MapAnnotation.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
   [super viewDidLoad];
   mapView = [[MKMapView alloc]initWithFrame:
   CGRectMake(10, 100, 300, 300)];
   mapView.delegate = self;
   mapView.centerCoordinate = CLLocationCoordinate2DMake(37.32, -122.03);
   mapView.mapType = MKMapTypeHybrid;
   CLLocationCoordinate2D location;
   location.latitude = (double) 37.332768;
   location.longitude = (double) -122.030039;
   // Add the annotation to our map view
   MapAnnotation *newAnnotation = [[MapAnnotation alloc]
   initWithTitle:@"Apple Head quaters" andCoordinate:location];
   [mapView addAnnotation:newAnnotation];
   CLLocationCoordinate2D location2;
   location2.latitude = (double) 37.35239;
   location2.longitude = (double) -122.025919;
   MapAnnotation *newAnnotation2 = [[MapAnnotation alloc] 
   initWithTitle:@"Test annotation" andCoordinate:location2];
   [mapView addAnnotation:newAnnotation2];
   [self.view addSubview:mapView];
}
// When a map annotation point is added, zoom to it (1500 range)
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
   MKAnnotationView *annotationView = 131 次浏览;
   id <MKAnnotation> mp = [annotationView annotation];
   MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance
   ([mp coordinate], 1500, 1500);
   [mv setRegion:region animated:YES];
   [mv selectAnnotation:mp animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

输出

当我们向上滚动地图时,输出结果如下

mapOutput2

若文章对你有帮助,可以点赞或打赏支持我们。发布者:lyh会员,转载请注明出处:http://61.174.243.28:13541/AY-knowledg-hub/ios%e5%9c%b0%e5%9b%be%e5%bc%80%e5%8f%91-%e8%8f%9c%e9%b8%9f%e6%95%99%e7%a8%8b/

(0)
lyhlyh会员认证作者
上一篇 2023年 4月 1日 下午8:55
下一篇 2023年 4月 1日 下午9:04

相关推荐

  • Objective C 基础知识

    在iOS的开发中使用的是Objective C语言,它是一种面向对象的语言,因而对于已经掌握面向对象语言知识的编程者来说是非常简单的。 文章目录接口和实现创建对象方法(method…

    入门教程 2023年 4月 10日
  • 循环语句

    文章目录循环语句for 语句语法例子breakcontinue更多例子无限循环 循环语句 上一节:第八篇 if else 语句下一节:第十篇 switch 语句 这是本Golang…

    2023年 12月 5日
  • sesearch

    文章目录sesearch补充说明语法选项实例 sesearch 查询SELinux策略的规则详情 补充说明 使用seinfo命令可以查询SELinux的策略提供多少相关规则,如果查…

    入门教程 2024年 3月 4日
  • HTTP介绍

    文章目录HTTP/HTTPS 简介HTTP 工作原理HTTP 三点注意事项:HTTPS 作用证书有效证书无效 HTTP/HTTPS 简介 HTTP 协议是 Hyper Text T…

    2023年 5月 14日
  • yes

    文章目录yes补充说明语法参数实例 yes 重复打印指定字符串 补充说明 yes命令 在命令行中输出指定的字符串,直到yes进程被杀死。不带任何参数输入yes命令默认的字符串就是y…

    入门教程 2024年 3月 11日
  • echo

    文章目录echo补充说明语法选项参数实例 echo 输出指定的字符串或者变量 补充说明 echo命令 用于在shell中打印shell变量的值,或者直接输出指定的字符串。linux…

    入门教程 2023年 12月 14日
  • Java StringBuffer 和 StringBuilder 类

    当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。 和 String 类不同的是,StringBuffer 和 StringBuil…

    2023年 3月 4日
  • HTML 元素

    HTML 文档由 HTML 元素定义。 文章目录HTML 元素HTML 元素语法嵌套的 HTML 元素HTML 文档实例HTML 实例解析不要忘记结束标签HTML 空元素HTML …

    入门教程 2023年 4月 11日
  • less

    文章目录less补充说明语法选项参数实例 less 分屏上下翻页浏览文件内容 补充说明 less命令 的作用与more十分相似,都可以用来浏览文字档案的内容,不同的是less命令允…

    入门教程 2023年 12月 19日
  • uucico

    文章目录uucico补充说明语法选项实例 uucico UUCP文件传输服务程序 补充说明 uucico命令 命令UUCP文件传输服务程序。 uucico是用来处理uucp或uux…

    入门教程 2024年 3月 11日
Translate »