zoukankan      html  css  js  c++  java
  • 地图简单事例代码

    一:百度地图

    1.按照API文档导入相应的库

    2.AppDelegate文件

    #import "BMapKit.h"
    
    BMKMapManager* _mapManager;
    
        _mapManager = [[BMKMapManager alloc]init];
        BOOL ret = [_mapManager start:@"please enter your key" generalDelegate:self];
        
        if (!ret) {
            NSLog(@"manager start failed!");
        }

    3.对应的viewController文件

    #import "MapViewController.h"
    #import "BMapKit.h"
    #import "MyAnontation.h"
    
    @interface MapViewController ()<BMKMapViewDelegate>
    {
        BMKMapView *_mapView;
    
    }
    @end
    
    @implementation MapViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self createBackBtn];
        self.view.backgroundColor = [UIColor whiteColor];
        _mapView=[[BMKMapView alloc]initWithFrame:self.view.bounds];
        [self.view addSubview:_mapView];
        [_mapView viewWillAppear];
        
        _mapView.centerCoordinate=CLLocationCoordinate2DMake(_cinemaModel.latitude.doubleValue, _cinemaModel.longitude.doubleValue);
        _mapView.delegate=self;
        _mapView.zoomLevel=14;
        
        //BMKPointAnnotation *annotation=[[BMKPointAnnotation alloc]init];
        //annotation.coordinate=_mapView.centerCoordinate;
        //[_mapView addAnnotation:annotation];
        
        CLLocationCoordinate2D coord;
        coord.longitude=[_cinemaModel.longitude doubleValue];
        coord.latitude=[_cinemaModel.latitude doubleValue];
        
        //自定义一个大头针
        MyAnontation *anontation=[[MyAnontation alloc] initWithCoor:coord title:_cinemaModel.name subTitle:_cinemaModel.addr];
        //大头针的编号
        //anontation.anonIndex=i;
        //添加到地图上(使用添加大头针的方法,不是添加控件)
        [_mapView addAnnotation:anontation];
    }
    
    #pragma mark --BMKMapViewDelegate代理----
    - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
    {
        BMKAnnotationView *view=[[BMKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"annotation"];
        view.image=[UIImage imageNamed:@"mark"];
        return view;
    }

     二:高德地图

    1.AppDelegate设置key值(高德后台注册)

    #import <MAMapKit/MAMapKit.h>

    [MAMapServices sharedServices].apiKey = (NSString *)key;

    2.对应的viewController文件

    #import "MAMapViewController.h"
    #import <MAMapKit/MAMapKit.h>
    
    @interface MAMapViewController ()<MAMapViewDelegate>
    {
        MAMapView *_mapView;
    }
    @end
    
    @implementation MAMapViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self createBackBtn];
        self.view.backgroundColor = [UIColor whiteColor];
        _mapView=[[MAMapView alloc]initWithFrame:self.view.bounds];
        _mapView.delegate  =self;
        _mapView.centerCoordinate = CLLocationCoordinate2DMake( _cinemaModel.latitude.doubleValue, _cinemaModel.longitude.doubleValue);
        //_mapView.showsUserLocation = YES;
        //[_mapView setUserTrackingMode:MAUserTrackingModeFollow];
        [self.view addSubview:_mapView];
        
        MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
        annotation.coordinate = CLLocationCoordinate2DMake(_cinemaModel.latitude.doubleValue, _cinemaModel.longitude.doubleValue);
        //NSLog(@"%f---%f",_cinemaModel.latitude.doubleValue, _cinemaModel.longitude.doubleValue);
        annotation.title = _cinemaModel.name;
        annotation.subtitle = _cinemaModel.addr;
        [_mapView addAnnotation:annotation];
    }
    
    #pragma mark --MAMapViewDelegate代理----
    -(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
    {
        static NSString *annoId = @"PointAnnotation";
        MAAnnotationView *view = [mapView dequeueReusableAnnotationViewWithIdentifier:annoId];
        if (!view) {
            view = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annoId];
            view.draggable = YES;
            view.canShowCallout = YES;
            view.image = [UIImage imageNamed:@"mark"];
        }
        return view;
    }

    三:自定制大头针

    1.头文件

    #import <Foundation/Foundation.h>
    #import <MapKit/MapKit.h>
    #import "BMapKit.h"
    
    //自定义大头针(必须具备系统大头针的属性)
    @interface MyAnontation : NSObject <BMKAnnotation>
    
    //大头针的编号
    @property (nonatomic,assign) NSInteger anonIndex;
    
    
    //重新实现一个初始化方法
    -(instancetype) initWithCoor:(CLLocationCoordinate2D ) coor title:(NSString *) title subTitle:(NSString *) subtitle;
    
    @end

    2.对应的.m文件

    #import "MyAnontation.h"
    
    @implementation MyAnontation
    {
        //经纬度
        CLLocationCoordinate2D _coor;
        
        //主标题
        NSString *_title;
        
        //副标题
        NSString *_subTitle;
    }
    -(instancetype) initWithCoor:(CLLocationCoordinate2D)coor title:(NSString *)title subTitle:(NSString *)subtitle
    {
        self=[super init];
        if (self)
        {
            _coor=coor;
            _title=title;
            _subTitle=subtitle;
        }
        return self;
    }
    
    #pragma mark-MKAnontation
    
    -(CLLocationCoordinate2D) coordinate
    {
        return _coor;
    }
    
    -(NSString *) title
    {
        return _title;
    }
    
    -(NSString *) subtitle
    {
        return _subTitle;
    }
    @end
  • 相关阅读:
    如何优化多个关键字
    如何优化中小型企业网站
    SEO内容为王之如何创造伪原创
    中央电化教育馆教学资源库介绍
    教育网络游戏《学乐吧》介绍
    教育技术学专业主干课程系列教材(共八本)
    百度,google对网站首页内页权重分配的区别
    网站内链对网站排名有那些作用和影响?
    网站好排名,页面内链少不了
    公务员考试与事业单位考试
  • 原文地址:https://www.cnblogs.com/liaods/p/4805342.html
Copyright © 2011-2022 走看看