2012-08-06 4 views
2

Je voudrais tracer une POLY-LINE entre deux ou plusieurs coordonnées données sur la carte (poly-ligne et non la route). Imaginez que j'ai deux épingles sur la carte et que je dois tracer une ligne droite entre la première épingle tombée et la seconde épingle.Tracer une polyligne entre deux coordonnées dans Xcode

Répondre

8

Dans le fichier d'interface

MKPolyline* _routeLine; 
MKPolylineView* _routeLineView; 

Dans le fichier de mise en œuvre

magasin toutes les coordonnées dans

NSMutablrArray *routeLatitudes 

puis

MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * [routeLatitudes count]); 
for(int idx = 0; idx < [routeLatitudes count]; idx++) 
{ 
    CLLocationCoordinate2D workingCoordinate;  
    workingCoordinate.latitude=[[routeLatitudes objectAtIndex:idx] doubleValue]; 
    workingCoordinate.longitude=[[routeLongitudes objectAtIndex:idx] doubleValue]; 
    MKMapPoint point = MKMapPointForCoordinate(workingCoordinate); 
    pointArr[idx] = point;  
} 
// create the polyline based on the array of points. 
routeLine = [MKPolyline polylineWithPoints:pointArr count:[routeLatitudes count]]; 
[mapViewHome addOverlay:self.routeLine]; 
free(pointArr); 

et délégué Overlay

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

    if(overlay == routeLine) 
    { 
     routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease]; 
     routeLineView.fillColor = [UIColor colorWithRed:0.945 green:0.027 blue:0.957 alpha:1]; 
     routeLineView.strokeColor = [UIColor colorWithRed:0.945 green:0.027 blue:0.957 alpha:1]; 
     routeLineView.lineWidth = 4; 

     overlayView = routeLineView; 
    } 
    return overlayView; 
} 

Hope this helps

Modifié code

ici est le code pour obtenir NSMutableArray de coordonnées.

appeler cette fonction

NSString * saddr = [NSString stringWithFormat:@"%f,%f",StartCoordinate.latitude, StartCoordinate.longitude]; 
NSString* daddr = [NSString stringWithFormat:@"%f,%f",EndCoordinate.latitude, EndCoordinate.longitude]; 
routeLatitudes=[[[self getDirectionRoutesFrom:[saddr copy] to:[daddr mutableCopy]] mutableCopy] retain]; 

définition de la fonction

-(NSMutableArray *)getDirectionRoutesFrom:(NSString *)saddr1 to:(NSString *)daddr 
{ 
NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr1, daddr]; 
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];  
//NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl]; 
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding error:nil]; 
NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L]; 
//NSMutableArray *temparr=[[MapViewController decodePolyLine:[encodedPoints mutableCopy]] retain]; 
return [[self decodePolyLine:[encodedPoints mutableCopy]] retain]; 
//return temparr; 
} 

et

-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded { 
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\" 
          options:NSLiteralSearch 
           range:NSMakeRange(0,  [encoded length])]; 
NSInteger len = [encoded length]; 
NSInteger index = 0; 
NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease]; 
NSInteger lat=0; 
NSInteger lng=0; 
while (index < len) { 
    NSInteger b; 
    NSInteger shift = 0; 
    NSInteger result = 0; 
    do { 
     b = [encoded characterAtIndex:index++] - 63; 
     result |= (b & 0x1f) << shift; 
     shift += 5; 
    } while (b >= 0x20); 
    NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1)); 
    lat += dlat; 
    shift = 0; 
    result = 0; 
    do { 
     b = [encoded characterAtIndex:index++] - 63; 
     result |= (b & 0x1f) << shift; 
     shift += 5; 
    } while (b >= 0x20); 
    NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1)); 
    lng += dlng; 
    NSNumber *latitude = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease]; 
    NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease]; 
    printf("[%f,", [latitude doubleValue]); 
    printf("%f]", [longitude doubleValue]); 
    CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease]; 
    [array addObject:loc]; 
} 

return array; 
} 

comprennent RegexKitLite.h dans votre dossier.

+0

Merci Mahendra pour votre aide, mais a écrit votre code, après avoir rempli le tableau. mais maintenant je reçois et erreur "Fil 1: signal sigabrt" ce qui est l'erreur? – fadd

+0

Définir la propriété pour les variables routeLineView et routeLine et utiliser avec self. self.routeLine et self.routeLineView. – Mahendra

+0

Mahendara, merci encore. J'ai juste compris que je remplissais NSMutablrArray * routeLatitudes incorrectement. Voulez-vous s'il vous plaît pour m'aider avec comment stocker toutes les coordonnées dans NSMutablrArray. – fadd

Questions connexes