iOS:地图:MapKit和CoreLocation
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了iOS:地图:MapKit和CoreLocation,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3311字,纯文字阅读大概需要5分钟。
内容图文
地图: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
内容总结
以上是互联网集市为您收集整理的iOS:地图:MapKit和CoreLocation全部内容,希望文章能够帮你解决iOS:地图:MapKit和CoreLocation所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。