- (void)viewDidLoad {
[super viewDidLoad];
if(self.userData.city.length < 2)
{
return;
}
self.mapView.delegate = self;
// CLGeocoder 解析地址
CLGeocoder *geoCoder = [CLGeocoder new];
// 地址解码
[geoCoder geocodeAddressString:self.userData.city completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
self.placemark = placemarks[0];
// 插入大头针
FLAnnotation *ann = [FLAnnotation new];
ann.coordinate = self.placemark.location.coordinate;
ann.title = self.userData.name;
ann.subtitle = self.userData.city;
ann.iconUrl = self.userData.iconUrl;
[self.mapView addAnnotation:ann];
self.mapView.showsUserLocation = YES;
}];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(getUserPosition:)];
// 点击3下触发
tap.numberOfTapsRequired = 3;
[self.mapView addGestureRecognizer:tap];
}
// 获取用户位置
- (void) getUserPosition:(UITapGestureRecognizer *)tap
{
CGPoint pt = [tap locationInView:tap.view];
// CLLocationCoordinate2D 枚举类型, 经纬度
CLLocationCoordinate2D co = [self.mapView convertPoint:pt toCoordinateFromView:self.mapView];
CLGeocoder *geoCoder = [CLGeocoder new];
CLLocation *location = [[CLLocation alloc] initWithLatitude:co.latitude longitude:co.longitude];
[geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark * placemark = placemarks[0];
// 大头贴的信息
MKPointAnnotation * ann = [MKPointAnnotation new];
ann.coordinate = co;
ann.title = [NSString stringWithFormat:@"%@ %@", placemark.country, placemark.administrativeArea];
ann.subtitle = placemark.name;
[self.mapView addAnnotation:ann];
}];
}
// 按照鼠标点击的地方和需要放大的地方的中心点放大
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if(!self.updateUserLocation)
{
// 计算差距
MKCoordinateSpan span = MKCoordinateSpanMake(fabs(userLocation.coordinate.latitude-self.placemark.location.coordinate.latitude)+1, fabs(userLocation.coordinate.longitude-self.placemark.location.coordinate.longitude)+1);
// 中心
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((userLocation.coordinate.latitude+self.placemark.location.coordinate.latitude)/2, (userLocation.coordinate.longitude+self.placemark.location.coordinate.longitude)/2);
// 范围
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[self.mapView setRegion:region animated:YES];
self.updateUserLocation = YES;
}
}
#pragma mark -- 设置大头贴
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[FLAnnotation class]])
{
static NSString *annID = @"FL_ANN_ID";
MKAnnotationView *annVIew = [mapView dequeueReusableAnnotationViewWithIdentifier:annID];
if(!annVIew)
{
annVIew = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annID];
annVIew.canShowCallout = YES;
// 在 地理信息左侧添加图片
UIImageView *leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
annVIew.leftCalloutAccessoryView = leftImage;
UIImageView *rightImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
annVIew.rightCalloutAccessoryView = rightImage;
rightImage.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(navi2:)];
[rightImage addGestureRecognizer:tap];
}
// 给大头针添加左右图片
FLAnnotation *fla = (FLAnnotation *)annotation;
UIImageView *iconImageView = (UIImageView *)annVIew.leftCalloutAccessoryView;
[iconImageView sd_setImageWithURL:[NSURL URLWithString:fla.iconUrl]];
annVIew.image = [UIImage imageNamed:@"icon_marker"];
UIImageView * rightView = (UIImageView *)annVIew.rightCalloutAccessoryView;
objc_setAssociatedObject(rightView, &annStoreKey, fla, OBJC_ASSOCIATION_ASSIGN);
return annVIew;
}
//如果不是则调用系统的
else
{
return nil;
}
}
// 路程导航(从用户位置到目的地)
- (void) navi2:(UITapGestureRecognizer *)gesture
{
FLAnnotation * ann = objc_getAssociatedObject(gesture.view, &annStoreKey);
MKMapItem * fromItem = [MKMapItem mapItemForCurrentLocation];
MKMapItem * toItem = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:ann.coordinate addressDictionary:nil]];
toItem.name = ann.title;
[MKMapItem openMapsWithItems:@[fromItem, toItem]
launchOptions:[NSDictionary dictionaryWithObjects:@[MKLaunchOptionsDirectionsModeDriving, @(YES)]
forKeys:@[MKLaunchOptionsDirectionsModeKey, MKLaunchOptionsShowsTrafficKey]]];
}
iOS-自定制 地图大头针
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 要达到的效果图 一.首先看下地图中最基础的大头针的定制: 二.看下气泡callout的自定制 最后在Viewcon...
- 前言学习地图,我们必须要接触两个框架:Core Location,主要包含定位、地理编码、反编码功能,如需了解请移...
- ViewController.m(声明文件中我没有写代码) //// MyAnnotation.m// Add...