iOS-自定制 地图大头针

- (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 * _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 * _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)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-自定制 地图大头针)