ZFMapManager
https://github.com/FanyiZeng/ZFMapManager
選擇地點
搜索地點
管理定位模塊
使用說明:
- 您需要在您的Xcode工程中引入
CoreLocation.framework,
QuartzCore.framework罗捎、
OpenGLES.framework甥材、
SystemConfiguration.framework、
CoreGraphics.framework蛙吏、
Security.framework、
libsqlite3.0.tbd(xcode7以前為 libsqlite3.0.dylib)、
CoreTelephony.framework 鸦做、
libstdc++.6.0.9.tbd
下圖所示所有百度SDK
mapapi.bundle 路徑:BaiduMapAPI_Map.framework/Resources
mapapi.bundle 路徑:BaiduMapAPI_Map.framework/Resources
- 添加支持HTTPS所需的penssl靜態(tài)庫:libssl.a和libcrypto.a(SDK打好的包存放于Framework目錄下)
添加方法: 在 TARGETS->Build Phases-> Link Binary With Libaries中點擊“+”按鈕励烦,在彈出的窗口中點擊“Add Other”按鈕,選擇libssl.a和libcrypto.a添加到工程中
- 從BaiduMapAPI_Map.framework||Resources文件中選擇mapapi.bundle文件泼诱,并勾選“Copy items if needed”復(fù)選框坛掠,單擊“Add”按鈕,將資源文件添加到工程中治筒。
- 在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC却音。
- 添加定位允許 NSLocationWhenInUseUsageDescription
- 構(gòu)造函數(shù) 使用前還需要先配置自己的AK 在構(gòu)造函數(shù)內(nèi).
ZFMapManager.m
L : 76
- (instancetype)init
{
self = [super init];
if (self) {
// 要使用百度地圖,請先啟動BaiduMapManager
_mapManager = [[BMKMapManager alloc]init];
// 如果要關(guān)注網(wǎng)絡(luò)及授權(quán)驗證事件矢炼,請設(shè)定 generalDelegate參數(shù)
// BundleID 為 net.yeah.fanyizeng.---- 如果無法使用請更換為自己注冊的BundleID
BOOL ret = [_mapManager start:@"tUWhg88bXiiBnR39fri78LBhTEc0bN95" generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
...
使用接口
使用ZFMapManager
提供的方法進(jìn)行地圖操作
- 初始化
// 第一次調(diào)用_MapMgr 會對百度SDK進(jìn)行初始化,可以參見 init 函數(shù) SDK所有的數(shù)據(jù)訪問功能依賴于初始化,所以請在使用前提前構(gòu)造Manager
#define _MapMgr ([ZFMapManager sharedInstance])
// 使用宏定義調(diào)用Manager 用于向百度注冊你的AK 這一步可以在程序啟動時操作.
_MapMgr;
/// 界面顯示需要等待 鏈接上百度SDK 才能刷新位置成功 可以使用延遲,來保證成功獲取定位
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//Demo中獲取定位的方法
[self currentLocAction:nil];
});
- 獲取當(dāng)前城市
/// 當(dāng)前城市 可能為nil 獲取一次后如果為nil會自動刷新當(dāng)前城市位置,并拋出通知.
/// 設(shè)置城市位置信息,如果為nil 會自動通過通知中心來更新
self.currentCityLabel.text = _MapMgr.city;
/// 注冊通知 可以對通知進(jìn)行監(jiān)聽,獲取最新的所在城市信息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mapManagerCityDidChangeNotification:) name:KZFMapManagerCityDidChangeNotification object:nil];
#pragma mark - 接收城市和地區(qū)更新的消息
- (void)mapManagerCityDidChangeNotification:(NSNotification *)noti
{
/// 接收城市位置信息
if (noti.object[ZFMapCityKEY]) {
self.currentCityLabel.text = noti.object[ZFMapCityKEY];
}
if (noti.object[ZFMapDistrictKEY]) {
self.currentDistrict.text = noti.object[ZFMapDistrictKEY];
}
}
- 獲取當(dāng)前定位信息,可以幫助用戶確定一個大致準(zhǔn)確的當(dāng)前地址.
//制作弱引用替身
__weak typeof(self) weakSelf = self;
[_MapMgr nearByPoi:^(NSArray *poi) {
/// 獲取當(dāng)前位置信息
weakSelf.currentLocLabel.text = poi.firstObject[ZFMapNameKEY];
}];
- 進(jìn)入地圖選擇器 直接初始化
ZFAddressSelectorController
/// 獲取地圖選擇器
ZFAddressSelectorController *vc = [ZFAddressSelectorController new];//[[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"ZFAddressSelectorController"];
[self.navigationController pushViewController:vc animated:YES];
- 獲取選擇器選擇的地址
/// 選擇的地點. 這個通知如果不使用 ZFAddressSelectorController類 需要自己手動實現(xiàn)POST部分 他響應(yīng)了Demo中的cell點擊
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mapManagerLocationDidSelectedNotification:) name:KZFMapManagerLocationDidSelectedNotification object:nil];
#pragma mark - 接收選中的地點,并進(jìn)行地區(qū)更新
- (void)mapManagerLocationDidSelectedNotification:(NSNotification *)noti{
/// 設(shè)置當(dāng)前位置信息
self.currentLocLabel.text = noti.object[ZFMapNameKEY];
/// 選中熱點信息后,可以搜索當(dāng)前熱點位置,用于更改城市信息
CGPoint pt = [noti.object[ZFMapPointKEY] CGPointValue];
NSLog(@"X:%f Y:%f",pt.x,pt.y);
CLLocationCoordinate2D point = {pt.y,pt.x};
[_MapMgr searchCityNameWithCoordinate2D:point];
}
- 如何自定義界面
- tableView的樣式定制,可以在
ZFAddressSelectorController
對tb
屬性進(jìn)行定制 - mapView界面中,定位按鈕和選擇按鈕可以在
ZFMapView.m
L:49-70行進(jìn)行樣式定制 - searchBar是tableView的表頭視圖.