标签:
地图:MapKit和CoreLocation
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
添加显示地图的MKMapView控件 :
地图的类型 :
如何添加大头针(地标):
@interface MyAnnotation : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
<2>添加Annotation:
MyAnnotation *anno = [[MyAnnotation alloc] init];
anno.title = @“中国";
anno.subtitle = @“北京”;
//经度和纬度
anno.coordinate = CLLocationCoordinate2DMake(40, 110);
//添加大头针到地图中
[_mapView addAnnotation:anno];
// 让地图挪动到对应的位置(经纬度交叉处)
[_mapView setCenterCoordinate:anno.coordinate animated:YES];
自定义大头针:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *ID = @"anno";
MKPinAnnotationView *annoView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
if (annoView == nil) {
annoView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
// 显示气泡
annoView.canShowCallout = YES;
// 设置绿色
annoView.pinColor = MKPinAnnotationColorGreen;
}
return annoView;
}
// 定位管理器
_mgr = [[CLLocationManager alloc] init];
// 获取授权
[_mgr requestAlwaysAuthorization];
// 设置代理
_mgr.delegate = self;
// 设置精度
_mgr.desiredAccuracy = kCLLocationAccuracyBest;
// 开始获取用户的位置
[_mgr startUpdatingLocation];
定位管理器的代理方法:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations•
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
通过地址获得经纬度 :
CLGeocoder:地理信息编解码
_geocoder = [[CLGeocoder alloc] init];
[_geocoder geocodeAddressString:@“东三旗" completionHandler:^(NSArray *placemarks, NSError *error) {
//没有找到符合要求的地址
if (placemarks.count == 0) return;
// 取出位置
CLPlacemark *firstPlacemark = placemarks[0];
// 添加大头针
MyAnnotation *anno = [[MyAnnotation alloc] init];
anno.title = firstPlacemark.name; // 名称
anno.subtitle = firstPlacemark.country; // 国家
anno.coordinate = firstPlacemark.location.coordinate; // 坐标
[_mapView addAnnotation:anno];
}];
标签:
原文地址:http://www.cnblogs.com/XYQ-208910/p/4893027.html