上次只是大概说了一下显示地图的几种方法,以及如何设置地图的一些属性,今天跟大家分享的事如何在地图上进行位置注解以及进行位置管理。
// 完成位置反编码
CLGeocoder *geocoder = [[CLGeocoder alloc ]init];
CLLocation *location = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if (nil != error) {
NSLog(@"error info is %@",error);
}
for (CLPlacemark *placeMark in placemarks) {
NSLog(@"%@,%@,%@,%@,%@,%@,%@,%@,%@",placeMark.country,placeMark.areasOfInterest,placeMark.ocean,placeMark.ISOcountryCode,placeMark.inlandWater,placeMark.locality,placeMark.postalCode,placeMark.administrativeArea,placeMark.subLocality);
}
}];
显示位置注解:这里用硬编码方式示例
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
pointAnnotation.coordinate = coordinate;
pointAnnotation.title = @"你好";
pointAnnotation.subtitle = @"世界";
[self.mapView addAnnotation:pointAnnotation];
使用
MKMapViewDelegate来实现地图的位置信息以及大头针的显示
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *indentify = @"indentify";
MKPinAnnotationView *pinAnnotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:indentify];
if (nil == pinAnnotationView) {
pinAnnotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:indentify];
}
pinAnnotationView.image = [UIImage imageNamed:@"icon_nav_start"];
UIImageView *imageVew = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
imageVew.image = [UIImage imageNamed:@"Icon"];
pinAnnotationView.leftCalloutAccessoryView = imageVew;
pinAnnotationView.animatesDrop = YES;
pinAnnotationView.pinColor = MKPinAnnotationColorPurple;
pinAnnotationView.canShowCallout = YES;
return pinAnnotationView;
}
当然在这之前还是要遵循CLLocationManagerDelegate,MKMapViewDelegate协议的
设置好后,运行即可看到大头针的位置显示视图了。