高德地图详细使用方法

1.简单定位

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _mapView.showsUserLocation = YES;
    [_mapView setUserTrackingMode:MAUserTrackingModeFollow];
}

- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    NSLog(@"定位中...");
}

View Code

2.创建大头针

#import "AnnotationViewController.h"

@interface AnnotationViewController ()

@end

@implementation AnnotationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    MAPointAnnotation *annotation1 = [[MAPointAnnotation alloc]init];
    annotation1.coordinate = CLLocationCoordinate2DMake(39.9, 116.4);
    annotation1.title = @"北京";
    annotation1.subtitle = @"beijing";
    [_mapView addAnnotation:annotation1];
    
    MAPointAnnotation *annotation2 = [[MAPointAnnotation alloc]init];
    annotation2.coordinate = CLLocationCoordinate2DMake(39.94, 116.44);
    annotation2.title = @"上海";
    annotation2.subtitle = @"shanghai";
    [_mapView addAnnotation:annotation2];

}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    static NSString *anIde = @"PointAnnotation";
    MAAnnotationView *view = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:anIde];
    if (!view) {
        view = [[MAAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:anIde];
//        view.animatesDrop=YES;
        view.draggable=YES;
        view.image = [UIImage imageNamed:@"1.png"];
        view.canShowCallout = YES;
    }
    return view;
}

- (void)mapView:(MAMapView *)mapView didDeselectAnnotationView:(MAAnnotationView *)view
{
    
}

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

View Code

3.自定制大头针(实现MAAnnotation代理)

#import <Foundation/Foundation.h>
#import <MAMapKit/MAMapKit.h>
@interface CustomAnnotation : NSObject<MAAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *subtitle;
//
@property (nonatomic,copy) NSString *leftImgName;

@end

View Code

4.自定制大头针内容(继承MAAnnotationView)

#import "CustomAnnotationView.h"

@implementation CustomAnnotationView
{
    DetailView *_detail;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    
    if (self.selected == selected) {
        return;
    }

    //选中添加详情
    if (selected)
    {
        if (!_detail) {
            _detail = [[DetailView alloc]initWithFrame:CGRectMake((-140+40)/2, -50, 140, 50)];
        }
        [self addSubview:_detail];
        CustomAnnotation *annotation = self.annotation;
        _detail.leftImgView.image = [UIImage imageNamed:annotation.leftImgName];
        _detail.titleLabel.text = annotation.title;
        _detail.subtitleLabel.text = annotation.subtitle;
    }
    //未选中时
    else
    {
        //移除详情
        [_detail removeFromSuperview];
    }
    
    [super setSelected:selected animated:animated];
}

@end

View Code

5.使用高德地图部分方法

#import "CustomAnnotationViewController.h"
#import "CustomAnnotationView.h"
#import <AMapSearchKit/AMapSearchAPI.h>
@interface CustomAnnotationViewController ()<AMapSearchDelegate>
{
    AMapSearchAPI *_searchApi;
}
@end
const static NSString *key = @"d0ab6d8ab6902765f6714c1534b4d4a0";
@implementation CustomAnnotationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CustomAnnotation *ann = [[CustomAnnotation alloc]init];
    ann.coordinate = CLLocationCoordinate2DMake(39.9, 116.4);
    ann.title = @"肯德基";
    ann.subtitle = @"KFC";
    ann.leftImgName = @"3.png";
    [_mapView addAnnotation:ann];
    
    MACircle *circle = [MACircle circleWithCenterCoordinate:ann.coordinate radius:3000];
    [_mapView addOverlay:circle];
    
    //搜索附近的kfc
    _searchApi = [[AMapSearchAPI alloc]initWithSearchKey:key Delegate:self];
    //创建request对象
    AMapPlaceSearchRequest *request = [[AMapPlaceSearchRequest alloc]init];
    //指定searchtype为周边搜索
    request.searchType = AMapSearchType_PlaceAround;
    //指定搜索附近的中心点位置
    request.location = [AMapGeoPoint locationWithLatitude:ann.coordinate.latitude longitude:ann.coordinate.longitude];
    //指定搜索半径
    request.radius = 3000;
    //关键字
    request.keywords = @"KFC";
    [_searchApi AMapPlaceSearch:request];
//    
//    AMapPlaceSearchRequest *request1 = [[AMapPlaceSearchRequest alloc]init];
//    request1.keywords = @"俏江南";
//    request1.city = @[@"beijing"];
//    [_searchApi AMapPlaceSearch:request1];
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        static NSString *annIde = @"PointAnnotation";
        MAPinAnnotationView *view = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annIde];
        if (!view) {
            view = [[MAPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annIde];
            view.canShowCallout = YES;
        }
        return view;
    }
    static NSString *annIde = @"CustomAnnotation";
    CustomAnnotationView *view = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annIde];
    if (!view) {
        view = [[CustomAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annIde];
    }
    view.image = [UIImage imageNamed:@"1.png"];
    return view;
}

- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay
{
    MACircleRenderer *circleRe = [[MACircleRenderer alloc]initWithOverlay:overlay];
    circleRe.strokeColor = [UIColor redColor];
    circleRe.fillColor = [UIColor colorWithWhite:0.2 alpha:0.4];
    circleRe.lineWidth = 5;
    return circleRe;
}

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view
{
    //导航查询
    AMapNavigationSearchRequest *request = [[AMapNavigationSearchRequest alloc]init];
    request.searchType = AMapSearchType_NaviDrive;
    //起点
    request.origin = [AMapGeoPoint locationWithLatitude:39.9 longitude:116.4];
    //终点
    request.destination = [AMapGeoPoint locationWithLatitude:[view.annotation coordinate].latitude longitude:[view.annotation coordinate].longitude];
    [_searchApi AMapNavigationSearch:request];
}

- (void)onNavigationSearchDone:(AMapNavigationSearchRequest *)request response:(AMapNavigationSearchResponse *)response
{
    AMapRoute *route = response.route;
    NSArray *paths = route.paths;
    for (AMapPath *path in paths) {
        for (AMapStep *step in path.steps) {
            NSLog(@"%@",step.instruction);
        }
    }
}

- (void)onPlaceSearchDone:(AMapPlaceSearchRequest *)request response:(AMapPlaceSearchResponse *)response
{
    if (!response.count) {
        return;
    }
    
    //遍历搜索结果,创建annotation,显示搜索结果
    for (AMapPOI *poi in response.pois) {
        MAPointAnnotation *annotation = [[MAPointAnnotation alloc]init];
        annotation.coordinate = CLLocationCoordinate2DMake(poi.location.latitude, poi.location.longitude);
        annotation.title = poi.name;
        annotation.subtitle = poi.tel;
        [_mapView addAnnotation:annotation];
    }
}

- (void)searchRequest:(id)request didFailWithError:(NSError *)error
{
    NSLog(@"%@",error);
}

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

@end

View Code

 

版权声明:本文为liaods原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/liaods/p/4805568.html