Ios 多段线是在地图上的随机位置随机绘制的

Ios 多段线是在地图上的随机位置随机绘制的,ios,objective-c,mkmapview,mkoverlay,mkpolyline,Ios,Objective C,Mkmapview,Mkoverlay,Mkpolyline,我正在努力研究如何在地图上显示线条。我试图从触球开始到触球结束划一条线 现在,我正在获取我触摸的位置坐标,将其添加到阵列中,获取触摸末端的连线,将其添加到阵列中,然后绘制线。下面是我的代码 当我悲伤地运行代码时,我的行没有显示出来。然而,当我坐在那里想我错过了什么,因为我的坐标被保存到阵列中时,我沮丧地不停地滑动屏幕,突然出现了一条线!我关闭了程序,再次做了同样的事情,据我所知,线条是随机绘制的,并且在错误的坐标处 我变了 CLLocationCoordinate2D obh1=CLLocati

我正在努力研究如何在地图上显示线条。我试图从触球开始到触球结束划一条线

现在,我正在获取我触摸的位置坐标,将其添加到阵列中,获取触摸末端的连线,将其添加到阵列中,然后绘制线。下面是我的代码

当我悲伤地运行代码时,我的行没有显示出来。然而,当我坐在那里想我错过了什么,因为我的坐标被保存到阵列中时,我沮丧地不停地滑动屏幕,突然出现了一条线!我关闭了程序,再次做了同样的事情,据我所知,线条是随机绘制的,并且在错误的坐标处

我变了

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

看看我是不是在混音,但同样的事情正在发生。这些线是在随机位置绘制的

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

我做了一些工作,这就是我想到的。当我单击地图时,您看到的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;
}
随机行来自相关代码

本节讨论了主要问题:

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结构,但计数设置为arrayOfLine中的对象数,很可能是两触开始+触结束

polylineWithCoordinates方法需要CLLocationCoordinate2D结构的普通C数组

目前,代码只给该方法一个结构,但计数告诉该方法也使用该结构后面的内存字节作为数组中的坐标。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]];
不直接导致问题的其他几点:

在TouchesBegind中,创建obh1是不必要的。startTouch已经是可以添加到阵列中的CLLocationCoordinate2D。 在touchesEnded中,不需要创建obh。endTouch已经是可以添加到阵列中的CLLocationCoordinate2D。 目前,arrayOfLine在任何时候都没有被清除。这意味着,每当触摸结束时,用户绘制的所有线都会重新绘制并连接在一起。不确定其意图是什么,但如果您只希望绘制当前的开始+结束触摸对,则在触摸开始的顶部,执行[arrayOfLine removeAllObjects];。 确保在地图视图中将userInteractionEnabled设置为“否”,否则它可能会干扰触摸方法,并导致像touchesEnded这样的奇怪行为未被调用。
谢谢你,我已经修正了1,2和4,第3点是经过设计的。如果允许我用你作为一些提示的传声筒,我将不胜感激。我所做的重点是能够在地图上画出指引你到某个地方的路线,因为你知道这条路是弯曲的等等。然而这只画出了直线。我画的是一幅风景画,线条非常完美,因为我可以随意弯曲,但我不能在地图上画。如果我要为某人画方向图,你会推荐一张下面有视图的透明地图吗?那么,对于第3点,如何处理缩放:在这种情况下,您至少应该在添加新覆盖之前删除所有现有覆盖。否则,最终会出现多个重叠覆盖。叠加[1]有第1行,叠加[2]有第1到第2行,叠加[3]有第1到第3行,等等。在添加叠加之前,执行[_mapremoveOverlays:_map.overlays];。不清楚为什么用户必须在地图上画线来指引他们去某个地方。如果您希望线条在弯曲时跟随用户的手指,请尝试更新阵列并替换touchesMoved中的覆盖,而不是ToucheSendd。但是,用户为什么必须绘制以获取方向还不清楚。MKDirections类将为您提供绘制某处方向所需的mkpolyline。研究和尝试更多的东西,然后发布一个新的,详细的问题和代码。假设你去tokoyo,你想知道健身房在哪里或一些不在地图上的小巷子,然后你问别人。我使用tokoyo,但也许我不应该说某个小国家,那里的一切在google places上都不可用。我希望个人能够追踪从您所在位置到所需位置的路线。这是我的初衷
//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]];