欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

iOS 开发摘要(一)--显示地图

最编程 2024-03-28 17:49:40
...
1.引入头文件

在AppDelegate.h中引入头文件,代码如下:

#import "AppDelegate.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [AMapServices sharedServices].apiKey = @"72ec793300c95f1dc32943eb885424be";
    return YES;
}
2.显示地图

一般应用使用地图时都会在地图上加上相应的其他子view,所以我个人的做法就是创建一个继承自MAMapView的mapView,将子view在这个新的mapView绘制以及做一些其他的地图相关设置,以减少controller中的代码量
在controller.m中代码如下:

#import "ViewController.h"
#import "AmapView.h"//继承的新mapview
@interface ViewController ()
@property (nonatomic, strong)AmapView *mapView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view addSubview:self.mapView];
}

- (AmapView *)mapView {
    if (!_mapView) {
        _mapView = [[AmapView alloc]initWithFrame:self.view.bounds];
    }
    return _mapView;
}

编译运行后,最原始的地图效果如下:

高德地图中还提供了地图上的其他设置比如比例尺,手势等,代码如下:

        //地图需要v4.5.0及以上版本才必须要打开此选项(v4.5.0以下版本,需要手动配置info.plist)
        [AMapServices sharedServices].enableHTTPS = YES;
        //如果您需要进入地图就显示定位小蓝点,则需要下面两行代码
        self.showsUserLocation = YES;
        self.userTrackingMode = MAUserTrackingModeFollow;
        //指南针
        self.showsCompass = NO;
        //logo位置
        self.logoCenter = CGPointMake(CGRectGetWidth(self.bounds)-55, 1000);
        //比例尺
        self.showsScale = NO;
        //旋转手势
        self.rotateEnabled= NO;
        //倾斜收拾
        self.rotateCameraEnabled = NO;
        //缩放级别
        [self setZoomLevel:12.0 animated:YES];
        //自定义蓝点
        MAUserLocationRepresentation *rep = [[MAUserLocationRepresentation alloc]init];
        rep.image = [UIImage imageNamed:@"定位点"];
        rep.showsAccuracyRing = NO;
        [self updateUserLocationRepresentation:rep];

以上就是对高德地图的基本设置,如能正常运行,就可以使用了。

推荐阅读