0

我正在使用坐标从地图上的 A 点到 B 点画一条直线,MKOverlayView 这很好,但我现在需要重复同一条线UIBezierPath

我可以以某种方式将地图坐标值转换为屏幕坐标值吗?

这是我到目前为止所拥有的

#define EARTH_RADIUS 6371

@implementation ViewController
@synthesize mapView = _mapView;
@synthesize routeLine = _routeLine;
@synthesize routeLineView = _routeLineView;



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    // create the overlay
    [self loadRoute];

    // add the overlay to the map
    if (nil != self.routeLine) {
        [self.mapView addOverlay:self.routeLine];
    }

    // zoom in on the route.
    [self zoomInOnRoute];

}

// creates the route (MKPolyline) overlay
-(void) loadRoute
{
    NSString* fileContents = @"41.0311,21.3403 42.0012,21.4167";
    NSArray* pointStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


    // while we create the route points, we will also be calculating the bounding box of our route
    // so we can easily zoom in on it.
    MKMapPoint northEastPoint;
    MKMapPoint southWestPoint;

    // create a c array of points.
    MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * pointStrings.count);


    NSMutableArray *cgPoint = [NSMutableArray array];

    for(int idx = 0; idx < pointStrings.count; idx++)
    {
        // break the string down even further to latitude and longitude fields.
        NSString* currentPointString = [pointStrings objectAtIndex:idx];
        NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];

        CLLocationDegrees latitude  = [[latLonArr objectAtIndex:0] doubleValue];
        CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue];


        // create our coordinate and add it to the correct spot in the array
        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);

        CGPoint latLong = CGPointFromString([NSString stringWithFormat:@"{%@}",currentPointString]);

        [cgPoint addObject:[NSValue valueWithCGPoint:latLong]];

        MKMapPoint point = MKMapPointForCoordinate(coordinate);

        // if it is the first point, just use them, since we have nothing to compare to yet.
        if (idx == 0) {
            northEastPoint = point;
            southWestPoint = point;
        }
        else
        {
            if (point.x > northEastPoint.x)
                northEastPoint.x = point.x;
            if(point.y > northEastPoint.y)
                northEastPoint.y = point.y;
            if (point.x < southWestPoint.x)
                southWestPoint.x = point.x;
            if (point.y < southWestPoint.y)
                southWestPoint.y = point.y;
        }

        pointArr[idx] = point;


    }

    CGPoint point = [self convertLatLongCoord:[[cgPoint objectAtIndex:0] CGPointValue]];
    CGPoint point1 = [self convertLatLongCoord:[[cgPoint objectAtIndex:1] CGPointValue]];

    // create the polyline based on the array of points.
    self.routeLine = [MKPolyline polylineWithPoints:pointArr count:pointStrings.count];


    _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);

    // clear the memory allocated earlier for the points
    free(pointArr);

    UIView *invisibleView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    invisibleView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:invisibleView];
    //  [self drawLine];
    [invisibleView setUserInteractionEnabled:NO];


    UIBezierPath *linePath = [UIBezierPath bezierPath];
    [linePath moveToPoint:point1]; 
    [linePath addLineToPoint:point];

    //shape layer for the line
    CAShapeLayer *line = [CAShapeLayer layer];
    line.path = [linePath CGPath];
    line.strokeColor = [[UIColor whiteColor] CGColor];
    line.lineWidth = 5;

    [invisibleView.layer addSublayer:line];

    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration =8.0;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [line addAnimation:pathAnimation forKey:@"strokeEnd"];

}
- (CGPoint)convertLatLongCoord:(CGPoint)latLong {

    CGSize screenSize = [UIScreen mainScreen].applicationFrame.size;
    CGFloat SCALE = MIN(screenSize.width, screenSize.height) / (2.0 * EARTH_RADIUS);
    CGFloat OFFSET = MIN(screenSize.width, screenSize.height) / 2.0;

    CGFloat x = EARTH_RADIUS * cos(latLong.x) * cos(latLong.y) * SCALE + OFFSET;
    CGFloat y = EARTH_RADIUS * cos(latLong.x) * sin(latLong.y) * SCALE + OFFSET;

    return CGPointMake(x, y);
}
-(void) zoomInOnRoute
{

    [self.mapView setVisibleMapRect:_routeRect];
    //[self.mapView setUserInteractionEnabled:NO];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

}


#pragma mark MKMapViewDelegate
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKOverlayView* overlayView = nil;

    if(overlay == self.routeLine)
    {
        //if we have not yet created an overlay view for this overlay, create it now.
        if(nil == self.routeLineView)
        {
            self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
            self.routeLineView.fillColor = [UIColor whiteColor];
            self.routeLineView.strokeColor = [UIColor blueColor];
            self.routeLineView.lineWidth = 0.5;
        }

        overlayView = self.routeLineView;

    }

    return overlayView;

}

这就是它的外观:

在此处输入图像描述

我需要那条白线越过蓝线,任何帮助将不胜感激

4

0 回答 0