MapMapM 2013-04-08 07:07 采纳率: 0%
浏览 6379
已采纳

在MKMapView中设置范围

有10-15个经纬度需要同时显示在地图上,怎么设置区域这样可以在地图中看到所有的注释?

  • 写回答

2条回答

  • g989_1314125 2013-04-08 09:32
    关注
    create class as below. create two class: ViewController is class of UIViewController and MapViewAnnotation is class of NSObject. I have created that two class as  below. bind mapview in XIB of ViewController and set delegate.
    
    #import <UIKit/UIKit.h>
    #import <MapKit/MapKit.h>
    #import "MapViewAnnotation.h"
    
    @interface ViewController : UIViewController<MKMapViewDelegate>{
        IBOutlet  MKMapView* mapView;
         NSMutableArray *arrayLocation;
    }
    @end
    #import "ViewController.h"
    @implementation ViewController
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        arrayLocation = [[NSMutableArray alloc]init];
        NSMutableArray *arrAnnotations  = [[NSMutableArray alloc]init];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setValue:@"32.774125" forKey:@"latitude"];
        [dict setValue:@"-117.240658" forKey:@"longitude"];
        [dict setValue:@"Bars & Restaurants 1" forKey:@"title"];
        [arrayLocation addObject:dict];
        dict = nil;
    
        dict = [[NSMutableDictionary alloc]init];
        [dict setValue:@"32.784526" forKey:@"latitude"];
        [dict setValue:@"-117.240985" forKey:@"longitude"];
        [dict setValue:@"Bars & Restaurants 2" forKey:@"title"];
        [arrayLocation addObject:dict];
        dict = nil;
    
        dict = [[NSMutableDictionary alloc]init];
        [dict setValue:@"32.773477" forKey:@"latitude"];
        [dict setValue:@"-117.241144" forKey:@"longitude"];
        [dict setValue:@"Bars & Restaurants 3" forKey:@"title"];
        [arrayLocation addObject:dict];
        dict = nil;
    
        dict = [[NSMutableDictionary alloc]init];
        [dict setValue:@"32.775301" forKey:@"latitude"];
        [dict setValue:@"-117.238893" forKey:@"longitude"];
        [dict setValue:@"Bars & Restaurants 4" forKey:@"title"];
        [arrayLocation addObject:dict];
        dict = nil;
    
        for(int i=0;i<[arrayLocation count];i++)
        {
            CLLocationCoordinate2D location;
            location.latitude = [[[arrayLocation objectAtIndex:i] objectForKey:@"latitude"] doubleValue];
            location.longitude = [[[arrayLocation objectAtIndex:i] objectForKey:@"longitude"] doubleValue];
    
            MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:[[arrayLocation objectAtIndex:i] objectForKey:@"title"] Coordinate:location andIndex:i];
            [arrAnnotations addObject:newAnnotation]; 
        }
        [mapView addAnnotations:arrAnnotations];
        mapView.region = [MapViewAnnotation regionForAnnotations:arrAnnotations];
    }
    - (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 
    {
        if (annotation == mapView.userLocation)
        {
            return nil;
        }
        MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @"restMap"];
        if (pin == nil) 
        {
            pin = [[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"restMap"];
        }
        else 
        {
            pin.annotation = annotation;
        }
        pin.pinColor = MKPinAnnotationColorRed;
        pin.animatesDrop = NO;
        pin.canShowCallout=TRUE;
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        MapViewAnnotation *temp = (MapViewAnnotation *)pin.annotation;
        btn.tag=temp.index;
        pin.rightCalloutAccessoryView=btn;
        [btn addTarget:self action:@selector(openDetail:) forControlEvents:UIControlEventTouchUpInside];
    
        return pin;
    }
    @end
    Implementation of MapViewAnnotation class
    
    #import <MapKit/MapKit.h>
    
    @interface MapViewAnnotation : NSObject <MKAnnotation , MKMapViewDelegate>{
        NSString *title;
        int index;
        CLLocationCoordinate2D coordinate;
    
    }
    @property (nonatomic, copy) NSString *title;
    @property (nonatomic, readwrite) int index;
    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
    
    - (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;
    - (id)initWithTitle:(NSString *)ttl Coordinate:(CLLocationCoordinate2D)c2d andIndex:(int)intIndex;
    
    +(MKCoordinateRegion) regionForAnnotations:(NSArray*) annotations ;
    @end
    
    #import "MapViewAnnotation.h"
    @implementation MapViewAnnotation
    @synthesize title, coordinate,index;
    
    - (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
        self = [super init];
        title = ttl;
        coordinate = c2d;
        return self;
    }
    - (id)initWithTitle:(NSString *)ttl Coordinate:(CLLocationCoordinate2D)c2d andIndex:(int)intIndex
    {
        self = [super init];
        title = ttl;
        coordinate = c2d;
        index = intIndex;
        return self;
    }
    
    +(MKCoordinateRegion) regionForAnnotations:(NSArray*) annotations 
    {
        NSAssert(annotations!=nil, @"annotations was nil");
        NSAssert([annotations count]!=0, @"annotations was empty");
    
        double minLat=360.0f, maxLat=-360.0f;
        double minLon=360.0f, maxLon=-360.0f;
    
        for (id<MKAnnotation> vu in annotations) {
            if ( vu.coordinate.latitude  < minLat ) minLat = vu.coordinate.latitude;
            if ( vu.coordinate.latitude  > maxLat ) maxLat = vu.coordinate.latitude;
            if ( vu.coordinate.longitude < minLon ) minLon = vu.coordinate.longitude;
            if ( vu.coordinate.longitude > maxLon ) maxLon = vu.coordinate.longitude;
        }
        CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLat+maxLat)/2.0, (minLon+maxLon)/2.0);
        MKCoordinateSpan span = MKCoordinateSpanMake(maxLat-minLat, maxLon-minLon);
        MKCoordinateRegion region = MKCoordinateRegionMake (center, span);
        return region;
    }
    @end
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败