iOS 地图(自定义地位图标)
2024-10-21 14:37:19
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = [[RootViewController alloc] init]; [self.window makeKeyAndVisible];
return YES;
} @end
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
#import "RootViewController.h"
#import <MapKit/MapKit.h>
#import "YXYCAnation.h"
@interface RootViewController ()<MKMapViewDelegate> @end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad];
// 初始化MKMapView
MKMapView *mapView = [[MKMapView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// 设置代理
mapView.delegate = self;
// 设置显示当前位置
mapView.showsUserLocation = YES;
// 设置地图显示类型
mapView.mapType = MKMapTypeStandard;
// 经纬度
CLLocationCoordinate2D coord2D = {23.117000,113.27500};
// 显示范围精度
MKCoordinateSpan span = {0.01,0.01};
// 显示区域
MKCoordinateRegion region = {coord2D,span};
// 给地图设置显示区域
[mapView setRegion:region animated:YES];
[self.view addSubview:mapView]; // 创建anation对象
CLLocationCoordinate2D showCoord = {23.117000,113.27500};
YXYCAnation *anation1 = [[YXYCAnation alloc] initWithCoordinate2D:showCoord];
anation1.title = @"越秀公园";
anation1.subtitle = @"小标题";
[mapView addAnnotation:anation1];
} #pragma mark -MKAnnotationView delegate-
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *identifier = @"Annotion";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotationView reuseIdentifier:identifier];
annotationView.image = [UIImage imageNamed:@""];
}
return annotationView;
} - (void)buttonAction:(UIButton *)sender
{
NSLog(@"显示公园简介");
} @end
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface YXYCAnation : NSObject<MKAnnotation> @property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle; - (id)initWithCoordinate2D:(CLLocationCoordinate2D)coordinate; @end
#import "YXYCAnation.h" @implementation YXYCAnation - (id)initWithCoordinate2D:(CLLocationCoordinate2D)coordinate
{
self = [super init];
if (self != nil) {
_coordinate = coordinate;
}
return self;
} @end
最新文章
- 前端组件化Polymer入门教程(8)——事件
- php 两段文本对比,不同的文字显示高亮
- 语义化HTML:ul、ol和dl
- android 基本布局(RelativeLayout、TableLayout等)使用方法及各种属性
- PAT 1016. 部分A+B (15)
- 为什么使用ConcurrentHashMap
- js控制文本框只能输入数字 及 常用字符对应ASCII码值
- 如何使盘ISO图像文件
- java中String与StringBuffer拼接的区别
- 浅谈canvas绘画王者荣耀--雷达图
- Frogger POJ - 2253
- Raid5两块硬盘掉线可以恢复数据吗_raid数据恢复案例分享
- Unable to instantiate Action, MenuAction, defined for &#39;QueryMenuAll&#39; in namespace &#39;/&#39;MenuAction
- php使用redis的几种常见方式和用法
- Array 新增加的一些API用法
- 关于spring事务
- node的模块包装为promise写法
- 【转】前端Web开发MVC模式-入门示例
- ES6标准入门之变量的解构赋值简单解说
- java并发基础(三)--- 任务执行