基於LBS平臺的iOS開發

LBS,即Location Based Services,基於位置服務,用於定位、導航等功能,好比地圖應用、訂外賣等的app就須要這個功能。ios

在這裏我使用的是高德LBS開放平臺,地址:http://lbs.amap.com/git

進入網站,首先註冊並認證爲開發者,而後爲你的每一個APP申請一個key,其中安全碼(Bundle Identifier)經過Xcode切換到General標籤,查看Bundle Identifier。github

使用第三方服務,咱們能夠使用自動配置,這裏就要使用到Cocoapods。CocoaPods是一個用來幫助咱們管理第三方依賴庫的工具。它能夠解決庫與庫之間的依賴關係,下載庫的源代碼,同時經過建立一個Xcode的workspace來將這些第三方庫和咱們的工程鏈接起來,供咱們開發使用。使用CocoaPods的目的是讓咱們能自動化的、集中的、直觀的管理第三方開源庫。api

若是須要升級ruby,使用homebrew安裝:brew install ruby
安全

另一種方法升級ruby: sudo gem update --system ruby

安裝Cocoapods教程:bash

gem sources -l
gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
gem sources -l
sudo gem install -n /usr/local/bin cocoapods
pod setup

建立Podfile:app

cd /project
touch Podfile

或者直接cd到工程目錄,而後pod init ,在Podfile裏直接添加也行。ide

編輯Podfile文件:工具

source 'https://github.com/CocoaPods/Specs.git'
platform:ios,'7.0' #手機系統
#pod 'AMap3DMap' #3D地圖SDK
pod 'AMap2DMap' #2D地圖SDK(2D和3D不能同時使用)
pod 'AMapSearch' #搜索服務SDK

如下是額外補充
#platform :ios  
#pod 'Reachability',  '~> 3.0.0'  
#pod 'SBJson', '~> 4.0.0'  

#platform :ios, '7.0'  
#pod 'AFNetworking', '~> 2.0'  

標準格式:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target 'AlamofireDemo' do
pod 'Alamofire'
pod 'SwiftyJSON'
end

 

而後到工程目錄,輸入命令pod  install,若是出現-bash: pod: command not found,輸入sudo gem install -n /usr/local/bin cocoapods便可解決。

若是若是pod update / pod install 卡在:Updating local specs repositories,

等待便可,或者能夠使用 「pod install --verbose --no-repo-update」 進行下載。

更新:若是你又添加新的三方庫,能夠在Podfile文件中繼續添加,而後再終端輸入pod update,而後CocoaPods就會從新幫你導入.

而後經過打開xcworkspace文件打開安裝有cocoapods的工程

如下是我Controller.m的核心代碼,僅供參考:

#import "ViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>
#define APIKey @"你申請的key"

@interface ViewController ()<MAMapViewDelegate,AMapSearchDelegate>
{
    MAMapView *_mapView;
    AMapSearchAPI *_search;
    CLLocation *_currentLocation;
    UIButton *_locationButton;
}

@end

@implementation ViewController

- (void)initControls
{
    _locationButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _locationButton.frame = CGRectMake(20, CGRectGetHeight(_mapView.bounds)-80,40,40);
    _locationButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
    _locationButton.backgroundColor = [UIColor whiteColor];
    _locationButton.layer.cornerRadius = 5;
    [_locationButton addTarget:self action:@selector(locateAction) forControlEvents:UIControlEventTouchUpInside];
    [_locationButton setImage:[UIImage imageNamed:@"location_no"] forState:UIControlStateNormal];
    [_mapView addSubview:_locationButton];
    
}
//搜索服務
- (void)initSearch
{
    _search = [[AMapSearchAPI alloc]initWithSearchKey:APIKey Delegate:self];
}

//修改用戶定位模式
- (void)locateAction
{
    if (_mapView.userTrackingMode != MAUserTrackingModeFollow) {
        [_mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES];
    }
}
//地理編碼搜索請求
- (void)reGeoAction
{
    if(_currentLocation)
    {
        AMapReGeocodeSearchRequest *request = [[AMapReGeocodeSearchRequest alloc]init];
        request.location = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
        [_search AMapGeocodeSearch:request];
    }
}

- (void)searchRequest:(id)request didFailWithError:(NSError *)error
{
    NSLog(@"request :%@, error :%@",request,error);
}
- (void)onReGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    NSLog(@"response :%@",response);
    NSString *title = response.regeocode.addressComponent.city;
    if (title.length == 0) {
        title = response.regeocode.addressComponent.province;
    }
    _mapView.userLocation.title = title;
    _mapView.userLocation.subtitle = response.regeocode.formattedAddress;
}

//用戶代理方法
- (void)mapView:(MAMapView *)mapView didChangeUserTrackingMode:(MAUserTrackingMode)mode animated:(BOOL)animated
{
    //修改定位按鈕狀態
    if(mode == MAUserTrackingModeNone)
    {
        [_locationButton setImage:[UIImage imageNamed:@"location_no"] forState:UIControlStateNormal];
    }
    else{
        [_locationButton setImage:[UIImage imageNamed:@"location_yes"] forState:UIControlStateNormal];
    }
}

//記錄當前所在位置
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    NSLog(@"userLocation: %@",userLocation.location);
    _currentLocation = [userLocation.location copy];
    
}

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view
{
    //選中定位annotation的時候進行逆地理編碼查詢
    if ([view.annotation isKindOfClass:[MAUserLocation class]])
    {
        [self reGeoAction];
    }
}


//初始化地圖
- (void)initMapView
{
    [MAMapServices sharedServices].apiKey = APIKey;
    _mapView = [[MAMapView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    //設置地圖的代理和兩個附件的位置
    _mapView.delegate = self;
    _mapView.compassOrigin = CGPointMake(_mapView.compassOrigin.x, 22);
    _mapView.scaleOrigin = CGPointMake(_mapView.scaleOrigin.x, 22);
    [self.view addSubview:_mapView];
    //打開用戶定位
    _mapView.showsUserLocation = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self initMapView];
    [self initSearch];
    [self initControls];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

運行結果:

若是你有更多的API接口想要實現,你能夠去高德LBS開放平臺去看看開發文檔,能夠做進一步深刻和開發。

github地址:https://github.com/AbelSu131/HelloAmap

相關文章
相關標籤/搜索