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 = 84 次浏览;
   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

相关推荐

  • resize

    文章目录resize补充说明语法选项实例 resize 命令设置终端机视窗的大小 补充说明 resize命令 命令设置终端机视窗的大小。执行resize指令可设置虚拟终端机的视窗大…

    入门教程 2024年 3月 4日
  • iOS环境搭建

    文章目录iOS Xcode 安装界面生成器(Interface Builder)iOS模拟器 iOS Xcode 安装 1、从 https://developer.apple.co…

    2023年 3月 18日
  • mysqlimport

    文章目录mysqlimport补充说明语法选项参数 mysqlimport 为MySQL服务器用命令行方式导入数据 补充说明 mysqlimport命令 为mysql数据库服务器提…

    入门教程 2024年 1月 3日
  • losetup

    文章目录losetup补充说明语法选项参数loop设备介绍实例 losetup 设定与控制循环(loop)设备 补充说明 losetup命令 用来设置循环设备。循环设备可把文件虚拟…

    入门教程 2023年 12月 19日
  • Warning: The resulting partition is not properly aligned for best performance

    在使用parted创建分区时产生告警信息, “Warning: The resulting partition is not properly aligned for …

    入门教程 2023年 7月 11日
  • touch

    文章目录touch补充说明语法选项参数实例 touch 创建新的空文件 补充说明 touch命令 有两个功能:一是用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的…

    入门教程 2024年 3月 11日
  • K8S集群的开机关机操作

    K8S集群中,如何开机关机

    2021年 7月 26日
  • screen

    文章目录screen补充说明语法选项常用screen参数使用 screenscreen 高级应用  screen 用于命令行终端切换 补充说明 Screen 是一款由GNU计划开发…

    入门教程 2024年 3月 4日
  • accept

    文章目录accept补充说明语法选项参数 accept 指示打印系统接受发往指定目标打印机的打印任务 补充说明 accept命令 属于CUPS套件,用于指示打印系统接受发往指定目标…

    入门教程 2023年 12月 6日
  • pvdisplay

    文章目录pvdisplay补充说明语法选项参数实例 pvdisplay 显示物理卷的属性 补充说明 pvdisplay命令 用于显示物理卷的属性。pvdisplay命令显示的物理卷…

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