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

相关推荐

  • chmod

    文章目录chmod概要主要用途参数选项返回值例子注意 chmod 用来变更文件或目录的权限 概要 chmod [OPTION]… MODE[,MODE]… FILE… …

    入门教程 2023年 12月 7日
  • DataFrame(表数据)

    DataFrame 是一个表格型的数据结构,它含有一组有序的列,每列可以是不同的值类型(数值、字符串、布尔型值)。DataFrame 既有行索引也有列索引,它可以被看做由 Seri…

    2023年 5月 16日
  • vgcreate

    文章目录vgcreate补充说明语法选项参数实例 vgcreate 用于创建LVM卷组 补充说明 vgcreate命令 用于创建LVM卷组。卷组(Volume Group)将多个物…

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

    文章目录chcon补充说明语法选项实例 chcon 修改对象(文件)的安全上下文 补充说明 chcon命令 是修改对象(文件)的安全上下文,比如:用户、角色、类型、安全级别。也就是…

    入门教程 2023年 12月 7日
  • Helm | Helm 展示readme

    文章目录helm show readme简介可选项从父命令继承的命令请参阅 helm show readme 显示chart的README 简介 该命令检查chart(目录、文件或…

    入门教程 2023年 12月 14日
  • HTML 布局

    网页布局对改善网站的外观非常重要。 请慎重设计您的网页布局。 文章目录网站布局HTML 布局 – 使用<div> 元素实例HTML 布局 – 使…

    2023年 4月 13日
  • 上手编写-柿饼UI“环境检测仪”代码

    文章目录前言第一周:熟悉语法,产品框架第二周:设计UI界面第三周:设计界面,代码实现第四周:测试验证 前言 首先,感谢RTT的这次体验机会,此代码参考了Factory_Demo 先…

    入门教程 2021年 3月 31日
  • 前言

    Pandas 是 Python 语言的一个扩展程序库,用于数据分析。 Pandas 是一个开放源码、BSD 许可的库,提供高性能、易于使用的数据结构和数据分析工具。 Pandas …

    2023年 5月 16日
  • grpunconv

    文章目录grpunconv补充说明语法实例 grpunconv 用来关闭群组的投影密码 补充说明 grpunconv命令 用来关闭群组的投影密码。它会把密码从gshadow文件内,…

    入门教程 2023年 12月 14日
  • centos 下安装go环境

    centos下安装go一般 有两种方式,一个是yum,一个是直接到官网下载.tar.gz包yum方式安装比较简单 直接执行 yum install golang 然后就可以使用go…

    2024年 4月 19日
Translate »