ios - 缩小以显示用户位置/注释

标签 ios objective-c mapkit core-location

我可以缩小 map 以包含用户的位置和我想要的注释;但是我有两个问题。

  1. 我不希望用户的位置有 PIN 码(参见 image1)。
  2. 图钉太靠近边缘,有时在导航/标签栏下方(参见 image2 )。需要一些填充,但不确定如何完成。

非常感谢任何帮助。下面是我的 map View Controller 代码。

#import "BuildingsMapViewController.h"
#import "BuildingsAnnotations.h"

@interface BuildingsMapViewController () 
@end

@implementation BuildingsMapViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];


    BuildingsAnnotations *userAnnotation = [[BuildingsAnnotations alloc] init];
    userAnnotation.coordinate = self.locationManager.location.coordinate;


    self.mapView.delegate = self;
    self.navigationItem.title = self.title;

    CLLocationCoordinate2D startCenter = CLLocationCoordinate2DMake(42.334596, -83.049578);
    CLLocationDistance regionWidth = 1500;
    CLLocationDistance regionHeight = 1500;

    MKCoordinateRegion startRegion = MKCoordinateRegionMakeWithDistance(startCenter, regionWidth, regionHeight);
    [self.mapView setRegion:startRegion animated:YES];


    self.coordinate = CLLocationCoordinate2DMake(self.geoPoint.latitude,self.geoPoint.longitude);
    BuildingsAnnotations *annotation = [[BuildingsAnnotations alloc] init];
    annotation.coordinate = self.coordinate;
    annotation.title = self.navigationItem.title;
    annotation.subtitle = @"Click for Directions";
    [self.mapView addAnnotation:annotation];
    [self.mapView setCenterCoordinate:annotation.coordinate animated:YES];


    self.annotations = @[annotation, userAnnotation];
    [self.mapView showAnnotations:self.annotations animated:YES];

}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    NSLog(@"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);
}

@end

更新:按照 Duncan 所说的进行操作并使其发挥作用。这是我的代码:

// Get user coordinates
self.userLat = self.locationManager.location.coordinate.latitude;
self.userLong = self.locationManager.location.coordinate.longitude;

// Initialize and set arrays
self.lats = [[NSMutableArray alloc] init];
self.lngs = [[NSMutableArray alloc] init];

[self.lats addObject:[NSNumber numberWithDouble:self.userLat]];
[self.lats addObject:[NSNumber numberWithDouble:annotation.coordinate.latitude]];
[self.lngs addObject:[NSNumber numberWithDouble:self.userLong]];
[self.lngs addObject:[NSNumber numberWithDouble:annotation.coordinate.longitude]];

// Sort numbers in arrays
[self.lats sortUsingSelector:@selector(compare:)];
[self.lngs sortUsingSelector:@selector(compare:)];

// Get smallest/biggest coordinates
double smallestLat = [self.lats[0] doubleValue];
double smallestLng = [self.lngs[0] doubleValue];
double biggestLat = [[self.lats lastObject] doubleValue];
double biggestLng = [[self.lngs lastObject] doubleValue];

// Get Center Point and Span
CLLocationCoordinate2D annotationsCenter = CLLocationCoordinate2DMake((biggestLat + smallestLat) / 2, (biggestLng + smallestLng) / 2);
MKCoordinateSpan annotationsSpan = MKCoordinateSpanMake((biggestLat - smallestLat) * 1.75, (biggestLng - smallestLng) * 1.75);

// Create and set Region
MKCoordinateRegion region = MKCoordinateRegionMake(annotationsCenter, annotationsSpan);
[self.mapView setRegion:region];

最佳答案

所以请考虑一下。

如果您不想显示用户的位置,请关闭 map 的显示当前位置标志。

打开位置管理器并请求位置更新。

在为您提供位置更新的位置管理器委托(delegate)方法中,获取用户的新位置,并将其与您要显示的注释进行平均 (lat1 + lat2)/2,(long1 + long2)/2。

这会给你一个介于 2 之间的中心点。然后取这两点之间的纬度增量,这两点之间的经度增量,将这些增量乘以一个小的比例因子,如 1.1 或 1.2,所以这两个点不在屏幕的边缘,并将这些数字输入 MKCoordinateSpanMake,并使用生成的跨度加上中心点来创建坐标区域,并将 map 设置到该坐标区域。

关于ios - 缩小以显示用户位置/注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23796042/

相关文章:

objective-c - MKAnnotationView 的子类

ios - 如何正确删除 NSManagedObject 关系

ios - Swift Firebase 为 UIActivityViewController 附加图像

iphone - Xcode 4 问题中的多个 Nib

c++ - Objective-C++ 未编译

ios - 在 UISegmentedControl 中换行文本 (iOS 9+)

iphone - 如何在Apple的MapKit中处理KMZ文件

ios - 使用 Restkit 中的 RKObjectManager 在 https 和 http 之间切换的最佳方法是什么?

objective-c - NSTextField doubleValue 本地化混淆

SwiftUI View 阻止对 MapKit 的触摸,但不会阻止其他 View