2012-10-22 50 views
5

我寫了一個簡單的應用程序,它在MapKit上的兩個位置之間繪製路線。我正在使用Google Map API。我使用了我在網上找到的資源,以下是我用來向Google發送請求的代碼:MapKit - 在地圖放大時使路線沿着街道行進

_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]]; 
    [_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]]; 
    [_httpClient setDefaultHeader:@"Accept" value:@"application/json"]; 

    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; 
    [parameters setObject:[NSString stringWithFormat:@"%f,%f", coordinate.latitude, coordinate.longitude] forKey:@"origin"]; 
    [parameters setObject:[NSString stringWithFormat:@"%f,%f", endCoordinate.latitude, endCoordinate.longitude] forKey:@"destination"]; 
    [parameters setObject:@"true" forKey:@"sensor"]; 

    NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path: @"maps/api/directions/json" parameters:parameters]; 
    request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData; 

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];  
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){ 
     NSInteger statusCode = operation.response.statusCode; 

     if (statusCode == 200) 
     { 
      NSLog(@"Success: %@", operation.responseString); 
     } 
     else 
     { 
      NSLog(@"Status code = %d", statusCode); 
     } 
    } 
             failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
              NSLog(@"Error: %@", operation.responseString); 

             } 
    ]; 

    [_httpClient enqueueHTTPRequestOperation:operation]; 

此功能完美無瑕。當我運行這一點,並試圖說明洛杉磯和芝加哥之間的路線,這裏是它的樣子:

LA - Chicago route zoomed out

BUT。當我縮放地圖到街道級別,這裏的路線的樣子:

LA - Chicago route zoomed in

有誰知道我可以做到,我繪製的路線沿着街道時,地圖縮放?我想要通過街道展示確切的路線。我不知道是否需要將某些其他參數添加到我對Google的請求中。

任何幫助或建議將是偉大的。提前謝謝了!


[編輯#1:添加從谷歌的請求URL和響應]

從上面的代碼中創建操作對象後,我的請求的URL看起來像這樣:

http://maps.googleapis.com/maps/api/directions/json?sensor=true&destination=34%2E052360,-118%2E243560&origin=41%2E903630,-87%2E629790 

只需粘貼您的瀏覽器的網址,您將看到Google發送的JSON數據,作爲我在代碼中獲得的響應。


[編輯#2:解析來自谷歌的答案,建設路徑]

- (void)parseResponse:(NSData *)response 
{ 
    NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil]; 
    NSArray *routes = [dictResponse objectForKey:@"routes"]; 
    NSDictionary *route = [routes lastObject]; 

    if (route) 
    { 
     NSString *overviewPolyline = [[route objectForKey: @"overview_polyline"] objectForKey:@"points"]; 
     _path = [self decodePolyLine:overviewPolyline]; 
    } 
} 

- (NSMutableArray *)decodePolyLine:(NSString *)encodedStr 
{ 
    NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]]; 
    [encoded appendString:encodedStr]; 
    [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\" 
           options:NSLiteralSearch 
            range:NSMakeRange(0, [encoded length])]; 
    NSInteger len = [encoded length]; 
    NSInteger index = 0; 
    NSMutableArray *array = [[NSMutableArray alloc] init]; 
    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]; 
     NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5]; 

     CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]]; 
     [array addObject:location]; 
    } 

    return array; 
} 
+0

我使用此圖書館。這對我有好處。 https://github.com/kadirpekel/MapWithRoutes –

回答

1

它看起來像谷歌是不是給你所有的點,或者你是不是在看所有的點。實際上,我認爲地標之間的多段線不僅僅是像你這樣的地標(用直線)。

  • 在響應檢查DirectionsStatus看到,如果你是有限
  • 提供,谷歌發回的JSON數據。

我不太確定他們是否使用與谷歌使用的完全不同的墨卡託投影。

+0

謝謝你的回答,Cyril。在我的問題中檢查編輯#1,我提供了你在那裏詢問的數據。 – uerceg

+0

在編輯#2中,我添加了解析來自Google的答案並構建路徑的源代碼。我現在嘗試將模式從DRIVING改爲WALKING,有趣的是,儘管Google的JSON響應顯然比DRIVING模式下的響應更大,但我仍然在路徑中獲得205行程的座標,而對於DRIVING,我獲得了203個座標路徑。奇怪。 – uerceg

+0

我很遺憾沒有足夠的方向API知識來進一步幫助你。我認爲一個關鍵要素是文檔中的這個句子:「overview_polyline包含一個對象,其中包含一個代表所得方向的近似(平滑)路徑的編碼點陣列。」嘗試使用短路線的相同請求,看看它是否更精確,或者更好,嘗試像紐約或洛杉磯這樣的真正方形路線,以查看在您請求更近路線時您的路線是否會變得更好。 –