zoukankan      html  css  js  c++  java
  • MKMapView和MKMapViewDelegate

    1. 概述

    1. 插入MapView,设置Delegate(一般为Controller),Annotations记录兴趣位置点(AnnotationView用来显示兴趣位置点),  
    2. annotation是可选的,选中的annotation会显示callout,用来显示信息。  


    2. 设置地图显示类型:

    1. mapView.mapType = MKMapTypeStandard;  
    2. mapView.mapType = MKMapTypeSatellite;  
    3. mapView.mapType = MKMapTypeHybrid;   

    3. 显示用户位置

    1. 设置为可以显示用户位置:  
    2. mapView.showsUserLocation = YES;   
    3. 判断用户当前位置是否可见(只读属性):  
    4. userLocationVisible   
    5. 得到用户位置坐标:当userLocationVisible为YES时  
    6. CLLocationCoordinate2D coords = mapView.userLocation.location.coordinate;   

    4.坐标范围

    1. MKCoordinateRegion用来设置坐标显示范围。包括两部分:  
    2.   
    3. a. Center(CLLocationCoordinate2D struct,包括latitude和longitude),坐标中心  
    4. b. Span(MKCoordinateSpan struct,包括latitudeDelta和longitudeDelta),缩放级别  
    5.   
    6. //创建一个以center为中心,上下各1000米,左右各1000米得区域,但其是一个矩形,不符合MapView的横纵比例  
    7. MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(center,2000, 2000);  
    8.   
    9. //以上代码创建出来一个符合MapView横纵比例的区域  
    10. MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];   
    11.   
    12. //以上代码为:最终显示该区域  
    13. [mapView setRegion:adjustedRegion animated:YES];   

    5. delegate

    1. 使用MapView须符合MKMapViewDelegate协议  
    2. 5.1、地图加载Delegate  
    3.   
    4. 当需要从Google服务器取得新地图时  
    5. mapViewWillStartLoadingMap:   
    6. 当成功地取得地图后  
    7. mapViewDidFinishLoadingMap:   
    8. 当取得地图失败后(建议至少要实现此方法)  
    9. mapViewDidFailLoadingMap:withError:  
    10.   
    11. 5.2、范围变化Delegate  
    12.   
    13. 当手势开始(拖拽,放大,缩小,双击)  
    14. mapView:regionWillChangeAnimated:   
    15. 当手势结束(拖拽,放大,缩小,双击)  
    16. mapView:regionDidChangeAnimated:   
    17. 判断坐标是否在MapView显示范围内:  
    18. CLLocationDegrees leftDegrees = mapView.region.center.longitude –(mapView.region.span.longitudeDelta / 2.0);  
    19. CLLocationDegrees rightDegrees = mapView.region.center.longitude +(mapView.region.span.longitudeDelta / 2.0);  
    20. CLLocationDegrees bottomDegrees = mapView.region.center.latitude –(mapView.region.span.latitudeDelta / 2.0);  
    21. CLLocationDegrees topDegrees = self.region.center.latitude +(mapView.region.span.latitudeDelta / 2.0);  
    22.   
    23. if (leftDegrees > rightDegrees)   
    24. { // Int'l Date Line in View  
    25.     leftDegrees = -180.0 - leftDegrees;  
    26.     if (coords.longitude > 0) // coords to West of Date Line  
    27.         coords.longitude = -180.0 - coords.longitude;  
    28. }  
    29.   
    30. if (leftDegrees <= coords.longitude   
    31.     && coords.longitude <= rightDegrees   
    32.     && bottomDegrees <= coords.latitude   
    33.     && coords.latitude <= topDegrees)   
    34. {  
    35.     // 坐标在范围内  
    36. }  

    6.Annotation

    1. Annotation包含两部分:Annotation Object和Annotation View  
    2. Annotation Object 必须符合协议MKAnnotation,包括两个方法:title和subtitle,分别用于显示注释的标题和子标题。还有coordinate属性, 返回CLLocationCoordinate2D,表示Annotation的位置  
    3. 然后,需使用mapView:viewForAnnotation: 方法来返回MKAnnotationView或者MKAnnotationView的子类用来显示Annotation(注意:这里显示的不是选中Annotation后的弹出框)   
    4. 你可以子类化MKAnnotationView,然后再drawRect:方法里面进行自己的绘制动作(这个方法很蠢)  
    5. 你完全可以实例化一个MKAnnotationView,然后更改它的image属性,这样很简单。  

    7.添加移除Annotation

    1. 添加一个Annotation  
    2. [mapView addAnnotation:annotation];   
    3. 添加一个Annotation数组  
    4. [mapView addAnnotations:[NSArray arrayWithObjects:annotation1, annotation2, nil]];   
    5. 移除一个Annotation  
    6. removeAnnotation:   
    7. 移除一个Annotation数组  
    8. removeAnnotations:   
    9. 移除所有Annotation  
    10. [mapView removeAnnotations:mapView.annotations];  


    8. 选中Annotation

    1. 一次只能有一个Annotation被选中,选中后会出现CallOut(浮动框)  
    2. 简单的CallOut显示Title和SubTitle,但你也可以自定义一个UIView作为CallOut(与自定义的TableViewCell一样)  
    3. 可通过代码选中Annotation:  
    4. selectAnnotation:animated:   
    5. 或者取消选择:  
    6. deselectAnnotation:animated:  


    9. 显示Annotation

    1. 通过mapView:viewForAnnotation: 方法显示Annotation,每在MapView中加入一个Annotation,就会调用此方法  
    2. 示例(与tableView:cellForRowAtIndexPath: 很相似)  
    3.   
    4. - (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation   
    5. {  
    6.     static NSString *placemarkIdentifier = @"my annotation identifier";  
    7.     if ([annotation isKindOfClass:[MyAnnotation class]])   
    8.     {  
    9.         MKAnnotationView *annotationView = [theMapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];  
    10.         if (annotationView == nil)   
    11.         {  
    12.             annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];  
    13.             annotationView.image = [UIImage imageNamed:@"blood_orange.png"];  
    14.         }  
    15.         else  
    16.             annotationView.annotation = annotation;  
    17.         return annotationView;  
    18.     }  
    19.     return nil;  
    20. }  


    10. 取得真实地址

      1. 示例:  
      2.   
      3. 初始化MKReverseGeocoder  
      4. MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinates];  
      5. geocoder.delegate = self;  
      6. [geocoder start];   
      7.   
      8. 如果无法处理坐标,则调用reverseGeocoder:didFailWithError: 方法  
      9. - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error   
      10. {  
      11.     NSLog(@"Error resolving coordinates: %@", [error localizedDescription]);  
      12.     geocoder.delegate = nil;  
      13.     [geocoder autorelease];  
      14. }   
      15. 如果成功,则调用reverseGeocoder:didFindPlacemark: 并把信息存储在MKPlacemark 中  
      16. didFindPlacemark:(MKPlacemark *)placemark   
      17. {  
      18.     NSString *streetAddress = placemark.thoroughfare;  
      19.     NSString *city = placemark.locality;  
      20.     NSString *state = placemark.administrativeArea;  
      21.     NSString *zip = placemark.postalCode;  
      22.     // Do something with information  
      23.     geocoder.delegate = nil;  
      24.     [geocoder autorelease];  
      25. }
  • 相关阅读:
    eclipse真机调试显示Target unknown的解决方法
    教你看懂GERBER中的钻孔(.txt)文件
    Quartus ii 12.0 和ModelSim 10.1 SE安装及连接
    Android的学习——ubuntu下android5.1源码的make编译
    ubuntu 14.04 下找不到命令,路径出错
    【转载】VMware虚拟机修改硬盘容量大小
    Fedora10下建立linux系统的窗口没有地址栏
    [转]SecureCRT连接主机时,无法从键盘输入
    在FASTBuild中使用Distribution
    在FASTBuild中使用Caching
  • 原文地址:https://www.cnblogs.com/shuxiachahu123/p/5050271.html
Copyright © 2011-2022 走看看