在iphone上绘制地图上的路径

时间:2009-04-26 05:49:51

标签: iphone objective-c

我正在使用iPhone的Route-Me库。我的问题是我想在地图上画一条路径,例如。

我在达拉斯,我想去纽约,然后我会在这两个地方放置标记,并在这两个标记之间绘制路径。

任何人都可以建议我如何做到这一点。

如果有任何其他Map而不是RouteMe,那么也没关系。

4 个答案:

答案 0 :(得分:7)

以下代码将绘制2点之间的路径。 (在您的情况下,您必须添加所有路线点)

// Set map view center coordinate
CLLocationCoordinate2D center;
center.latitude = 47.582;
center.longitude = -122.333;
slideLocation = center;
[mapView.contents moveToLatLong:center];
[mapView.contents setZoom:17.0f];

// Add 2 markers(start/end)  and RMPath with 2 points
RMMarker *newMarker;
UIImage *startImage = [UIImage imageNamed:@"marker-blue.png"];
UIImage *finishImage = [UIImage imageNamed:@"marker-red.png"];
UIColor* routeColor = [[UIColor alloc] initWithRed:(27.0 /255) green:(88.0 /255) blue:(156.0 /255) alpha:0.75];
RMPath* routePath = [[RMPath alloc] initWithContents:mapView.contents];
[routePath setLineColor:routeColor];
[routePath setFillColor:routeColor];
[routePath setLineWidth:10.0f];
[routePath setDrawingMode:kCGPathStroke];
CLLocationCoordinate2D newLocation;
newLocation.latitude = 47.580;
newLocation.longitude = -122.333;   
[routePath addLineToLatLong:newLocation];
newLocation.latitude = 47.599;
newLocation.longitude = -122.333;   
[routePath addLineToLatLong:newLocation];
[[mapView.contents overlay] addSublayer:routePath];

newLocation.latitude = 47.580;
newLocation.longitude = -122.333;   
newMarker = [[RMMarker alloc] initWithUIImage:startImage anchorPoint:CGPointMake(0.5, 1.0)];
[mapView.contents.markerManager addMarker:newMarker AtLatLong:newLocation];
[newMarker release];
newMarker = nil;

newLocation.latitude = 47.599;
newLocation.longitude = -122.333;   
newMarker = [[RMMarker alloc] initWithUIImage:finishImage anchorPoint:CGPointMake(0.5, 1.0)];
[mapView.contents.markerManager addMarker:newMarker AtLatLong:newLocation];
[newMarker release];
newMarker = nil;

答案 1 :(得分:4)

为此,Route-me有一个RMPath类。你玩过吗?如果是这样,你做了什么,做了什么,做了什么不起作用?

答案 2 :(得分:3)

路线 - 如何在地图上绘制路径,例如,不完全但是非常接近。

使用RMPath在叠加层上绘制多边形

//polygonArray is a NSMutableArray of CLLocation
- (RMPath*)addLayerForPolygon:(NSMutableArray*)polygonArray toMap:(RMMapView*)map {
    RMPath* polygonPath = [[[RMPath alloc] initForMap:map] autorelease];
    [polygonPath setLineColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:0.5]];
    [polygonPath setFillColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:0.5]];
    [polygonPath setLineWidth:1];

    BOOL firstPoint = YES;
    for (CLLocation* loc in polygonArray) {
        if (firstPoint) {
            [polygonPath moveToLatLong:loc.coordinate];
            firstPoint = NO;
        } else {
            [polygonPath addLineToLatLong:loc.coordinate];
        } 
    }

    [polygonPath closePath];

    polygonPath.zPosition = -2.0f;

    NSMutableArray *sublayers = [[[[mapView contents] overlay] sublayers] mutableCopy];
    [sublayers insertObject:polygonPath atIndex:0];
    [[[mapView contents] overlay] setSublayers:sublayers];
    return polygonPath;
}

注意:

最新的RouteMe有addLineToCoordinate :( CLLocationCoordinate2D)坐标而不是addLineToLatLong。

这是在Route-me

中的MapTestBed示例中找到的新版本
- (RMMapLayer *)mapView:(RMMapView *)aMapView layerForAnnotation:(RMAnnotation *)annotation
{
    if ([annotation.annotationType isEqualToString:@"path"]) {
        RMPath *testPath = [[[RMPath alloc] initWithView:aMapView] autorelease];
        [testPath setLineColor:[annotation.userInfo objectForKey:@"lineColor"]];
        [testPath setFillColor:[annotation.userInfo objectForKey:@"fillColor"]];
        [testPath setLineWidth:[[annotation.userInfo objectForKey:@"lineWidth"] floatValue]];

        CGPathDrawingMode drawingMode = kCGPathStroke;
        if ([annotation.userInfo containsObject:@"pathDrawingMode"])
            drawingMode = [[annotation.userInfo objectForKey:@"pathDrawingMode"] intValue];
        [testPath setDrawingMode:drawingMode];

        if ([[annotation.userInfo objectForKey:@"closePath"] boolValue])
            [testPath closePath];

        for (CLLocation *location in [annotation.userInfo objectForKey:@"linePoints"])
        {
            [testPath addLineToCoordinate:location.coordinate];
        }

        return testPath;
    }
    if ([annotation.annotationType isEqualToString:@"marker"]) {
        return [[[RMMarker alloc] initWithUIImage:annotation.annotationIcon anchorPoint:annotation.anchorPoint] autorelease];
    }

    return nil;
}

答案 3 :(得分:0)

对不起,我没有看到这样的事情 - 我只会注意到,如果人们因为他们认为你在谈论新的SDK映射功能而投票给你,那么在你谈论第三个问题时再次阅读你的问题派对图书馆(仍然可能是有效的需求,因为您无法使用官方地图SDK进行转弯指示)。