gpt4 book ai didi

ios - 我们如何在 iPhone map View 中将图像添加为折线?

转载 作者:行者123 更新时间:2023-11-29 13:42:08 24 4
gpt4 key购买 nike

我有一张多段线图像,但我想在 map View 中添加此多段线图像。我已经对此进行了研究,尽管努力尝试但没有发现任何相关内容。

这是我的代码

 CGSize polyimagesize = CGSizeMake(768, 1024);

UIGraphicsBeginImageContextWithOptions(polyimagesize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2);
// CGContextSetAlpha(context, 0.6);
MKMapPoint *pointArr = malloc(sizeof(CLLocationCoordinate2D) * segments.count);

for(NSDictionary *route in segments) {

NSString *locations = [route valueForKey:@"Locations"];
double speed = [[route valueForKey:@"Speed"] doubleValue];
NSString *roadNumber = [route valueForKey:@"RoadNumber"];

if ([roadNumber isEqualToString:@"A"] && aRoadFlag == NO) {
continue;
}
else if ([roadNumber isEqualToString:@"N"] && nRoadFlag == NO) {
continue;
}
else if ([roadNumber isEqualToString:@"Others"] && othersRoadFlag == NO) {
continue;
}

if (locations && ([locations length]/16 > 1)) {

UIColor *color;
if (speed <= 20) {
color = [UIColor colorWithRed:222/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
}
else if (speed <= 40) {
color = [UIColor colorWithRed:253/255.0 green:91/255.0 blue:2/255.0 alpha:1.0];
}
else if (speed <= 60) {
color = [UIColor colorWithRed:253/255.0 green:145/255.0 blue:4/255.0 alpha:1.0];
}
else if (speed <=80) {
color = [UIColor colorWithRed:255/255.0 green:212/255.0 blue:4/255.0 alpha:1.0];
}
else if (speed >80) {
color = [UIColor colorWithRed:42/255.0 green:176/255.0 blue:39/255.0 alpha:1.0];
}

CGContextSetStrokeColorWithColor(context, color.CGColor);

for (int i = 0; i <= locations.length - 32; i += 32) {

CLLocationCoordinate2D coordinates;
coordinates.latitude = hexDecode1([locations substringWithRange:NSMakeRange(i, 16)]);
coordinates.longitude = hexDecode1([locations substringWithRange:NSMakeRange(i+16, 16)]);
MKMapPoint point1 = MKMapPointForCoordinate(coordinates);

NSLog(@"coordinates.latitude=%g",coordinates.latitude);

CGPoint point = [mapView convertCoordinate:coordinates toPointToView:self.mapView];

if (i == 0)
{
CGContextMoveToPoint(context, point.x, point.y);
}
else{
CGContextAddLineToPoint(context, point.x, point.y);
}

pointArr[i] = point1;
i++;
}


CGContextStrokePath(context);
}
}


polyimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我可以在上下文中绘制折线图像,然后在我的图像中获取该上下文。但现在我想在 map View 中将此图像添加为折线。

请帮助我,提前致谢。

最佳答案

只需阅读 Apple 文档即可。

无论如何:

//在 C 中分配:

int count = ... // number or points.
CLLocationCoordinate2D* arrayOfPoints =(CLLocationCoordinate2D*)malloc(sizeof(CLLocationCoordinate2D) * (1+count) );
.... // fill a std C array... NOTE the +1, as we must add a closing point equal to the first.

// now add first to close:
arrayOfPoints[count] = arrayOfPoints[0];

MKPolyline * myPolyLine = [MKPolyline polylineWithCoordinates:arrayOfPoints count: count+1];
[myMapView addOverlay: myPolyLine];
free(arrayOfPoints);

...

// You need a call back:

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay
{
MKPolylineView *polylineView = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease];
polylineView.strokeColor = [UIColor greenColor];
polylineView.lineWidth = 12.0;
return polylineView;
}

关于ios - 我们如何在 iPhone map View 中将图像添加为折线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8799574/

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