нарисовать путь на карте в iphone - PullRequest
5 голосов
/ 26 апреля 2009

Я использую библиотеку Route-Me для iPhone. Моя проблема в том, что я хочу нарисовать путь на карте, например.

Я нахожусь в Далласе и хочу поехать в Нью-Йорк, тогда я просто поставлю маркер на эти два места, и между этими двумя маркерами будет проложен путь.

Может ли кто-нибудь предложить мне, как это можно сделать.

Если есть любая другая карта, а не RouteMe, тогда тоже все в порядке.

Ответы [ 4 ]

7 голосов
/ 21 июня 2010

Следующий код нарисует путь между 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;
4 голосов
/ 07 ноября 2009

Route-me имеет класс RMPath для этой цели. Ты играл с этим? Если да, что вы делали, что делали, а что не работали?

3 голосов
/ 14 мая 2012

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) вместо addLineToLatLong.

Это более новый, найденный в MapTestBed, например, в Route-me

- (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;
}
0 голосов
/ 27 апреля 2009

Извините, я не видел ничего подобного - я бы только отметил, что если люди будут голосовать за вас, потому что они думают, что вы говорите о новых функциях сопоставления SDK, снова прочитайте ваш вопрос, где вы говорите о треть библиотека для вечеринок (все еще возможно, потому что вы не можете делать пошаговые указания с SDK официальной карты).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...