【问题标题】:Why isn't the center of my mapview's frame lining up its center coordinate?为什么我的地图视图框架的中心没有与其中心坐标对齐?
【发布时间】:2020-06-28 12:02:14
【问题描述】:

我正在尝试在地图视图的中心渲染一个圆圈。我还在地图视图上显示用户的当前位置,并将地图的中心设置为那里。不过,这两个没有排队。

这是我建立圈子的地方:

[self.mapView setShowsUserLocation:YES];
CAShapeLayer *circle = [CAShapeLayer layer];
int radius = 50;
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
                                         cornerRadius:radius].CGPath;
// Set position of the circle.
// We use the frame of the mapview subtracted by circle radius to determine the center
circle.position = CGPointMake(CGRectGetMidX(self.mapView.frame)-radius,
                              CGRectGetMidY(self.mapView.frame)-radius);
circle.fillColor = [[UIColor alloc] initWithRed:0.5 green:0.5 blue:0.6 alpha:0.3].CGColor;
circle.strokeColor = [UIColor blackColor].CGColor;
circle.lineWidth = 1;

// Add to parent layer
[self.mapView.layer addSublayer:circle];

// Add 'crosshair'
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor blackColor];
view.frame = CGRectMake(0, 0, 5, 5);
[self.mapView addSubview:view];
view.center = self.mapView.center;

在 didUpdateUserLocation 委托方法中我有:

    [self.mapView setCenterCoordinate:userLocation.coordinate];

这是模拟器上的样子。

我最终希望允许用户平移和选择地图的一个区域,但现在我不确定我的地图中心坐标是否与我的圆心重合。有什么帮助吗?谢谢!

【问题讨论】:

    标签: ios objective-c mkmapview


    【解决方案1】:

    您可能遇到了我最近发现的相同错误。我已经向 Apple 报告了它,它被标记为重复,所以它可能会在 iOS 7.1 中修复。

    Bug with MKMapView's centerCoordinate during rotation?

    【讨论】:

    • 认为你可能是对的。当我将地图视图从 TabBarController 中取出时,中心似乎与状态栏的中心 +10p 重合。
    【解决方案2】:

    我认为是状态栏问题。

    试试这个。

    [self.mapView setShowsUserLocation:YES];
    CAShapeLayer *circle = [CAShapeLayer layer];
    int radius = 50;
    circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
                                         cornerRadius:radius].CGPath;
    // Set position of the circle.
    // We use the frame of the mapview subtracted by circle radius to determine the center
    circle.position = CGPointMake(CGRectGetMidX(self.mapView.frame)-radius,
                              CGRectGetMidY(self.mapView.frame)-radius-20);
    circle.fillColor = [[UIColor alloc] initWithRed:0.5 green:0.5 blue:0.6 alpha:0.3].CGColor;
    circle.strokeColor = [UIColor blackColor].CGColor;
    circle.lineWidth = 1;
    
    // Add to parent layer
    [self.mapView.layer addSublayer:circle];
    
    // Add 'crosshair'
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor blackColor];
    view.frame = CGRectMake(0, 0, 5, 5);
    [self.mapView addSubview:view];
    view.center = self.mapView.center;
    

    我希望它对你有用。

    【讨论】:

    • 添加到 Y 将其移动到错误的方向。我减去了 10,它很接近。减去所有数字中的 15 即可完美排列。 (i.imgur.com/ymTZDae.jpg) 不过,我正在寻找一个无论地图视图在哪里都可以使用的公式,因为我正在创建一个地图视图类别。
    【解决方案3】:

    看起来像 20 点偏移(与状态栏相同)。在您使用frame 查找中心后,您的地图视图可能正在调整大小。

    您可以尝试使用MKCircleView 或其他叠加层来代替形状图层。

    【讨论】:

    • 谢谢,如果是这样的话,我正在努力追查。奇怪的是,确切的偏移量是 15。如果我在 Y 上添加 -15,它就会排成一行。我想象它与状态栏和标签栏的组合有关。此外,我使用的是形状图层而不是圆形视图,因为我希望圆圈在用户平移时在地图中心保持静态。
    【解决方案4】:

    使用 Xamarin.Forms,我必须确保启用安全区域布局指南,并将其添加到我的 ContentPage XAML。

    <ContentPage ...
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
             Title="Safe Area"
             ios:Page.UseSafeArea="true">
    <StackLayout>
        ...
    </StackLayout>
    

    欲了解更多信息:Safe Area Layout Guide on iOS

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2019-08-06
      • 2016-03-21
      • 1970-01-01
      相关资源
      最近更新 更多