如何用高德LBS开放平台开发全英文iOS 地图应用

如题所述

第一步:申请Key,用于搜索SDK。
提示:MapKit是不需要Key的,该key仅用于高德的iOS SDK搜索功能。
第二步:初始化MKMapView ,并添加到Subview。同时,进行定位,设置定位模式,在地图上显示定位点。

注意:
(1)MapKit中的定位(showUserLocation= YES),在回调中获取的坐标不用进行坐标偏转;若使用CLLocationManager方法进行定位,需要进行坐标偏转(参考附加内容)。
(2)MapKit没有申请定位权限,需在代码中申请一下定位权限。
申请方法:在 info.plist中追加NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription字段。其中:
NSLocationWhenInUseUsageDescription表示应用在前台的时候可以搜到更新的位置信息。
NSLocationAlwaysUsageDescription表示应用在前台和后台(suspend或terminated)都可以获取到更新的位置数据。
代码如下:

//申请定位权限
- (void) initLocation
{
if(nil == _locationManager)
{
_locationManager = [[CLLocationManager alloc] init];

}

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[_locationManager requestAlwaysAuthorization];
}
}

//初始化MapView
- (void) initMapView{

//构造MKMapView
_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 21, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
_mapView.delegate = self;

_mapView.showsUserLocation = YES;//显示定位图标

[_mapView setUserTrackingMode:MKUserTrackingModeFollow];//设置定位模式

//将mapview添加到Subview中
[self.view addSubview:_mapView];

}

第三步:初始化主搜索对象AMapSearchAPI

构造AMapSearchAPI对象,并设置搜索结果语言。(支持英文结果的搜索功能包括:POI搜索、逆地理编码和地理编码、输入提示,能够满足基本的搜索功能)

//初始化AMapSearchAPI
- (void)initSearch
{
//构造AMapSearchAPI
_search = [[AMapSearchAPI alloc] initWithSearchKey:APIKey Delegate:self];
_search.language = AMapSearchLanguage_en;//设置语言
}

第四步:构造搜索对象,设置搜索参数,发起查询,在相应的回调中进行结果展示(如:POI查询结果以大头针标注等等)。

以Demo中的POI查询为例,以选择的输入提示语为关键字

/* POI 搜索. */
- (void)searchPOIWithKey:(NSString *)key adcode:(NSString *)adcode
{
if (key.length == 0)
{
return;
}
//构造POI搜索对象AMapPlaceSearchRequest
AMapPlaceSearchRequest *place = [[AMapPlaceSearchRequest alloc] init];
//设置关键字、
place.keywords = key;
place.requireExtension = YES;//设置成YES,返回信息详细,较费流量
if (adcode.length > 0)
{
place.city = @[adcode];
}
//发起查询
[_search AMapPlaceSearch:place];
}

//回调中显示结果
- (void)onPlaceSearchDone:(AMapPlaceSearchRequest *)request response:(AMapPlaceSearchResponse *)respons
{
if (respons.pois.count == 0)
{
return;
}

NSMutableArray *poiAnnotations = [NSMutableArray arrayWithCapacity:respons.pois.count];

[respons.pois enumerateObjectsUsingBlock:^(AMapPOI *obj, NSUInteger idx, BOOL *stop) {

[poiAnnotations addObject:[[POIAnnotation alloc] initWithPOI:obj]];

}];

/* 将结果以annotation的形式加载到地图上. */
[_mapView addAnnotations:poiAnnotations];

/* 如果只有一个结果,设置其为中心点. */
if (poiAnnotations.count == 1)
{
_mapView.centerCoordinate = [poiAnnotations[0] coordinate];
}
/* 如果有多个结果, 设置地图使所有的annotation都可见. */
else
{
[_mapView showAnnotations:poiAnnotations animated:NO];
}
}
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网