2012-08-06 94 views
2

我想在地圖上的兩個或多個給定座標之間繪製多邊形線(多邊線而不是路線)。想象一下,我在地圖上有2個放下的針腳,我需要畫出一條從第一個放下的針腳到第二個放下的針腳的直線。在Xcode中的兩個座標之間繪製多段線

回答

8

在接口文件

MKPolyline* _routeLine; 
MKPolylineView* _routeLineView; 

在實現文件

存儲所有的座標

NSMutablrArray *routeLatitudes 

然後

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); 

和覆蓋委託

- (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; 
} 

希望這有助於

編輯代碼

這裏是代碼來獲得座標的NSMutableArray。

調用這個函數

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]; 

函數定義

-(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; 
} 

-(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; 
} 

包括RegexKitLite.h在您的文件。

+0

感謝Mahendra的幫助, 但寫完你的代碼後,我填充數組。但現在我收到和錯誤「線程1:信號sigabrt」 什麼是錯誤? – fadd 2012-08-07 11:07:54

+0

爲routeLineView和routeLine變量設置屬性,並與self一起使用。 self.routeLine和self.routeLineView。 – Mahendra 2012-08-08 00:55:49

+0

Mahendara,再次感謝。我只是想通了,我正在填補NSMutablrArray * routeLatitudes不正確。請你幫我解決如何在NSMutablrArray中存儲所有的座標。 – fadd 2012-08-10 10:02:55

相關問題