• iOS开发——高级技术&地图功能的实现


    地图功能的实现

    因为有个项目要在地图中显示位置,所以用到了MapKit。

    记录下来,以免以后忘记。

    加入MapKit library

    首先得在项目中加入MapKit,如图

    MapView

    先增加一个ViewController,我这里用的storyboard,这个玩意还是挺好用的,比以前用xib好多了。

    然后拖一个mapview上去,如:

    给新增加的ViewController绑定一个class。首先得增加一个class,从uiViewController继承下来。这个很简单,如图

    把新增加的ViewController绑定到这个class,也很easy,发现Xcode还是挺牛的。就是在右边Identity inspector里面的custom class里面改成新增加的类,原来是UIViewController。

    然后给map view控件绑定一个变量,类型是MKMapView

    然后就初始化mapview,显示。代码如下:

    复制代码
     1 - (void)viewDidLoad
     2 {
     3     [super viewDidLoad];
     4     // Do any additional setup after loading the view.
     5     
     6     _mapView.mapType = MKMapTypeStandard;//标准模式
     7     _mapView.showsUserLocation = YES;//显示自己
     8 
     9     _mapView.zoomEnabled = YES;//支持缩放
    10     
    11     
    12     CLLocationCoordinate2D pos = {39.931203, 116.395573};//找个坐标,我是用百度坐标抓取弄的。http://api.map.baidu.com/lbsapi/getpoint/
    13     
    14     MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos为中心,显示2000米
    15     MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//适配map view的尺寸
    16     [_mapView setRegion:adjustedRegion animated:YES];
    17     
    18 
    19 }
    复制代码

    我这里使用百度坐标,找了个坐标(直接搜索“百度 坐标”),然后在我们自己的地图里显示。这样运行一下就可以看到:

    Map view delegate 回调

    可以实现协议MKMapViewDelegate, 这样就会有几个回调。

    复制代码
     1 - (void) mapViewWillStartLoadingMap:(MKMapView *)mapView//开始从服务器获取地图数据
     2 {
     3    
     4 }
     5 
     6 -(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView//获取数据结束
     7 {
     8     
     9 }
    10 
    11 - (void) mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error//获取数据失败了。
    12 {
    13     
    14 }
    复制代码

    获取设备当前位置并且在地图中显示

    增加一个按钮,点击这个按钮,将显示设备当前位置。点击上面的按钮将显示某个固定位置。

    CLLocationManager,首先使用CLLocationManager来获取设备的当前位置。

    代码也是很简单

     

    复制代码
     1 //获得自己的当前的位置信息
     2 - (void) getCurPosition
     3 {
     4     //开始探测自己的位置
     5     if (locationManager==nil)
     6     {
     7         locationManager =[[CLLocationManager alloc] init];
     8     }
     9     
    10     
    11     if ([CLLocationManager locationServicesEnabled])
    12     {
    13         locationManager.delegate=self;
    14         locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    15         locationManager.distanceFilter=10.0f;
    16         [locationManager startUpdatingLocation];
    17     }
    18 }
    复制代码

    然后实现回调函数

     

    复制代码
     1 #pragma mark -- CLLocationManagerDelegate
     2 - (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
     3 {
     4     if ([locations count] > 0) {
     5         CLLocation* loc = [locations objectAtIndex:0];
     6         CLLocationCoordinate2D pos = [loc coordinate];
     7         
     8         NSLog(@"locationManager, longitude: %f, latitude: %f", pos.longitude, pos.latitude);
     9         
    10         if (show == NO) {
    11             MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos为中心,显示2000米
    12             MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//适配map view的尺寸
    13             [_mapView setRegion:adjustedRegion animated:YES];
    14 
    15             show = YES;
    16         }
    17     }
    18 }
    复制代码

    当设备位置变化时,这个函数会被调用。这样我们就可以根据位置来做一些事情了。这个例子里就在第一次获取位置的时候更新一下地图显示。以设备当前位置为中心,显示2000米。

     

    完了。贴一下mapview所在的controller代码:

     

    复制代码
      1 //
      2 //  KMapViewController.m
      3 //  MapDemo
      4 //
      5 //  Created by Kevin on 14-2-10.
      6 //  Copyright (c) 2014年 Kevin. All rights reserved.
      7 //
      8 
      9 #import "KMapViewController.h"
     10 
     11 @interface KMapViewController ()
     12 
     13 @end
     14 
     15 @implementation KMapViewController
     16 
     17 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
     18 {
     19     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     20     if (self) {
     21         // Custom initialization
     22     }
     23     return self;
     24 }
     25 
     26 - (void)viewDidLoad
     27 {
     28     [super viewDidLoad];
     29     // Do any additional setup after loading the view.
     30     
     31     show = NO;
     32     
     33     _mapView.mapType = MKMapTypeStandard;//标准模式
     34     _mapView.showsUserLocation = YES;//显示自己
     35     _mapView.delegate = self;
     36     _mapView.zoomEnabled = YES;//支持缩放
     37     
     38     
     39     NSString* i = self.Index;
     40     
     41     if([i isEqualToString:@"1"])
     42     {
     43         CLLocationCoordinate2D pos = {39.931203, 116.395573};//找个坐标,我是用百度坐标抓取弄的。http://api.map.baidu.com/lbsapi/getpoint/
     44         
     45         MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos为中心,显示2000米
     46         MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//适配map view的尺寸
     47         [_mapView setRegion:adjustedRegion animated:YES];
     48 
     49     }
     50     else
     51     {
     52         [self getCurPosition];
     53     }
     54 
     55 }
     56 
     57 - (void)didReceiveMemoryWarning
     58 {
     59     [super didReceiveMemoryWarning];
     60     // Dispose of any resources that can be recreated.
     61 }
     62 
     63 - (void) dealloc
     64 {
     65     
     66  //   [super dealloc];
     67 }
     68 
     69 //获得自己的当前的位置信息
     70 - (void) getCurPosition
     71 {
     72     //开始探测自己的位置
     73     if (locationManager==nil)
     74     {
     75         locationManager =[[CLLocationManager alloc] init];
     76     }
     77     
     78     
     79     if ([CLLocationManager locationServicesEnabled])
     80     {
     81         locationManager.delegate=self;
     82         locationManager.desiredAccuracy=kCLLocationAccuracyBest;
     83         locationManager.distanceFilter=10.0f;
     84         [locationManager startUpdatingLocation];
     85     }
     86 }
     87 
     88 #pragma mark -- MPMapViewDelegate
     89 
     90 - (void) mapViewWillStartLoadingMap:(MKMapView *)mapView
     91 {
     92     
     93 }
     94 
     95 -(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
     96 {
     97     
     98 }
     99 
    100 - (void) mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
    101 {
    102     
    103 }
    104 
    105 #pragma mark -- CLLocationManagerDelegate
    106 - (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    107 {
    108     if ([locations count] > 0) {
    109         CLLocation* loc = [locations objectAtIndex:0];
    110         CLLocationCoordinate2D pos = [loc coordinate];
    111         
    112         NSLog(@"locationManager, longitude: %f, latitude: %f", pos.longitude, pos.latitude);
    113         
    114         if (show == NO) {
    115             MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos为中心,显示2000米
    116             MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//适配map view的尺寸
    117             [_mapView setRegion:adjustedRegion animated:YES];
    118 
    119             show = YES;
    120         }
    121     }
    122 }
    123 
    124 @end
    复制代码
     
  • 相关阅读:
    PostgreSQL表空间、数据库、模式、表、用户/角色之间的关系(转)
    PostgreSQL学习手册-模式Schema(转)
    Python中的编码与解码(转)
    HttpRequest中常见的四种Content-Type(转)
    Django中对静态文件的支持(转)
    IPython的基本功能(转)
    GET请求Referer限制绕过总结
    Linux pwn入门教程(6)——格式化字符串漏洞
    CVE-2015-1641 Office类型混淆漏洞及shellcode分析
    我用着差不多的套路收拾差不多的骗子过着差不多的又一天!
  • 原文地址:https://www.cnblogs.com/LifeTechnologySupporter/p/4762747.html
Copyright © 2020-2023  润新知