ios – Google地图在iPhone上的地图方向

ios – Google地图在iPhone上的地图方向,第1张

概述我正在研究一个iPhone应用程序,它需要在UIWebView中显示从A点到另一点B的步行路线,该UIWebView会加载带有相关参数的Google Maps URL. 我正在加载的URL示例: [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://maps.google.com 我正在研究一个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将方向显示为地图(如地图上的线条),如下图链接所示:

How I want the directions to be displayed (directions as a map)

但是,当在iPhone上加载此URL时,方向将显示为列表(请参阅下面的链接).

How the directions are displayed (directions as a list)

用户需要按顶部栏中的地图按钮才能将路线视为地图.

如何设置Google地图网址参数,以便我的iPhone应用中的路线直接显示为地图(而不是列表!)?

解决方法@H_502_29@ 如果您从github使用MKMapVIEw.check nvpolyline项目,这会容易得多.它将帮助您在点a到pont 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 – Google地图在iPhone上的地图方向全部内容,希望文章能够帮你解决ios – Google地图在iPhone上的地图方向所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1019833.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存