zoukankan      html  css  js  c++  java
  • iOS-Senior20-Map地图

    #import <CoreLocation/CoreLocation.h>

    #import "MyAnimation.h"

    //地图使用的框架是MapKit

    /*大头针

     在iOS开发中经常会标记某个位置,需要使用地图标注,也就是大家俗称的“大头针”。只要一个NSObject类实现MKAnnotation协议就可以作为一个大头针,通常会重写协议中coordinate(标记位置)、title(标题)、subtitle(子标题)三个属性,然后在程序中创建大头针对象并调用addAnnotation:方法添加大头针即可(之所以iOS没有定义一个基类实现这个协议供开发者使用,多数原因应该是MKAnnotation是一个模型对象,对于多数应用模型会稍有不同,例如后面的内容中会给大头针模型对象添加其他属性)。*/

    ####################属性######################

    ##################对象方法######################

    #################代理方法#######################

    ################自定义大头针视图###################

    代码:

    1.先定义一个继承NSObject的类,并在类中重写协议的三个属性:coordinate(标记位置),title(标题),subtitle(子标题)三个属性

    @property (nonatomic) CLLocationCoordinate2D coordinate;

    @property (nonatomic,copy) NSString *title;

    @property (nonatomic,copy) NSString *subtitle;

    //自定义大头针

    //自定义大头针显示图片的

    @property (nonatomic,strong) UIImage *image;

    2.回到viewController里写内容

    //定位管理器

    @property (nonatomic,strong) CLLocationManager *locationManager;

    //显示地图的视图

    @property (nonatomic,strong) MKMapView *mapView;

    - (void) viewDidLoad {

    [self createMapView];

    }

    #pragma mark - 创建视图

    - (void) createMapView {

    self.mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    [self.view addSubview :self.mapView];

    //设置代理

    self.mapView.delegate = self;

    //定位

    self.locationManager = [[CLLocationManager alloc] init];

    if(![CLLocationManager locationSevicesEnabled]) {

    NSLog(@"当前设备定位不可用");

    }

    if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse){

    [self.locationManager requestWhenInUseAuthorization];

    }

    //设置地图的定位追踪

    self.mapView.userTrackingMode = MKUserTrackingModeFollow;

    //设置地图的显示类型

    self.mapView.mapType = MKMapTypeStandard;

    //添加大头针

    [self addAnnotation];

    }

    #pragma mark - 添加大头针

    - (void) addAnnotation {

    //设置位置

    CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(40,116);

    //大头针-北京

    MyAnimation *annotation = [[MyAnimation alloc] init];

    annotation.coordinate = location1;

    annotation.title = @"Beijing";

    annotation.subtitle = @"Anana~s home";

    //添加image图片

    annotation.image = [UIImage imageNamed:@"1.jpg"];

    [self.mapView addAnnotation : annotation];

    }

    #pragma mark - 代理方法

    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

        NSLog(@"%@",userLocation);

    }

    #pragma mark - 实现自定义大头针视图的代理方法

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

    //判断是否是当前自定义的大头针类

    if([annotation isKindOfClass :[MyAnimation class]]){

    //先定义一个重用标识

    static NSString *identifier = @"AnnotationOne";

    MKAnnotationView *annotationView = [self.mapView dequeueReusableAnnotation ViewWithIdentifier : identifier];

    if(!annotationView){

    annotationView = [[MKAnnotationView alloc] initWithAnnotation : annotation resueIdentifier : identifier];

    //允许用户交互

    annotationView.canShowCallout = YES;

    //设置详情和大头针的头偏移量

    annotationView.calloutOffset = CGPointMake(0,1);

    //设置详情的左视图

    annotationView.leftCalloutAccessoryView = [[UIImageView alloc]initWithImage:[UIImage imageName:@"1.jpg"]];

    }

    //修改大头针视图

    annotationView.annotation = annotation;

    annotationView.image = ((MyAnimationg *)annotation).image;

    return annotationVie;

    }else {

    return nil;

    }

    }

     

  • 相关阅读:
    前端-微信公众号开发(jssdk)
    wampserver 配置本地环境局域网内pc移动访问
    客户端缓存之localStorage and sessionStorage
    webpack+vue搭建基础
    高德地图javascriptAPI基本使用心得(下)
    高德地图javascriptAPI基本使用心得(上)
    表单单选多选项清除默认样式小技巧
    (原创)C#零基础学习笔记003-流程控制语句
    (原创)C#零基础学习笔记002-表达式与运算符
    (原创)C#零基础学习笔记000-学习结构
  • 原文地址:https://www.cnblogs.com/zhoulina/p/5547506.html
Copyright © 2011-2022 走看看