【问题标题】:Polylines are being drawn randomly and in random location on map折线是随机绘制的,并且在地图上的随机位置
【发布时间】:2015-02-12 08:58:52
【问题描述】:

我正在努力研究如何让线条显示在我的地图上。我正在尝试从接触开始到接触结束画一条线。

目前我正在获取我触摸位置的坐标,将其添加到数组中,获取触摸结束位置的线,将其添加到数组中,然后画线。这是我下面的代码。

当我遗憾地运行代码时,我的行没有显示。然而,当我坐着想知道我错过了什么(因为我的坐标被保存到阵列中)时,我沮丧地不停地滑动我的屏幕,突然出现了一条线!我关闭了程序并再次执行相同的操作,据我所知,线条是随机绘制的,并且坐标错误。

我变了

CLLocationCoordinate2D obh1=CLLocationCoordinate2DMake
    (startTouch.latitude, startTouch.longitude);

CLLocationCoordinate2D obh1=CLLocationCoordinate2DMake
    (startTouch.longitude, startTouch.latitude);

看看我是否以某种方式混合了我的坐标,但同样的事情正在发生。这些线是在随机位置绘制的。

如果有人能帮我解决这个问题,我将不胜感激。

我做了一些工作,这就是我想出的。当我单击地图时,您看到的 4 个线点会出现。它们是触动的结束,触动的开始。我不知道它们来自哪里,但不是来自我的代码。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Touches began");

    UITouch *touch = [touches anyObject];

    // Get the specific point that was touched
    CGPoint point = [touch locationInView:self.view];
     //convert toc
    CLLocationCoordinate2D startTouch
    =[self.map convertPoint:point toCoordinateFromView:_map];
    CLLocationCoordinate2D obh1=CLLocationCoordinate2DMake(startTouch.latitude, startTouch.longitude);
    [arrayOfLine addObject: [NSValue valueWithMKCoordinate:obh1]];
    NSLog(@"array: %@", arrayOfLine);
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Touches END");


    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    CLLocationCoordinate2D endTouch
    =[self.map convertPoint:point toCoordinateFromView:_map];
    CLLocationCoordinate2D obh=CLLocationCoordinate2DMake(endTouch.latitude, endTouch.longitude);
        [arrayOfLine addObject: [NSValue valueWithMKCoordinate:obh]];
        NSLog(@"array: %@", arrayOfLine);

            MKPolyline *polygon = [MKPolyline polylineWithCoordinates:&obh count:[arrayOfLine count]];

    [_map addOverlay:polygon];
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]])
    {
        MKPolylineView *overlayView = [[MKPolylineView alloc] initWithPolyline:overlay];

        overlayView.strokeColor     = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        overlayView.lineWidth       = 3;

        return overlayView;
    }

    return nil;
}

【问题讨论】:

    标签: ios objective-c mkmapview mkoverlay mkpolyline


    【解决方案1】:

    随机行来自相关代码。

    主要问题在本节touchesEnded

    CLLocationCoordinate2D obh=CLLocationCoordinate2DMake(endTouch.latitude, endTouch.longitude);
    [arrayOfLine addObject: [NSValue valueWithMKCoordinate:obh]];
    NSLog(@"array: %@", arrayOfLine);
    
    MKPolyline *polygon = [MKPolyline polylineWithCoordinates:&obh count:[arrayOfLine count]];
    


    调用polylineWithCoordinates 时,代码给出obh 的地址,这是一个单个 CLLocationCoordinate2D 结构,但count 设置为arrayOfLine 中的对象数,这将最可能是两个(触摸开始 + 触摸结束)。

    polylineWithCoordinates 方法需要一个由 CLLocationCoordinate2D 结构组成的普通 C 数组。

    目前,代码只为该方法提供了一个结构,但计数告诉该方法也使用内存中 single 结构之后的字节作为数组中的坐标。 obh 之后的内存中的字节很可能是随机值,这些值会转换为无意义的坐标,因此您会得到随机行或根本没有行。


    要解决此问题,您需要从 arrayOfLine(Objective-C NSArray)构造一个纯 C 数组。
    这是一种方法:

    //REPLACE this existing line with the code below:
    //MKPolyline *polygon = [MKPolyline polylineWithCoordinates:&obh count:[arrayOfLine count]];
    
    CLLocationCoordinate2D coords[arrayOfLine.count];
    
    for (int c = 0; c < arrayOfLine.count; c++)
    {
        NSValue *v = [arrayOfLine objectAtIndex:c];
        coords[c] = v.MKCoordinateValue;
    }
    
    MKPolyline *polygon = [MKPolyline polylineWithCoordinates:coords count:[arrayOfLine count]];
    


    其他一些不直接导致问题的点:
    1. touchesBegan 中,不需要创建obh1startTouch 已经是 CLLocationCoordinate2D,您可以将其添加到数组中。
    2. touchesEnded 中,不需要创建obhendTouch 已经是 CLLocationCoordinate2D,您可以将其添加到数组中。
    3. 目前,arrayOfLine 在任何时候都不会被清除。这意味着每当触摸结束时,用户绘制的所有行都会重新绘制(并连接在一起)。不确定意图是什么,但如果您只想绘制当前的开始+结束触摸对,那么在touchesBegan 的顶部,执行[arrayOfLine removeAllObjects];
    4. 确保在地图视图上将userInteractionEnabled 设置为NO,否则它可能会干扰触摸方法并导致奇怪的行为(例如touchesEnded 未被调用)。

    【讨论】:

    • 谢谢你,我已经修复了 1,2&4,第 3 点是设计使然。如果允许我将您用作一些提示的共鸣板,我将不胜感激。我所做的重点是能够在地图上绘制以引导您到某个地方,如您所知,道路将是弯曲的等。但这只会绘制直线。我画了一个视图,线条很完美,因为我可以随意弯曲它们,但我不能在地图上画出来。如果我要为某人绘制路线,您会推荐一张下面有视图的透明地图吗?然后又是如何处理放大的
    • 对于第 3 点:在这种情况下,您至少应该在添加新的覆盖之前删除所有现有的覆盖。否则,您最终会得到多个重叠的叠加层。 Overlay[1] 有第 1 行,overlay[2] 有第 1 到 2 行,overlay[3] 有第 1 到 3 行,依此类推。在 addOverlay 之前,执行 [_map removeOverlays:_map.overlays];
    • 尚不清楚为什么用户必须在地图上画线才能将它们引导到某个地方。如果您希望线条在弯曲时跟随用户的手指,请尝试更新数组并替换 touchesMoved 中的覆盖而不是 touchesEnded。
    • 不过,尚不清楚为什么用户必须通过绘图来获取路线。 MKDirections 类将为您提供绘制到某个地方的方向所需的 MKPolylines。研究并尝试更多的东西,然后发布一个新的、详细的问题(带有代码)。
    • 假设你去东京,你想知道健身房在哪里,或者地图上没有的小巷子在哪里,你问别人。 (我使用tokoyo,但也许我不应该说一些在谷歌位置上无法提供所有内容的小国家)我希望个人能够追踪从您所在位置到所需位置的路线。这是我的初衷
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 2013-09-07
    • 2015-10-28
    相关资源
    最近更新 更多