【问题标题】:draw path on map in iphone在 iphone 的地图上绘制路径
【发布时间】:2009-04-26 05:49:51
【问题描述】:

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

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

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

如果有其他 Map 而不是 RouteMe 也可以。

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    以下代码将在 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;
    

    【讨论】:

      【解决方案2】:

      Route-me 有一个 RMPath 用于此目的的类。你玩过它吗?如果是这样,你做了什么,做了什么,什么没用?

      【讨论】:

        【解决方案3】:

        Route-Me 例如如何在地图上画一条路径,不完全但是很接近。

        使用 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)coordinate而不是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;
        }
        

        【讨论】:

          【解决方案4】:

          抱歉,我没有见过这样的事情 - 我只想指出,如果人们因为他们认为您在谈论新的 SDK 映射功能而对您投反对票,请再次阅读您在谈论第三个问题的问题派对库(仍然可能是一个有效的需求,因为您无法使用官方地图 SDK 进行转弯指示)。

          【讨论】:

            猜你喜欢
            • 2016-10-29
            • 2011-08-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-09-05
            • 2017-10-03
            相关资源
            最近更新 更多