技术文章分类(180)

技术随笔(11)

高德地图api使用in swift

 

1,导入库,具体方法参考官方文档http://developer.amap.com/api/ios-sdk/guide/project/

其中特别注意:AMap.bundle按照要求,拖进来选择copy if needed

 

2,修改Build Setting的4个地方的配置信息

以上两步操作,我想objc中引入到的地图也是这么操作的

3,在Header-Bridging.h中import你想导入的类

经几番折腾得出结论:swift在引入高德地图时还是存在bug,等待苹果修复中。现在只能用objc了。以下是地图的最基本功能,高德地图集成大量api,都必须在这个基础上拓展。

//
//  MapViewController.h
//  SwiftStudy3
//
//  Created by dongway on 14-7-9.
//  Copyright (c) 2014年 dongway. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "MAMapKit/MAMapKit.h"

@interface MapViewController : UIViewController<MAMapViewDelegate>

@property(nonatomic) MAMapView *mapView;

@end
//
//  MapViewController.m
//  SwiftStudy3
//
//  Created by dongway on 14-7-9.
//  Copyright (c) 2014年 dongway. All rights reserved.
//

#import "MapViewController.h"

//此处坐标为长沙某地坐标
#define DeviceFrame [UIScreen mainScreen].applicationFrame
#define Latitude 28.1658601239483
#define Longitude 112.947241748764

@interface MapViewController ()

@end

@implementation MapViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // Do any additional setup after loading the view.
    self.mapView = [[MAMapView alloc] initWithFrame:DeviceFrame];
    self.mapView.delegate = self;
    self.mapView.visibleMapRect = MAMapRectMake(220880104.0, 101476980.0,  9249.0, 13265.0);
    self.mapView.centerCoordinate = CLLocationCoordinate2DMake(Latitude, Longitude);
    
    NSMutableArray *annotations = [[NSMutableArray alloc] init];
    MAPointAnnotation *point1 = [[MAPointAnnotation alloc] init];
    point1.coordinate = CLLocationCoordinate2DMake(Latitude, Longitude);
    [annotations addObject:point1];
    [self.mapView addAnnotations:annotations];
    
    [self.view addSubview:self.mapView];
    
  }

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

-(MAAnnotationView*)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndetifier = @"pointReuseIndetifier";
        MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
            annotationView.canShowCallout= NO;      //设置气泡可以弹出,默认为NO
            annotationView.animatesDrop = YES;       //设置标注动画显示,默认为NO
            annotationView.draggable = NO;           //设置标注可以拖动,默认为NO
//            annotationView.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];  //设置气泡右侧按钮
        }
        return annotationView;
    }
    return nil;
}

@end

 

posted @ 2014-07-09 09:42  坤哥MartinLi  阅读(1281)  评论(0编辑  收藏  举报