gpt4 book ai didi

ios - 谷歌地图方向作为 iPhone 上的 map

转载 作者:可可西里 更新时间:2023-11-01 06:10:48 27 4
gpt4 key购买 nike

我正在开发一个 iPhone 应用程序,该应用程序需要在 UIWebView 中显示从一个点 A 到另一个点 B 的步行路线,该 UIWebView 会加载具有相关参数的 Google map 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 将方向显示为 map (如 map 上的线条),如下面链接中的图片所示:

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

但是,当此 URL 加载到 iPhone 上时,路线将显示为列表(请参阅下面的链接)。

How the directions are displayed (directions as a list)

用户需要按顶部栏中的 map 按钮才能以 map 形式查看路线。

如何设置 Google map URL 参数,以便在我的 iPhone 应用程序中直接将路线显示为 map (而不是列表!)

最佳答案

如果你使用 MKMapView,这会容易得多。检查 nvpolyline来自github的项目。它将帮助您在点 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);

这将为您提供方向字符串数组,您可以使用此字符串显示项目中的任何位置。

在我的项目中,我创建了一个显示在 map 顶部的小 View ,在该 View 中我正在一个一个地显示方向。

关于ios - 谷歌地图方向作为 iPhone 上的 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14684512/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com