美文网首页Ios@IONIC
苹果地图自定义大头针 详情视图

苹果地图自定义大头针 详情视图

作者: mayday2024 | 来源:发表于2015-05-31 18:32 被阅读584次

    自定一个继承 MKAnnotation
    #import <Foundation/Foundation.h>
    #import <MapKit/MapKit.h>

            @interface CZAnnotation : NSObject<MKAnnotation>
            /**
             *  标记经纬度
             */
            @property (nonatomic) CLLocationCoordinate2D coordinate;
    
            /**
             *  标记标题
             */
            @property (nonatomic, copy) NSString *title;
    
            /**
             *  标记子标题
             */
            @property (nonatomic, copy) NSString *subtitle;
    

    主文件
    #import "ViewController.h"
    //自定一个
    #import "CZAnnotation.h"
    #import <MapKit/MapKit.h>

            @interface ViewController ()<MKMapViewDelegate>
            @property (weak, nonatomic) IBOutlet MKMapView *mapView;
    
            @end
    
            @implementation ViewController
    
            - (void)viewDidLoad {
                [super viewDidLoad];
                // Do any additional setup after loading the view, typically from a nib.
                
                // 地图类型
                self.mapView.mapType = MKMapTypeStandard;
                
                // 用户位置跟踪模式
                self.mapView.userTrackingMode = MKUserTrackingModeFollow;
                
                // 设置mapView代理
                self.mapView.delegate = self;
                
                // 在地图上添加一个 "标记模型"
                CZAnnotation *anno = [[CZAnnotation alloc] init];
                anno.coordinate = CLLocationCoordinate2DMake(39.539, 116.260);
                anno.title = @"广州";
                anno.subtitle = @"天河xxxxxx";
                [self.mapView addAnnotation:anno];
                
                
    
            }
    
    
            #pragma mark 定位到当前用户位置
            -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
                // 1.当前位置详细描述
                userLocation.title = @"广州";
                userLocation.subtitle = @"天河";
            #warning 自己测试
                //当前的位置详细描述,要显示哪个城市,哪个区-(反地理编码)
                
                
                // 2.设置显示的region
                //MKCoordinateSpan span = MKCoordinateSpanMake(0.193626, 0.145513);
                MKCoordinateSpan span = MKCoordinateSpanMake(1.00, 1.0);
                MKCoordinateRegion region = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, span);
                self.mapView.region = region;
            #pragma mark 在此方法, 动画效果不起作用,其它方法方法可以
                //[self.mapView setRegion:region animated:YES];
    
                
    
            }
    
    
    
            #pragma 返回当前位置
            - (IBAction)backCurrentLocation{
            }
    
    
            #pragma mark 添加 "标记视图"
            -(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
    
                NSLog(@"%@",views);
            }
    
      //点哪里添哪里
            -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
                UITouch *touch = [touches anyObject];
                
                //获取点击的位置
                CGPoint touchP = [touch locationInView:touch.view];
                NSLog(@"%@",NSStringFromCGPoint(touchP));
                
                //把点击的位置转成经纬度
                CLLocationCoordinate2D coordinate = [self.mapView convertPoint:touchP toCoordinateFromView:touch.view];
                
                //在地图上添加当前触摸位置 "标记模型"
                CZAnnotation *anno = [[CZAnnotation alloc] init];
                anno.coordinate = coordinate;
                [self.mapView addAnnotation:anno];
                
            }
    
            @end
    

    //
    自定义详情 View

            #import "ViewController.h"
            #import "CZAnnotation.h"
            #import <MapKit/MapKit.h>
    
            @interface ViewController ()<MKMapViewDelegate>
            @property (weak, nonatomic) IBOutlet MKMapView *mapView;
    
            @end
    
            @implementation ViewController
    
            - (void)viewDidLoad {
                [super viewDidLoad];
                // Do any additional setup after loading the view, typically from a nib.
                
                // 地图类型
                self.mapView.mapType = MKMapTypeStandard;
                
                // 用户位置跟踪模式
                self.mapView.userTrackingMode = MKUserTrackingModeFollow;
                
                // 设置mapView代理
                self.mapView.delegate = self;
                
            }
    
            /**
             *  添加自定义 ”标记模型“
             */
            -(IBAction)addCustomAnno{
                // 在地图上添加一个 "标记模型"
                CZAnnotation *anno = [[CZAnnotation alloc] init];
                anno.coordinate = CLLocationCoordinate2DMake(39.539, 116.260);
                anno.title = @"广州";
                anno.subtitle = @"天河xxxxxx";
                [self.mapView addAnnotation:anno];
            }
    
            #pragma mark 定位到当前用户位置
            -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
                // 1.当前位置详细描述
                userLocation.title = @"广州";
                userLocation.subtitle = @"天河";
            #warning 自己测试
                //当前的位置详细描述,要显示哪个城市,哪个区-(反地理编码)
                
                
                // 2.设置显示的region
                //MKCoordinateSpan span = MKCoordinateSpanMake(0.193626, 0.145513);
                MKCoordinateSpan span = MKCoordinateSpanMake(1.00, 1.0);
                MKCoordinateRegion region = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, span);
                self.mapView.region = region;
            #pragma mark 在此方法, 动画效果不起作用,其它方法方法可以
                //[self.mapView setRegion:region animated:YES];
            }
    
    
            #pragma mark 在地图上显示 "标记视图" 会调用此方法
            -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
                NSLog(@"%@",annotation);
                
                // 自定义的 "标记模型"
                if ([annotation isKindOfClass:[CZAnnotation class]]) {
                    static NSString *annoViewID = @"CZAnnotationView";
                    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annoViewID];
                    
                    //MKAnnotationView *pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annoViewID];
                    /*
                    MKPinAnnotationColorRed = 0,
                    MKPinAnnotationColorGreen,
                    MKPinAnnotationColorPurple*/
                    //  设置 "标记视图" 的颜色
                    pinView.pinColor = MKPinAnnotationColorPurple;
                    
                    // ”标记视图“ 由天而降
                    //pinView.animatesDrop = YES;
                    
                    // "标记视图" 的图片
            #warning 设置从天而降(animatesDrop) 自定义图片就无效
                    pinView.image = [UIImage imageNamed:@"pin_green"];
                    
                    
                    // 显示 标记视图 - 详情
                    pinView.canShowCallout = YES;
                    
                    // callout 往上走,y值设置负数
                    pinView.calloutOffset = CGPointMake(0, -5);
                    
                    // 详情视图 左边的View
                    UIImageView *imgView = [[UIImageView alloc] init];
                    imgView.image = [UIImage imageNamed:@"gzlib.jpg"];
                    imgView.bounds = CGRectMake(0, 0, 40, 40);
                    pinView.leftCalloutAccessoryView = imgView;
                    
                    
                    // 详情视图 右边的View
                    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
                    pinView.rightCalloutAccessoryView = btn;
                    
                    return pinView;
                }
                // 返回一个空,标记模型 对应 标记视图 由系统决定
                return nil;
            }
    
    
            #pragma 返回当前位置
            - (IBAction)backCurrentLocation{
            }
    
    
            #pragma mark 添加 "标记视图"
            -(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
    
                NSLog(@"%@",views);
            }
    
            @end
    

    相关文章

      网友评论

        本文标题:苹果地图自定义大头针 详情视图

        本文链接:https://www.haomeiwen.com/subject/tsfdqttx.html