第三方SDK集成iOS开发资料收集区

iOS 高德地图的简单使用心得

2016-05-07  本文已影响2279人  随便的昵称都被占用

定位 搜索 附近相关信息

#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
@interface ViewController ()<MAMapViewDelegate,AMapSearchDelegate>
{
    MAMapView * _mapView;
    AMapSearchAPI * _search;
    AMapLocationManager * _locationManager;
    NSString * _position;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self searchPosition];
    [self creatAmapView];
    [self getCurrentPositionInfo];
}
#pragma  mark -------------------根据经纬度搜索周围信息-------------------------
-(void)searchPosition{
    
    //搜索地址获得地址附近信息
    [AMapSearchServices sharedServices].apiKey = @"你申请的key";
    _search = [[AMapSearchAPI alloc] init];
    _search.delegate = self;
    [self ahah];
    
}

-(void)ahah{
    
    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
    request.location = [AMapGeoPoint locationWithLatitude:26.063184 longitude:119.298224];
    request.keywords = _position;
    // types属性表示限定搜索POI的类别,默认为:餐饮服务|商务住宅|生活服务
    // POI的类型共分为20种大类别,分别为:
    // 汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|
    // 医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|
    // 交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施
    request.types = @"餐饮服务|生活服务|商务住宅";
    request.sortrule = 0;
    request.radius = 700;
    request.requireExtension = YES;
    
    //发起周边搜索
    [_search AMapPOIAroundSearch: request];
}
//实现POI搜索对应的回调函数
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    if(response.pois.count == 0)
    {
        return;
    }
    
    //通过 AMapPOISearchResponse 对象处理搜索结果
    //NSString *strCount = [NSString stringWithFormat:@"count: %ld",response.count];
    //NSString *strSuggestion = [NSString stringWithFormat:@"Suggestion: %@", response.suggestion];
    NSString *strPoi = @"";
    for (AMapPOI *p in response.pois) {
        strPoi = [NSString stringWithFormat:@"%@\nPOI: %@", strPoi, p.description];
        NSLog(@"%@*%@*%@",p.description,p.name,p.address);
    }
}
#pragma mark ------------------------地图的创建---------------------------
-(void)creatAmapView{
    
    //地图
    [MAMapServices sharedServices].apiKey = @"你申请的key";
    _mapView = [[MAMapView alloc] initWithFrame:self.view.frame];
    _mapView.showsUserLocation = YES;
    [_mapView setZoomLevel:16.1 animated:YES];
    _mapView.delegate = self;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    [self.view addSubview:_mapView];
    
}

-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation
{
    if(updatingLocation)
    {
        //取出当前位置的坐标
        //NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
    }
}
#pragma mark -----------------获得当前位置信息城市 区 街道-------------------------


-(void)getCurrentPositionInfo{
    [AMapLocationServices sharedServices].apiKey = @"你申请的key";
    _locationManager = [[AMapLocationManager alloc] init];
    [_locationManager setDistanceFilter:kCLLocationAccuracyKilometer];
    [self getCurrentAddress];
 }
//获取当前位置信息
-(void)getCurrentAddress{
    
    [_locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        
        if (error) {
            NSLog(@"获取当前地址错误");
            return;
        }
        NSMutableDictionary *addressDic = [NSMutableDictionary dictionary];
        [addressDic setValue:regeocode.province forKey:@"province"];
//        [addressDic setValue:regeocode.city forKey:@"city"];
//        [addressDic setValue:regeocode.district forKey:@"district"];
        NSString * position = [NSString stringWithFormat:@"%@%@%@%@",regeocode.city,regeocode.district,regeocode.street,regeocode.number];
        _position = position;
      
//city 市 district 区 street 街道 number  街道号码 
       NSLog(@"%@%@%@%@",regeocode.city,regeocode.district,regeocode.street,regeocode.number);
     }];
    
}
#pragma mark --------点击地图得到点击点的坐标------------------


//点击地图获得该点击地点的经纬度
-(void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate{

    NSLog(@"%f,%f",coordinate.latitude,coordinate.longitude);


}```
上一篇下一篇

猜你喜欢

热点阅读