zoukankan      html  css  js  c++  java
  • iOS开发学习之MapKit

    -(MKCoordinateRegion)regionForAnnotations:(NSArray *)annotations  //可原封不动的复制运用

    {

        MKCoordinateRegion region;

        

        if([annotations count] == 0)

        {

            region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.coordinate, 1000, 1000);

        }

        else if([annotations count] == 1)

        {

            id <MKAnnotation> annotation = [annotations lastObject];

            region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 1000, 1000);

        }

        else

        {

            CLLocationCoordinate2D topLeftCoord;

            topLeftCoord.latitude = -90;

            topLeftCoord.longitude = 180;

            

            CLLocationCoordinate2D bottomRightCoord;

            bottomRightCoord.latitude = 90;

            bottomRightCoord.longitude = -180;

            

            for(id <MKAnnotation> annotation in annotations)

            {

                topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);

                topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);

                bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);

                bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);

            }

            

            const double extraSpace = 1.1;

            

            region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) / 2.0;

            region.center.longitude = topLeftCoord.longitude - (topLeftCoord.longitude - bottomRightCoord.longitude) / 2.0;

            region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * extraSpace;

            region.span.longitudeDelta = fabs(topLeftCoord.longitude - bottomRightCoord.longitude) * extraSpace;

        }

        return [self.mapView regionThatFits:region];

    }

    使用说明:

    -(IBAction)showLocations

    {

        MKCoordinateRegion region = [self regionForAnnotations:_locations];  

        //调用以上方法,获得一个MKCoordinateRegion区域,该区域包含_locations里的多个标记

        

        [self.mapView setRegion:region animated:YES];

    }

    注意:_locations里的对象必须实现协议MKAnnotation,并实现以下三个方法:

    -(CLLocationCoordinate2D)coordinate

    {

        return CLLocationCoordinate2DMake([self.latitude doubleValue], [self.longitude doubleValue]);

    }

    -(NSString *)title

    {

        if([self.locationDescription length] > 0)

        {

            return self.locationDescription;

        }

        else

        {

            return @"(No Description)";

        }

    }

    -(NSString *)subtitle

    {

        return self.category;

    }

  • 相关阅读:
    使用urllib
    spring常用的45个注解
    音痴
    android与JS函数传参遗留问题
    方舟编译器源码过一遍流程
    什么是语义学,解释器
    synchronized,ReentrantLock解决锁冲突,脏读的问题
    【Unity3d】ScrollRect自动定位到某点
    计算点到直线的距离】 C#实现
    理财-房月供占工资多少比较合适?
  • 原文地址:https://www.cnblogs.com/guitarandcode/p/5571457.html
Copyright © 2011-2022 走看看