基於高德地圖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"
@interfaceViewController ()<MKMapViewDelegate,UITextFieldDelegate,CLLocationManagerDelegate,
@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 {
[superviewDidLoad];
_locManager = [[CLLocationManageralloc] init];
_locManager.distanceFilter = 100;
if ([CLLocationManagerauthorizationStatus] == kCLAuthorizationStatusNotDetermined)
{
if ([_locManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[_locManagerperformSelector:@selector(requestAlwaysAuthorization)];//用這個方法,plist中需要NSLocationAlwaysUsageDescription
}
if ([_locManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[_locManagerperformSelector:@selector(requestWhenInUseAuthorization)];//用這個方法,plist裡要加欄位NSLocationWhenInUseUsageDescription
}
}
_mapView = [[MKMapViewalloc]initWithFrame:self.view.bounds];
_mapView.userTrackingMode = MKUserTrackingModeFollow;
_mapView.showsUserLocation = YES;
[self.viewaddSubview: _mapView];
UIButton *locationBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
[locationBtn setBackgroundImage:[UIImageimageNamed:@"定位"] forState:UIControlStateNormal];
[locationBtn setFrame:CGRectMake(10, [UIScreenmainScreen].bounds.size.height-30, 20, 20)];
[locationBtn addTarget:selfaction:@selector(startLocation:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:locationBtn];
UITextField *searchBar = [[UITextFieldalloc]init];
[searchBar setFrame:CGRectMake(10, 20, [UIScreenmainScreen].bounds.size.width-20, 30)];
searchBar.borderStyle = UITextBorderStyleRoundedRect;
searchBar.delegate = self;
searchBar.returnKeyType = UIReturnKeySearch;
[self.view addSubview:searchBar];
_search = [[AMapSearchAPIalloc] initWithSearchKey:GDMAPKEYDelegate: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 = [[MKAnnotationViewalloc]initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
newAnnotationView.image = [UIImageimageNamed:[NSStringstringWithFormat:@"flower"]];
[newAnnotationView setBounds:CGRectMake(0, 0, 24, 24)];
newAnnotationView.canShowCallout = YES;
ZVAnnotation *ann = annotation;
UIImageView *imv = [[UIImageViewalloc] initWithFrame:CGRectMake(5, 5, 40, 40)];
imv.image = [UIImage imageNamed:ann.imageUrl];
newAnnotationView.leftCalloutAccessoryView = imv;
UIButton *detailBtn = [UIButtonbuttonWithType:UIButtonTypeDetailDisclosure];
[detailBtn addTarget:selfaction:@selector(showDetail) forControlEvents:UIControlEventTouchUpInside];
newAnnotationView.rightCalloutAccessoryView = detailBtn;
return newAnnotationView;
}
returnnil;
}
#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 = [NSStringstringWithFormat:@"%@\nPOI: %@", strPoi, p.description];
}
NSString *result = [NSString stringWithFormat:@"%@ \n %@ \n %@", strCount, strSuggestion, strPoi];
NSLog(@"Place: %@", result);
NSMutableArray *arr = [NSMutableArrayarray];
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];
}
[_mapViewaddAnnotations:arr];
}
#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
_myLocation = location.coordinate;
[_locManagerstopUpdatingLocation];
}
#pragma mark - UIEventHandle
- (void)startLocation:(id)sender
{
[_locManagerstartUpdatingLocation];
[_mapViewsetUserTrackingMode:MKUserTrackingModeFollowanimated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
AMapPlaceSearchRequest *request = [[AMapPlaceSearchRequestalloc] init];
request.searchType = AMapSearchType_PlaceAround;
request.location = [AMapGeoPointlocationWithLatitude:
_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.searchAMapPlaceSearch:request];
returnYES;
}
-(void)showDetail
{
UIViewController *vc = [[UIViewControlleralloc]init];
UILabel *lab = [[UILabelalloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
lab.text = @"11";
UIView *view = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
vc.view = view;
view.backgroundColor = [UIColorredColor];
[vc.view addSubview:lab];
[selfpresentViewController: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