gpt4 book ai didi

iphone - ios map 问题 - ios map 上显示多条线

转载 作者:行者123 更新时间:2023-12-03 21:15:39 25 4
gpt4 key购买 nike

我正在使用 ios map 。在这里,当我使用覆盖层在 ios map 上画一条线时,它显示多条线。现在我只想画一条直线。我怎样才能做到这一点。下面我附上了带有屏幕截图的代码。

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

if ([touches count]>1)
{
[super touchesBegan:touches withEvent:event];
return;
}
UITouch *touch = [touches anyObject];
tappedPoint1 = [touch locationInView:_mapView];
// _startCoord = [_mapView convertPoint:tappedPoint1 toCoordinateFromView:_mapView];
coord1= [_mapView convertPoint:tappedPoint1 toCoordinateFromView:_mapView];
polyline = nil;
}

- (void)updatePolylineWithCoordinate:(CLLocationCoordinate2D)coord6
{
if (!_polyline)
{
MKMapPoint points[2];
points[0] = MKMapPointForCoordinate(coord1);
points[1] = MKMapPointForCoordinate(coord6);
line2 = [MKPolyline polylineWithPoints:points count:2];
}
else
{
NSUInteger count = _polyline.pointCount + 1;
// create new point array and add new coord
CLLocationCoordinate2D coords[count];
[_polyline getCoordinates:coords range:NSMakeRange(0, count-1)];
coords[count - 1] = coord6;
[_mapView removeOverlay:_polyline];
line2 = [MKPolyline polylineWithCoordinates:coords count:count];
}
line2.title = @"line";
[_mapView addOverlay: line2];

// _polyline = line2;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:_mapView];
CLLocationCoordinate2D coord5= [_mapView convertPoint:currentPoint toCoordinateFromView:_mapView];
NSLog(@"_startCoord.latitude : %f", coord5.latitude);
NSLog(@"_startCoord.lontitude : %f", coord5.longitude);
[self updatePolylineWithCoordinate:coord5];
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]])
{
lineview=[[[MKPolylineView alloc] initWithOverlay:overlay] autorelease];
lineview.strokeColor=[[UIColor blueColor] colorWithAlphaComponent:0.5];
lineview.fillColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
lineview.lineWidth=2.0;
return [lineview autorelease];
}

return nil;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[_mapView removeOverlay:line2];

MKMapPoint points[2];
points[0] = MKMapPointForCoordinate(coord1);
points[1] = MKMapPointForCoordinate(coord5);
line2 = [MKPolyline polylineWithPoints:points count:2];
[_mapView addOverlay: line2];
}

现在,当我使用触摸移动画一条线时,它会在 map 上的任何地方绘制,具体取决于 ios map 。但我需要当用户移动手指时只显示一条直线。我该怎么做:请看这个屏幕截图。 enter image description here

任何人都可以帮我解决这个问题吗?

最佳答案

试试这个:

- (void)updatePolylineWithCoordinate:(CLLocationCoordinate2D)coord
{
MKPolyline* line;
if (!_polyline) {
MKMapPoint points[2];
points[0] = MKMapPointForCoordinate(_startCoord);
points[1] = MKMapPointForCoordinate(coord);
line = [MKPolyline polylineWithPoints:points count:2];
} else {
NSUInteger count = _polyline.pointCount + 1;
// create new point array and add new coord
CLLocationCoordinate2D coords[count];
[_polyline getCoordinates:coords range:NSMakeRange(0, count-1)];
coords[count - 1] = coord;
[_mapView removeOverlay:_polyline];
line = [MKPolyline polylineWithCoordinates:coords count:count];
}
line.title = @"line";
[_mapView addOverlay: line];
_polyline = line;


}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count]>1) {
[super touchesBegan:touches withEvent:event];
return;
}
UITouch *touch = [touches anyObject];
CGPoint tappedPoint1 = [touch locationInView:_mapView];
_startCoord = [_mapView convertPoint:tappedPoint1 toCoordinateFromView:_mapView];

DDAnnotation *annotation = [[[DDAnnotation alloc] initWithCoordinate:theCoordinate addressDictionary:nil] autorelease];
annotation.subtitle = [NSString stringWithFormat:@"%f,%f", annotation.coordinate.latitude, annotation.coordinate.longitude];
[_mapView addAnnotation:annotation];

_polyline = nil;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:_mapView];
CLLocationCoordinate2D coord= [_mapView convertPoint:currentPoint toCoordinateFromView:_mapView];
[self updatePolylineWithCoordinate:coord];
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineView* lineview= [[MKPolylineView alloc] initWithOverlay:overlay];
lineview.strokeColor=[[UIColor blueColor] colorWithAlphaComponent:0.5];
lineview.fillColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
lineview.lineWidth=2.0;
return [lineview autorelease];
}
return nil;
}

您只需要添加 2 个实例变量:

  • CLLocationCooperative2D _startCoord;
  • MKPolyline* _polyline;

关于iphone - ios map 问题 - ios map 上显示多条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15471391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com