2010-08-22 80 views
1

我爲iPhone EULA同時閱讀iPhone和Google Map,並希望在我的iPhone應用程序(本機)中實施靜態駕駛方向圖。在iPhone中獲取行車路線

我在iOS 4 SDK的Mapkit中找到一種獲取路徑數據並顯示內置路徑顯示功能的簡單方法。

是否有任何程序員使用Google Map和Bing Map實現這樣的功能?由於Bing Map在SOAP Web服務中提供了路由數據,因此使用Bing服務編程駕駛方向似乎更容易。

回答

1

我找到了解決方案。只需使用一個JSON解析器來了谷歌地圖API

例如:

NSDictionary *testJsondata = [self testJson:GoogleMapXMLDirectionQueryString]; 
    NSLog(@"Here is the title of the response: %@", [testJsondata valueForKey:@"status"]); 

    for (id key in testJsondata) { 

     NSLog(@"key: %@, value: %@", key, [testJsondata objectForKey:key]); 

    } 
} 

- (NSDictionary *) testJson : (NSString*) url 
{ 
    id response = [self objectWithUrl:[NSURL URLWithString:url]]; 

    NSDictionary *feed = (NSDictionary *)response; 
    return feed; 
} 

- (id) objectWithUrl:(NSURL *)url 
{ 
    SBJsonParser *jsonParser = [SBJsonParser new]; 
    NSString *jsonString = [self stringWithUrl:url]; 

    // Parse the JSON into an Object 
    return [jsonParser objectWithString:jsonString error:NULL]; 
} 

- (NSString *)stringWithUrl:(NSURL *)url 
{ 
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url 
               cachePolicy:NSURLRequestReturnCacheDataElseLoad 
              timeoutInterval:30]; 
    // Fetch the JSON response 
    NSData *urlData; 
    NSURLResponse *response; 
    NSError *error; 

    // Make synchronous request 
    urlData = [NSURLConnection sendSynchronousRequest:urlRequest 
            returningResponse: &response 
               error: &error]; 

    // Construct a String around the Data from the response 
    return [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding]; 
} 

- (NSString *)getDirectionInXML:(NSString *)GoogleMapXMLDirectionQueryString 
{ 
    NSError *error; 
    NSURLResponse *response; 
    NSData *dataReply; 
    NSString *stringReply; 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: 
            [NSURL URLWithString: [NSString stringWithFormat:GoogleMapXMLDirectionQueryString]]]; 
    [request setHTTPMethod: @"GET"]; 
    dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    stringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding]; 
    return stringReply; 
}