iOS开发学习之MapKit - 获得在MapView(地图)中显示多个标记的区域(MKCoordinateRegion)...

-(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;

}

转载于:https://www.cnblogs.com/guitarandcode/p/5571457.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在UIApp实现地图定位,您可以使用iOS SDK提供的MapKit框架。以下是一个简单的示例代码,演示如何在UIApp显示地图和定位用户的位置: ``` import UIKit import MapKit class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { @IBOutlet weak var mapView: MKMapView! let locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() // 设置地图代理 mapView.delegate = self // 配置定位管理器 locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() } // MARK: - CLLocationManagerDelegate func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let location = locations.last! // 创建地图区域 let region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: 1000, longitudinalMeters: 1000) mapView.setRegion(region, animated: true) // 在地图显示用户位置 mapView.showsUserLocation = true } // MARK: - MKMapViewDelegate func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) { // 更新用户位置 mapView.centerCoordinate = userLocation.coordinate } func mapView(_ mapView: MKMapView, didFailToLocateUserWithError error: Error) { // 定位失败时弹出警告框 let alert = UIAlertController(title: "定位失败", message: error.localizedDescription, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "确定", style: .default, handler: nil)) present(alert, animated: true, completion: nil) } } ``` 在这个示例,我们首先在`viewDidLoad()`方法设置了地图代理和定位管理器。然后,在`locationManager(_:didUpdateLocations:)`方法,我们获取了用户当前的位置,并使用`MKCoordinateRegion`创建了一个地图区域。接着,我们在地图显示了用户的位置。 在`mapView(_:didUpdate:)`方法,我们更新了地图心点,以确保用户的位置始终位于地图心。最后,在`mapView(_:didFailToLocateUserWithError:)`方法,我们处理了定位失败的情况,并弹出了一个警告框来通知用户。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值