基于高德地图SDK进行搜索

高德地图SDK使用地址http://lbs.amap.com/

地图设置


#define GDMAPKEY @"key"


#import "ViewController.h"

#import <MapKit/MapKit.h>

#import <AMapSearchKit/AMapSearchAPI.h>

#import "ZVAnnotation.h"

#import "ZVLeftCalloutView.h"


@interface ViewController ()<MKMapViewDelegate,UITextFieldDelegate,CLLocationManagerDelegate,AMapSearchDelegate>

@property(nonatomic, retain)UITextField *searchBar;

@property(nonatomic ,strong)MKMapView *mapView;

@property(nonatomic ,strong)AMapSearchAPI *search;

@property(nonatomic ,strong)CLLocationManager *locManager;

@property(nonatomic ,assign)CLLocationCoordinate2D currentLocation;

@property(nonatomic ,assign)CLLocationCoordinate2D myLocation;


@end


@implementation ViewController



-(void)viewWillAppear:(BOOL)animated

{

//    [_mapView viewWillAppear];

    _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放

    _locManager.delegate = self;

}

-(void)viewWillDisappear:(BOOL)animated

{

//    [_mapView viewWillDisappear];

    _mapView.delegate = nil; // 不用时,置nil

    _locManager.delegate = nil;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    _locManager = [[CLLocationManager alloc] init];

    _locManager.distanceFilter = 100;

    

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)

    {

        if ([_locManager respondsToSelector:@selector(requestAlwaysAuthorization)])

        {

            [_locManager performSelector:@selector(requestAlwaysAuthorization)];//用这个方法,plist中需要NSLocationAlwaysUsageDescription

        }

        

        if ([_locManager respondsToSelector:@selector(requestWhenInUseAuthorization)])

        {

            [_locManager performSelector:@selector(requestWhenInUseAuthorization)];//用这个方法,plist里要加字段NSLocationWhenInUseUsageDescription

        }

    }

    

    _mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];

    _mapView.userTrackingMode = MKUserTrackingModeFollow;

    _mapView.showsUserLocation = YES;

    [self.view addSubview: _mapView];

    

    UIButton *locationBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [locationBtn setBackgroundImage:[UIImage imageNamed:@"定位"] forState:UIControlStateNormal];

    [locationBtn setFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.height-30, 20, 20)];

    [locationBtn addTarget:self action:@selector(startLocation:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:locationBtn];

    

    UITextField *searchBar = [[UITextField alloc]init];

    [searchBar setFrame:CGRectMake(10, 20, [UIScreen mainScreen].bounds.size.width-20, 30)];

    searchBar.borderStyle = UITextBorderStyleRoundedRect;

    searchBar.delegate = self;

    searchBar.returnKeyType = UIReturnKeySearch;

    [self.view addSubview:searchBar];

    

    _search = [[AMapSearchAPI alloc] initWithSearchKey:GDMAPKEY Delegate:self];

}



#pragma mark - MKMapViewDelegate

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

{

    _currentLocation = mapView.centerCoordinate;

}


-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation

{

    if ([annotation isKindOfClass:[ZVAnnotation class]]) {

        MKAnnotationView *newAnnotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];

        newAnnotationView.image = [UIImage imageNamed:[NSString stringWithFormat:@"flower"]];

        [newAnnotationView setBounds:CGRectMake(0, 0, 24, 24)];

        newAnnotationView.canShowCallout = YES;

       

        ZVAnnotation *ann = annotation;

        UIImageView *imv = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)];

        imv.image = [UIImage imageNamed:ann.imageUrl];

        newAnnotationView.leftCalloutAccessoryView = imv;

        

        

        

        UIButton *detailBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        [detailBtn addTarget:self action:@selector(showDetail) forControlEvents:UIControlEventTouchUpInside];

        newAnnotationView.rightCalloutAccessoryView = detailBtn;

        

        return newAnnotationView;

        

    }

    return nil;

}



#pragma mark - AMapSearchDelegate


//实现POI搜索对应的回调函数

- (void)onPlaceSearchDone:(AMapPlaceSearchRequest *)request response:(AMapPlaceSearchResponse *)response

{

    

    if(response.pois.count == 0)

    {

        return;

    }

    

    //通过AMapPlaceSearchResponse对象处理搜索结果

    NSString *strCount = [NSString stringWithFormat:@"count: %d",(int)response.count];

    NSString *strSuggestion = [NSString stringWithFormat:@"Suggestion: %@", response.suggestion];

    NSString *strPoi = @"";

    for (AMapPOI *p in response.pois) {

        strPoi = [NSString stringWithFormat:@"%@\nPOI: %@", strPoi, p.description];

    }

    NSString *result = [NSString stringWithFormat:@"%@ \n %@ \n %@", strCount, strSuggestion, strPoi];

    NSLog(@"Place: %@", result);

    

    NSMutableArray *arr = [NSMutableArray array];

    for (int i = 0; i<response.pois.count; i++)

    {

        AMapPOI * p = response.pois[i];

        CLLocationCoordinate2D coordi = CLLocationCoordinate2DMake(p.location.latitude, p.location.longitude);

//        MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];

        ZVAnnotation *zvannotation = [[ZVAnnotation alloc]init];

        zvannotation.coordinate = coordi;

        zvannotation.imageUrl = @"icon";

        zvannotation.title = p.name;

        zvannotation.subtitle = [NSString stringWithFormat:@"与你的距离%d",(int)p.distance];

        [arr addObject:zvannotation];

    }

    [_mapView addAnnotations:arr];

}


#pragma mark - CLLocationManagerDelegate

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

    CLLocation *location = [locations lastObject];

    _myLocation = location.coordinate;

    [_locManager stopUpdatingLocation];

    

}



#pragma mark - UIEventHandle

- (void)startLocation:(id)sender

{

    [_locManager startUpdatingLocation];

    [_mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

}


- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    

    AMapPlaceSearchRequest *request = [[AMapPlaceSearchRequest alloc] init];

    

    request.searchType          = AMapSearchType_PlaceAround;

    request.location            = [AMapGeoPoint locationWithLatitude:

                                   _currentLocation.latitude longitude:_currentLocation.longitude];

    request.radius = 1000;

    request.keywords            = textField.text;

    /* 按照距离排序. */

    request.sortrule            = 1;

    //request.requireExtension    = YES;

    

    /* 添加搜索结果过滤 */

//    AMapPlaceSearchFilter *filter = [[AMapPlaceSearchFilter alloc] init];

//    filter.costFilter = @[@"100", @"200"];

//    filter.requireFilter = AMapRequireGroupbuy;

//    request.searchFilter = filter;

    

    [self.search AMapPlaceSearch:request];

    

    return YES;

}


-(void)showDetail

{

    UIViewController *vc = [[UIViewController alloc]init];

    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];

    lab.text = @"11";

    UIView *view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    vc.view = view;

    view.backgroundColor = [UIColor redColor];

    [vc.view addSubview:lab];

    [self presentViewController:vc

                       animated:YES

                     completion:nil];

}

@end



自定义annotation

.h

//

//  ZVAnnotation.h

//

//  Created by ZV on 15-5-11.

//  Copyright (c) 2015 zv. All rights reserved.

//


#import <Foundation/Foundation.h>

#import <MapKit/MKAnnotation.h>


@interface ZVAnnotation : NSObject<MKAnnotation>


@property (nonatomic ,copy)NSString *imageUrl;

@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) NSString *subtitle;

@end


.m

//

//  ZVAnnotation.m

//

//  Created by ZV on 15-5-11.

//  Copyright (c) 2015 zv. All rights reserved.

//


#import "ZVAnnotation.h"


@implementation ZVAnnotation

@synthesize coordinate = _coordinate;


-(void)setCoordinate:(CLLocationCoordinate2D)newCoordinate

{

    _coordinate = newCoordinate;

}

@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值