【问题标题】:Display google navigation based on given locations根据给定位置显示谷歌导航
【发布时间】:2017-07-05 11:33:53
【问题描述】:

我正在研究谷歌地图集成我正在尝试显示谷歌导航目前我正在使用以下代码。我正在尝试根据我通过 api 的位置显示像谷歌导航一样

NSURL *url=[[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&alternateroutes=false&sensor=true" ,12.920631,77.670494,8.3075,77.2218]];
NSURLResponse *res;
NSError *err;NSURL *url=[[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&alternateroutes=false&sensor=true" ,12.920631,77.670494,8.3075,77.2218]];
NSURLResponse *res;
NSError *err;

NSData *data=[NSURLConnection sendSynchronousRequest:[[NSURLRequest alloc] initWithURL:url] returningResponse:&res error:&err];
if(data)
{
    NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    if(dic)
    {
            NSArray *routes=dic[@"routes"];
            NSArray *legs=routes[0][@"legs"];
            NSArray *steps=legs[0][@"steps"];
            NSLog(@"the legs are:%@",steps);
            NSMutableArray *textsteps=[[NSMutableArray alloc] init];
            NSMutableArray *latlong=[[NSMutableArray alloc]init];
            for(int i=0; i< [steps count]; i++){
                NSString *html=steps[i][@"html_instructions"];
                [latlong addObject:steps[i][@"end_location"]];
                NSLog(@"the data:%@",steps[i][@"end_location"]);
                [textsteps addObject:html];
                encodedPath    = [[[steps objectAtIndex:i] valueForKey:@"polyline"] valueForKey:@"points"];
                [self polylineWithEncodedString:encodedPath];

                polyline.strokeColor = [UIColor redColor];
                polyline.strokeWidth = 2.f;
                polyline.map = mapView;
                NSLog(@"the encoded patg:%@",encodedPath);                    

                GMSMarker *marker1 = [[GMSMarker alloc]init];
                marker1.map = mapView;                    
                NSLog(@"Direction path");  
}


}
else
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"Connection Error" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"", nil];
    [alert show];
}

我期待像这样的输出

【问题讨论】:

  • 这不是问题。你有什么问题? “我该怎么做”在这里太宽泛了。

标签: ios objective-c google-maps cocoa-touch


【解决方案1】:

您希望显示与 Google 地图相同的方向、时间和所有内容。 iOS 的 Google Maps SDK 会在地图上为您绘制路径。

您可以使用Google Direction API 获取和请求来源和目的地之间的方向。您可以使用GMSPolyline 来绘制它。

但其他事情,比如在哪里转弯,到达目的地还剩多少时间等等,你需要手动管理。如果你想和谷歌一样显示,你需要制作 URL 并在 WebView 中打开

您可以查看URLSchema 文档了解更多详细信息。

【讨论】:

  • 但是我们如何根据流量改变折线颜色。在 google api 响应中,我们获得了交通状态,但我们将如何在折线中更新相同的状态。此外,如果我在 Web 视图中打开 url,我们将无法自定义注释
  • developers.google.com/maps/documentation/directions/intro 如果您想手动处理,请查看此链接。但是你用 URL 去或在谷歌地图应用程序中打开,那么你不必担心。
【解决方案2】:

希望你期待这样的代码

if ([[UIApplication sharedApplication] canOpenURL:
 [NSURL URLWithString:@"comgooglemaps://"]]) {
[[UIApplication sharedApplication] openURL:  
[NSURL URLWithString:@"comgooglemaps://?center=40.765819,-73.975866&zoom=14&views=traffic"]];
} else {
 NSLog(@"Can't use comgooglemaps://");
}

【讨论】:

  • 是的,这段代码正在将我导航到谷歌应用程序我想要的就像应该执行相同的操作但它不应该导航到其他应用程序
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
相关资源
最近更新 更多