在iPhone上将谷歌地图方向作为地图显示

4
我是一名有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:

我正在开发一个iPhone应用程序,需要在UIWebView中显示从点A到另一个点B的步行路线,该UIWebView加载带有相关参数的Google Maps URL。

我正在加载的URL示例:

[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://maps.google.com/maps?saddr=55.720923,12.513428&daddr=55.745666,12.546387"]]];

我需要 UIWebView 以地图的方式展示方向(作为地图上的线条),如下面链接中所示的图像: 我想要的方向展示方式(方向作为地图) 然而,在 iPhone 上加载此 URL 时,方向实际上是以列表的形式显示的(请参见下面的链接)。 方向的显示方式(方向作为列表) 用户需要按顶部栏中的地图按钮才能将方向作为地图查看。
我如何设置 Google 地图 URL 参数,以便在我的 iPhone 应用程序中直接将方向显示为地图(而不是列表)?

我有完全相同的问题。你解决了这个问题吗? - hoya21
2个回答

1

如果您使用来自github的MKMapView.check nvpolyline项目,这将使事情变得更加容易。它将帮助您在点A和点B之间创建线条,从而解决了您的第一个问题。

对于第二个问题,请将此代码添加到您的项目中。

NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false", saddr, daddr];
    //&alternatives=true

    NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];

    NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl];

    SBJsonParser *jsonParser = [SBJsonParser new];
    NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:apiResponse error:nil];

    NSLog(@"apiResponse is : %@",jsonData);

    //to display route in drop down

    NSArray *routeArray = [[NSArray alloc]init];
    routeArray= [jsonData objectForKey:@"routes"];

    for(int i=0;i<[routeArray count];i++)
    {
        NSDictionary *tempDictionary = [routeArray objectAtIndex:i];
        if([tempDictionary objectForKey:@"overview_polyline"]!=nil)
        {
            NSDictionary *secTempDictionary = [tempDictionary objectForKey:@"overview_polyline"];
            if([secTempDictionary objectForKey:@"points"]!=nil)
            {
                NSString * routePoint =[secTempDictionary objectForKey:@"points"];

                [routeSetAry addObject:routePoint];

                encodedPoints = [secTempDictionary objectForKey:@"points"];
            }
            // NSLog(@"secTempDictionary is: %@", secTempDictionary);
        } 
        if([tempDictionary objectForKey:@"legs"]!=nil)
        {
            NSArray *lagArray = [[NSArray alloc]init];
            lagArray= [tempDictionary objectForKey:@"legs"];

            for(int i=0;i<[lagArray count];i++)
            {
                NSDictionary *thirdTempDictionary = [lagArray objectAtIndex:i];
                if([thirdTempDictionary objectForKey:@"steps"]!=nil)
                {
                    NSArray *stepsArray = [[NSArray alloc]init];
                    stepsArray= [thirdTempDictionary objectForKey:@"steps"];

                    for(int i=0;i<[stepsArray count];i++)
                    {
                        NSDictionary *forthTempDictionary = [stepsArray objectAtIndex:i];

                        if([forthTempDictionary objectForKey:@"html_instructions"]!=nil)
                        {
                            NSString * directionStr =[forthTempDictionary objectForKey:@"html_instructions"];

                            NSRange range;
                            while ((range = [directionStr rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound){
                                directionStr=[directionStr stringByReplacingCharactersInRange:range withString:@""];
                            }
                            [directionStrAry addObject:directionStr];
                        }

                        NSDictionary *fifthTempDictionary = [forthTempDictionary objectForKey:@"polyline"];
                        if([fifthTempDictionary objectForKey:@"points"]!=nil)
                        {
                            NSString * routePoint =[fifthTempDictionary objectForKey:@"points"];

                            [polylineSetAry addObject:routePoint];

                           // encodedPoints = [fifthTempDictionary objectForKey:@"points"];
                        }
                        NSDictionary *sixthTempDictionary =[forthTempDictionary objectForKey:@"distance"];
                        if([sixthTempDictionary objectForKey:@"text"]!=nil)
                        {
                            NSString * distanceStr =[sixthTempDictionary objectForKey:@"text"];

                            [distanceStrAry addObject:distanceStr];

                            // encodedPoints = [fifthTempDictionary objectForKey:@"points"];
                        } 
                    }
                }
            }
        }  
    }

    NSLog(@"routeSetAry is :%@",routeSetAry);
 NSLog(@"polylineSetAry is : %i",polylineSetAry.count);

这将为您提供方向字符串数组,您可以使用此字符串在项目中的任何位置显示。
在我的项目中,我创建了一个小视图,在地图顶部显示,并在该视图中逐个显示方向。

它将在iOS 5.0.1之前加载谷歌地图。最新的iOS版本使用苹果地图数据。 - Saad Masood

0

我不确定,但你应该参考这个链接,谷歌已经很好地解释了"Google Maps URL Scheme"

你可以使用

"comgooglemaps://?saddr=Google+Inc,+8th+Avenue,+New+York,+NY&daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York&directionsmode=transit"

它有不同的参数,如saddrdaddrdirectionsmode

希望这可以帮到你!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接