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

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

最近在写项目的时候,首页打算加一个地图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;       //设置气泡可以d出,默认为NO
            annotationView.animatesDrop = YES;        //设置标注动画显示,默认为NO
            annotationView.draggable = YES;        //设置标注可以拖动,默认为NO
            annotationView.pinColor = MAPinAnnotationColorPurple;
            return annotationView;
        }
        return nil;
}
完整代码:
//viewController.h
#import 
#import 
#import 
#import 
#import 
@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;       //设置气泡可以d出,默认为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;
}
效果:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/993879.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存