高德地图的使用
高德地圖的使用
1.庫的配置
/***************高德地圖***************
1.庫的配置 --> 導入文件,添加頭文件
libstdc++.6.0.9
QuartzCore
CoreLocation
SystemCOnfiguration
libz
OpenGLES
CoreTelePhony
Security
/*需要在Other Link Flags添加-Objc,
靜態庫中實現了一些類別,會與系統有出入,所以需要標記-Objc
*/
#import "ViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>
@interface ViewController ()<MAMapViewDelegate,CLLocationManagerDelegate,UIGestureRecognizerDelegate,UISearchBarDelegate,AMapSearchDelegate>
{
CLLocationManager *_manager;
MAMapView *_mapView;
AMapSearchAPI *_search;
}
@end
2.初始化
#import "AppDelegate.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h> //
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[MAMapServices sharedServices].apiKey = @"db7d1a8f64d847ff49620ff13eed023c"; //初始化
// 注: INVALID User Scode
--> 原因: 申請的key太新 ,10分鐘內申請
key 與應用bundle Id 不一致
*/
3.創建和顯示地圖
#pragma mark - 請求定位服務
-(void)requestAccessLocation{
if(![CLLocationManager locationServicesEnabled]){
NSLog(@"定位服務不可用");
return;
}
_manager = [[CLLocationManager alloc] init];
_manager.delegate = self;
//此方法若沒有 返回,直接授權
[_manager requestAlwaysAuthorization]; //
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
NSLog(@"status = %d",status);
}
#pragma mark - 創建和顯示地圖
-(void)createAndShowMap{
_mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
_mapView.delegate = self;
[self.view addSubview:_mapView];
//常用設置
_mapView.mapType = MAMapTypeStandard;
_mapView.userTrackingMode = MAUserTrackingModeFollowWithHeading; //設置用戶追蹤模式
_mapView.zoomLevel = 16; //縮放級別
NSLog(@"%f %f",_mapView.maxZoomLevel,_mapView.minZoomLevel); //
}
4.地圖標記(長按添加大頭針)
//4.地圖標記(大頭針)--> 長按 加
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dealLongPress:)];
longPress.delegate = self; //version 不同 需加上
[_mapView addGestureRecognizer:longPress];
//添加searchBar 實現搜索功能
_search = [[AMapSearchAPI alloc] initWithSearchKey:@"db7d1a8f64d847ff49620ff13eed023c" Delegate:self]; //搜索服務
#pragma mark - 地圖標記
-(void)dealLongPress:(UILongPressGestureRecognizer *)longPress{
if(longPress.state == UIGestureRecognizerStateBegan){
//獲取長按點
CGPoint point = [longPress locationInView:_mapView];
CLLocationCoordinate2D coordinate = [_mapView convertPoint:point toCoordinateFromView:_mapView];
//添加大頭針
MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
annotation.coordinate = coordinate;
annotation.title = @"點擊位置";
annotation.subtitle = [NSString stringWithFormat:@"%.3f,%.3f",annotation.coordinate.longitude,annotation.coordinate.latitude];
NSLog(@"s = %@",annotation.subtitle);
[_mapView addAnnotation:annotation];
}
}
//顯示大頭針的詳細信息
-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
static NSString *annotationId = @"annotation";
MAPinAnnotationView *pin = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationId];
if(!pin){
pin = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationId];
}
pin.canShowCallout = YES; //
pin.pinColor = MAPinAnnotationColorGreen;
return pin;
}
注:iOS8.0需加此方法
#pragma mark - 長按顯示大頭針(iOS8.0以上需加)
//UIGestureRecognizerDelegate --> 加長按顯示大頭針
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
5.區域搜索 ---> 高德地圖官網
//添加searchBar 實現搜索功能
_search = [[AMapSearchAPI alloc] initWithSearchKey:@"db7d1a8f64d847ff49620ff13eed023c" Delegate:self]; //搜索服務
/****高德地圖官網 -- 開發者文檔(POI)****/
//5.區域搜索
CGSize size = [UIScreen mainScreen].bounds.size;
UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, size.height, 40)];
bar.delegate = self; //
[self.view addSubview:bar];
#pragma mark - 實現周邊的搜索
-(void)aroundSearchWithKey:(NSString *)key{
//...........
AMapPlaceSearchRequest *poiRequest = [[AMapPlaceSearchRequest alloc] init];
poiRequest.searchType = AMapSearchType_PlaceAround;
poiRequest.keywords = key;
poiRequest.city = @[@"guangzhou"];
poiRequest.location = [AMapGeoPoint locationWithLatitude:_mapView.centerCoordinate.latitude longitude:_mapView.centerCoordinate.longitude]; //
poiRequest.radius = 500; //
poiRequest.requireExtension = YES;
//發起POI搜索
[_search AMapPlaceSearch: poiRequest];
}
//實現POI搜索對應的回調函數
- (void)onPlaceSearchDone:(AMapPlaceSearchRequest *)request response:(AMapPlaceSearchResponse *)response
{
if(response.pois.count == 0)
{
return;
}
NSLog(@"搜索個數: %ld",response.count);
for(AMapPOI *p in response.pois){
NSLog(@"name = %@, long = %f, lat = %f",p.name,p.location.longitude,p.location.latitude);
//添加大頭針
MAPointAnnotation *point = [[MAPointAnnotation alloc] init];
point.coordinate = CLLocationCoordinate2DMake(p.location.latitude, p.location.longitude);
point.title = p.name;
point.subtitle = [NSString stringWithFormat:@"經度long = %f,緯度lat = %f",p.location.longitude,p.location.latitude];
[_mapView addAnnotation:point];
}
//原有的代碼
/*
//通過AMapPlaceSearchResponse對象處理搜索結果
NSString *strCount = [NSString stringWithFormat:@"count: %d",response.count];
NSString *strSuggestion = [NSString stringWithFormat:@"Suggestion: %@", response.suggestion];
NSString *strPoi = @"";
for (AMapPOI *p in response.pois) {
strPoi = [NSString stringWithFormat:@"%@
POI: %@", strPoi, p.description];
}
NSString *result = [NSString stringWithFormat:@"%@
%@
%@", strCount, strSuggestion, strPoi];
NSLog(@"Place: %@", result);
*/
}
6.路徑規劃
//UISearchBarDelegate
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
NSLog(@"search key = %@",searchBar.text); //
// [self aroundSearchWithKey:searchBar.text]; //周邊的搜索
#pragma mark 路徑搜索
//構造AMapNavigationSearchRequest對象,配置查詢參數
AMapNavigationSearchRequest *naviRequest= [[AMapNavigationSearchRequest alloc] init];
naviRequest.searchType = AMapSearchType_NaviDrive;
naviRequest.requireExtension = YES;
naviRequest.origin = [AMapGeoPoint locationWithLatitude:23.174659 longitude:113.341038];
naviRequest.destination = [AMapGeoPoint locationWithLatitude:23.174659 longitude:113.341038];
//發起路徑搜索
[_search AMapNavigationSearch: naviRequest];
}
//實現路徑搜索的回調函數
- (void)onNavigationSearchDone:(AMapNavigationSearchRequest *)request response:(AMapNavigationSearchResponse *)response
{
if(response.route == nil)
{
return;
}
//通過AMapNavigationSearchResponse對象處理搜索結果
NSString *route = [NSString stringWithFormat:@"Navi: %@", response.route];
NSLog(@"%@", route);
}
7.其他
總結
- 上一篇: SAP CDS view如何取得当前系统
- 下一篇: 搭建GitHub 自动镜像至 Gitee