791
社区成员




//申请定位权限
- (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
- (void)initSearch
{
//构造AMapSearchAPI
_search = [[AMapSearchAPI alloc] initWithSearchKey:APIKey Delegate:self];
_search.language = AMapSearchLanguage_en;//设置语言
}
/* 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];
}
}
/* 非高德转坐标转高德坐标 */
- (void) convert
{
NSError *error;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://restapi.amap.com/v3/assistant/coordinate/convert?key=897210d6f68d6a63cfc84f86d96ec5e3&coordsys=gps&locations=%f,%f",_currentLocation.coordinate.longitude,_currentLocation.coordinate.latitude]]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *coordinate = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSString *status = [coordinate objectForKey:@"status"];
NSString *locations = [coordinate objectForKey:@"locations"];
if([status isEqualToString:@"1"]&&![locations isEqualToString:@""])
{
NSArray *array = [locations componentsSeparatedByString:@","];
_convertLocation = [[CLLocation alloc] initWithLatitude:[array[1] doubleValue] longitude:[array[0] doubleValue]];
}
}