【问题标题】:Zooming Out to show User Location/Annotation缩小以显示用户位置/注释
【发布时间】:2014-05-22 00:38:44
【问题描述】:

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

  1. 我不希望用户的位置有图钉(请参阅image1)。
  2. 引脚太靠近边缘,有时位于导航/标签栏下方(请参阅image2)。需要一些填充,但不确定如何实现。

非常感谢任何帮助。下面是我的地图视图控制器代码。

#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];

【问题讨论】:

  • “我不希望用户的位置有图钉”:那就不要将其添加为注释。 看起来您不需要 CLLocationManager首先是你在做什么。
  • 由于 showAnnotations: 方法,我将其添加为注释(在某处看到有人推荐它)。与 CLLocationManager 相同。至少它显示了 2 个位置 =/.
  • 当显示用户位置在地图视图中打开时,它会自动为您添加注释(蓝点)。但是,这可能不会在 viewDidLoad 中立即发生。即使使用位置管理器,它在您调用 startUpdatingLocation 后立即返回有效坐标的事实也是偶然的。当地图视图实际上有一个坐标时,它会调用 didUpdateUserLocation。 showAnnotations 或 setRegion 应该在 didUpdateUserLocation 中完成(带有一些标志只做一次)。
  • 对于填充区域,我推荐this approach,但调用setVisibleMapRect:edgePadding:animated: 而不仅仅是setVisibleMapRect:animated:
  • 几件事。如果您在用户位置上获得了一个图钉,则需要更改 viewForAnnotation: 方法以检查用户位置,如果当前注释是用户位置,则返回 nil:if(annotation == mapView.userLocation) return nil

标签: ios objective-c mapkit core-location


【解决方案1】:

所以想一想。

如果您不希望显示用户的位置,请将其关闭地图的显示当前位置标志。

打开位置管理器并询问位置更新。

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

这将为您提供 2 之间的中心点。然后取这两个点之间的纬度增量,这两个点之间的经度增量,将这些增量乘以 1.1 或 1.2 之类的小比例因子,所以两者点不在屏幕边缘,并将这些数字输入 MKCoordinateSpanMake,并使用生成的跨度加上中心点来创建坐标区域,并将地图设置为该坐标区域。

【讨论】:

  • 谢谢Duncan,我会看一下文档,明天再返回一些结果。我确实希望显示用户位置,而不是顶部的红色图钉(如图所示)。
  • 照你说的做了,谢谢!更新了我的原始帖子以包含代码。
【解决方案2】:

对于 swift 3

let annotation = MKPointAnnotation();
    annotation.coordinate = CLLocationCoordinate2DMake(LATITUDE, LONGITUDE);
    self.mapView.addAnnotation(annotation);


    let userlongitude = location.coordinate.longitude
    let userlatitude = location.coordinate.latitude
    let newDistance = CLLocation(latitude: userlatitude, longitude: userlongitude).distance(from: CLLocation(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude))
    let region = MKCoordinateRegionMakeWithDistance(location.coordinate, 2 * newDistance, 2 * newDistance)
    let adjustRegion = self.mapView.regionThatFits(region)
    self.mapView.setRegion(adjustRegion, animated:true)

在上面的代码中,“位置”是CLLocation,它保存了用户当前的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多