2012-05-31 40 views
0

我想使用折線在兩點之間繪製路線。我有些路線,但不正確,我認爲它會在轉彎時感到困惑。我使用谷歌API獲取路線點。以下是代碼我試過請檢查。在iPhone中繪製路線部分沒有得到所需的輸出MapView

- (IBAction)onclickDrawButton:(id)sender 

{ 
    flag=TRUE; 
    NSString *startpoint=[satrtTextfield text];  
    NSString *endpoint=[endTextfield text]; 

    NSMutableString *urlString=[NSMutableString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false",startpoint,endpoint]; 
    NSURL *url = [NSURL URLWithString:urlString]; 


    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; 
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request 
    delegate:self]; 
    if(connection) 
    { 
     NSLog(@"connectin done"); 
    } 

}

-(void)connection:(NSURLConnection*)connection didReceiveResponse: (NSURLResponse*)response 
{  
if(flag) 
{ 
    recievedRoutes=[[NSMutableData alloc]init]; 

} 

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    if(flag){ 
      NSString *jsonResult = [[NSString alloc] initWithData:recievedRoutes encoding:NSUTF8StringEncoding]; 
    NSLog(@"json response %@",jsonResult); 

    NSDictionary *partialJsonDict=[jsonResult JSONValue]; 
    NSArray *items=[partialJsonDict valueForKey:@"routes"]; 

    //NSLog(@"responsed valued %@",[[items objectAtIndex:0]valueForKey:@"legs"]); 


    NSArray *aary=[[items objectAtIndex:0]valueForKey:@"legs"]; 
    NSLog(@"legs array wuth polyline %@",[[aary objectAtIndex:0]valueForKey:@"steps"]); 
    NSArray *steps=[[aary objectAtIndex:0]valueForKey:@"steps"]; 
    NSLog(@"steps %@",[[steps objectAtIndex:1]objectForKey:@"polyline"]); 
    NSMutableString *string=[[NSMutableString alloc]init]; 
    for(int i=0;i<[steps count];i++) 
    { 
     //NSLog(@"steps i value %@",[[[steps objectAtIndex:i]objectForKey:@"polyline"]objectForKey:@"sttpoints"]); 

     [string appendString:[[[steps objectAtIndex:i]objectForKey:@"polyline"]objectForKey:@"points"]]; 
    } 
    NSLog(@"final %@",string); 

    MKPolyline *polyline=[self polylineWithEncodedString:string]; 
    [mapView addOverlay:polyline]; 
    [self zoomToFitMapAnnotations:mapView]; 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    if(flag){ 
    [recievedRoutes appendData:data]; 
    } 

}

//解碼折線和retrives coordintes我已經使用以下方法

-(MKPolyline *)polylineWithEncodedString:(NSString *)encodedString { 

const char *bytes = [encodedString UTF8String]; 
NSUInteger length = [encodedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; 
NSUInteger idx = 0; 

NSUInteger count = length/4; 
CLLocationCoordinate2D *coords = calloc(count, sizeof(CLLocationCoordinate2D)); 
NSUInteger coordIdx = 0; 

float latitude = 0; 
float longitude = 0; 
while (idx < length) { 
    char byte = 0; 
    int res = 0; 
    char shift = 0; 

    do { 
     byte = bytes[idx++] - 63; 
     res |= (byte & 0x1F) << shift; 
     shift += 5; 
    } while (byte >= 0x20); 

    float deltaLat = ((res & 1) ? ~(res >> 1) : (res >> 1)); 
    latitude += deltaLat; 

    shift = 0; 
    res = 0; 

    do { 
     byte = bytes[idx++] - 0x3F; 
     res |= (byte & 0x1F) << shift; 
     shift += 5; 
    } while (byte >= 0x20); 

    float deltaLon = ((res & 1) ? ~(res >> 1) : (res >> 1)); 
    longitude += deltaLon; 

    float finalLat = latitude * 1E-5; 
    float finalLon = longitude * 1E-5; 

    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(finalLat, finalLon); 
    coords[coordIdx++] = coord; 
    NSLog(@"in encoding %f %f ",latitude,longitude);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
    if (coordIdx == count) { 
     NSUInteger newCount = count + 10; 
     coords = realloc(coords, newCount * sizeof(CLLocationCoordinate2D)); 
     count = newCount; 
    } 
} 

MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coords count:coordIdx]; 
free(coords); 

return polyline; 
    } 

//繪製實際路線

- (MKOverlayView *)mapView:(MKMapView *)mapView 
     viewForOverlay:(id<MKOverlay>)overlay { 
MKPolylineView *overlayView = [[MKPolylineView alloc] initWithOverlay:overlay]; 
overlayView.lineWidth = 2; 
overlayView.strokeColor = [UIColor purpleColor]; 
overlayView.fillColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1f]; 
return overlayView; 

}

enter image description here

enter image description here

+1

我建議你看看你從你的路線得到的觀點,你可能會得到一個奇怪的觀點。這是發生在你映射的每條路線上,還是僅僅出現在這種情況下? –

+0

每條路線都不會發生這種情況。 –

+0

謝謝艾倫,因爲你建議我沒有得到正確的道路點。實際上,我正在嘗試編碼折線,並將其附加到以前的輪詢,如製作單折線字符串,並將此字符串傳遞給polylineWithEncodedString方法,以便不會得到準確的點。現在爲每條多段線調用此編碼方法,併爲每個點添加疊加方法。這對我有用...... :) –

回答

0

你行之後

NSArray *steps=[[aary objectAtIndex:0]valueForKey:@"steps"]; 

替換該行可能工作

NSMutableArray *polyLinesArray = [[NSMutableArray alloc] init]; 

for (int i = 0; i < [steps count]; i++) 
{ 
    NSString* encodedPoints = [[[steps objectAtIndex:i] objectForKey:@"polyline"] valueForKey:@"points"]; 
    MKPolyline *route = [self polylineWithEncodedString:encodedPoints]; 
    [polyLinesArray addObject:route]; 
} 

[self.mapView addOverlays:polyLinesArray]; 
[polyLinesArray release];