【iOS开发】—— 调用地图SDK完成对附近地点的获取

最近在写项目的时候,首页打算加一个地图SDK,并且在地图上标记出附近的球馆。由于附近球馆比较少,示例用电影院。

第一步:导入第三方库

首先要完成第三方库的导入:在这个实现中,主要需要导入下面三个库:
AMap3DMap(用于显示地图)
AMapLocation 定位SDK
AMapSearch搜索SDK

导入的时候可以通过CocoaPods,可以看这篇博客去配置:【iOS开发】——CocoaPods的基本使用

第二步:设置info.plist

请添加图片描述

第三步:添加地图

    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, W, H/2)];
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    [self.view addSubview:_mapView];

第四步:设置定位蓝点

	MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];
    r.showsHeadingIndicator = YES;
    r.image = [UIImage imageNamed:@"daohang.png"];
    [_mapView updateUserLocationRepresentation:r];

定位当前位置

    self.locManager = [[CLLocationManager alloc] init];
    self.locManager.delegate = self;
    self.locManager.distanceFilter = 10.0f;
    self.locManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    [self.locManager startUpdatingLocation];

代理方法:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
   CLLocation *newLocation = locations[0];

   // 获取当前所在的城市名
   CLGeocoder *geocoder = [[CLGeocoder alloc] init];
   //根据经纬度反向地理编译出地址信息
   [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error){
       if (array.count > 0){
           CLPlacemark *placemark = [array objectAtIndex:0];
           
           //获取城市
           NSString *city = placemark.locality;
           if (!city) {
               //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
               city = placemark.administrativeArea;
           }
           [self searchPOI:city]; //获取到位置后调用搜索方法去获取当前城市的电影院
           
           NSLog(@"city = %@", city);
           
       }
       else if (error == nil && [array count] == 0)
       {
           NSLog(@"No results were returned.");
       }
       else if (error != nil)
       {
           NSLog(@"An error occurred = %@", error);
       }
   }];
   
}

第五步:搜索方法

我这里用的是搜索所在城市的电影院,如果有需要搜索附近或者多边形内的地点等可以看高德官方开发文档

- (void)searchPOI:(NSString*)city {
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;
    AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
    request.keywords = @"电影院";//关键字
    request.city = city; //所在城市
    request.types = @"电影院";//地点的类别
    request.requireExtension = YES;
    request.cityLimit  = YES;
    request.requireSubPOIs = YES;
    [self.search AMapPOIKeywordsSearch:request];
}

第六步:设置搜索的代理方法

- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    if (response.pois.count == 0)
    {
        NSLog(@"没有查询到相关场所");
    } else {
        NSLog(@"%ld", response.pois.count);
        
        [response.pois enumerateObjectsUsingBlock:^(AMapPOI *obj, NSUInteger idx, BOOL *stop) {
            MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
            //将搜索到的位置标记到地图中
            pointAnnotation.coordinate = CLLocationCoordinate2DMake(obj.location.latitude, obj.location.longitude);
            
            [_mapView addAnnotation:pointAnnotation];
            
        }];
    }
}

第七步:自定义绘制点

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
        if ([annotation isKindOfClass:[MAPointAnnotation class]])
        {
            static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
            MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
            if (annotationView == nil)
            {
                annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
            }
            annotationView.canShowCallout= YES;       //设置气泡可以弹出,默认为NO
            annotationView.animatesDrop = YES;        //设置标注动画显示,默认为NO
            annotationView.draggable = YES;        //设置标注可以拖动,默认为NO
            annotationView.pinColor = MAPinAnnotationColorPurple;
            return annotationView;
        }
        return nil;
}

完整代码:

//viewController.h
#import <UIKit/UIKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
@interface ViewController : UIViewController
<AMapSearchDelegate,
CLLocationManagerDelegate,
MAMapViewDelegate>
@property (nonatomic) AMapSearchAPI* search;
@property (nonatomic, strong) MAMapView *mapView;
@property (strong, nonatomic) CLLocationManager *locManager;
@property (strong, nonatomic) CLLocation *checkinLocation;
@end
//viewController.m
#import "ViewController.h"
#define W [UIScreen mainScreen].bounds.size.width
#define H [UIScreen mainScreen].bounds.size.height
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, W, H/2)];
    
     //如果您需要进入地图就显示定位小蓝点,则需要下面两行代码
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    
    MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];
    r.showsHeadingIndicator = YES;
    r.image = [UIImage imageNamed:@"daohang.png"];
    [_mapView updateUserLocationRepresentation:r];
    
    [self.view addSubview:_mapView];
    
    
    
    self.locManager = [[CLLocationManager alloc] init];
    self.locManager.delegate = self;
    self.locManager.distanceFilter = 10.0f;
    self.locManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    [self.locManager startUpdatingLocation];
}

- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    if (response.pois.count == 0)
    {
        NSLog(@"没有查询到相关场所");
    } else {
        NSLog(@"%ld", response.pois.count);
        
        [response.pois enumerateObjectsUsingBlock:^(AMapPOI *obj, NSUInteger idx, BOOL *stop) {
            MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
            pointAnnotation.coordinate = CLLocationCoordinate2DMake(obj.location.latitude, obj.location.longitude);
            
                [_mapView addAnnotation:pointAnnotation];
            
        }];
    }
}
    
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error
{
    NSLog(@"Error: %@", error);
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
        if ([annotation isKindOfClass:[MAPointAnnotation class]])
        {
            static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
            MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
            if (annotationView == nil)
            {
                annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
            }
            annotationView.canShowCallout= YES;       //设置气泡可以弹出,默认为NO
            annotationView.animatesDrop = YES;        //设置标注动画显示,默认为NO
            annotationView.draggable = YES;        //设置标注可以拖动,默认为NO
            annotationView.pinColor = MAPinAnnotationColorPurple;
            return annotationView;
        }
        return nil;
}
    

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
   CLLocation *newLocation = locations[0];

   // 获取当前所在的城市名
   CLGeocoder *geocoder = [[CLGeocoder alloc] init];
   //根据经纬度反向地理编译出地址信息
   [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error){
       if (array.count > 0){
           CLPlacemark *placemark = [array objectAtIndex:0];
           
           //获取城市
           NSString *city = placemark.locality;
           if (!city) {
               //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
               city = placemark.administrativeArea;
           }
           [self searchPOI:city];
           
           NSLog(@"city = %@", city);
           
       }
       else if (error == nil && [array count] == 0)
       {
           NSLog(@"No results were returned.");
       }
       else if (error != nil)
       {
           NSLog(@"An error occurred = %@", error);
       }
   }];
   
}

- (void)searchPOI:(NSString*)city {
    
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;
    AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
    request.keywords = @"电影院";
    request.city = city;
    request.types = @"电影院";
    request.requireExtension = YES;
    request.cityLimit  = YES;
    request.requireSubPOIs = YES;
    [self.search AMapPOIKeywordsSearch:request];
}
@end

注意:别忘了在AppDelegate.m文件中加入自己的apiKey:

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

效果:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值