简体   繁体   English

适用于iOS和路线的Google Maps SDK

[英]Google Maps SDK for iOS and routes

I've been reading the documentation of the Google Maps SDK for iOS, and I didn't see anything about drawing routes on the map. 我一直在阅读Google Maps SDK for iOS的文档,我没有看到任何关于在地图上绘制路线的信息。

I know that Polylines are available, but I don't think that's the best aproach. 我知道Polylines可用,但我不认为这是最好的方法。

Is it possible? 可能吗?

The iOS SDK does not yet support directions. iOS SDK尚不支持路线。 What you can do in the meantime is use the Directions API Web services and render the route yourself with a GMSPolyline. 在此期间您可以使用Directions API Web服务并使用GMSPolyline自行渲染路径。

You should also file a feature request to have this added to the SDK. 您还应该提交功能请求以将其添加到SDK中。

As Saxon Druce's answer, you can use the Google Maps Directions web service to find the route and draw them by polylines. 作为Saxon Druce的答案,您可以使用Google Maps Directions网络服务查找路线并通过折线绘制它们。 This episode shows you how to realize it, and you can download the example code from this github repository . 向您展示了如何实现它,您可以从此github存储库下载示例代码。

There are some things in the library which aren't in the documentation, so if you're looking for a feature it's worth downloading the SDK and having a look at the headers. 库中有些东西不在文档中,所以如果你正在寻找一个功能,那么值得下载SDK并查看标题。

However in the current version 1.0.2 I don't see anything for routing - either searching for a route or drawing them (or even looking up addresses). 然而,在当前版本1.0.2中,我没有看到任何路由 - 搜索路线或绘制路线(甚至查找地址)。 For now your only option is probably to use some other Google API to find the route, and then as Lee said, use polylines to draw them. 目前,您唯一的选择可能是使用其他一些Google API来查找路径,然后正如Lee所说,使用折线绘制它们。

I had similar problem i needed to have direction path to be drawn on google map using same google maps ios sdk. 我有类似的问题,我需要使用相同的谷歌地图ios sdk在谷歌地图上绘制方向路径。 What i did was i accessed Google Distance API . 我做的是访问了Google Distance API

I extracted the html_instructions & end_location from API using following code. 我使用以下代码从API中提取了html_instructions和end_location。

I had these variables defined as property 我把这些变量定义为属性

@property NSMutableArray *detailedSteps;
@property (strong, nonatomic) IBOutlet GMSMapView *mapview;

And Following code to extract the end_location & html_instructions 并使用以下代码提取end_location和html_instructions

NSURL *url=[[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=ahmedabad&destination=%@",self.city]];
NSURLResponse *res;
NSError *err;
NSData *data=[NSURLConnection sendSynchronousRequest:[[NSURLRequest alloc] initWithURL:url] returningResponse:&res error:&err];
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray *routes=dic[@"routes"];
 NSArray *legs=routes[0][@"legs"];
NSArray *steps=legs[0][@"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"]];
    [textsteps addObject:html];
}
self.detailedSteps=textsteps;
[self showDirection:latlong];

And here is my method showDirection which draws polygon on the map. 这是我的方法showDirection,它在地图上绘制多边形。 Here i am passing the latlong array. 我在这里传递latlong数组。 Which is actually end_location array. 实际上是end_location数组。

-(void)showDirection:(NSMutableArray*) latlong{
GMSMutablePath *path = [GMSMutablePath path];
for(int i=0; i<[latlong count]; i++){
    double lat=[latlong[i][@"lat"] doubleValue];
    double lng=[latlong[i][@"lng"] doubleValue];
    [path addLatitude:lat longitude:lng];
}
NSLog(@"Direction path");
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeColor = [UIColor blueColor];
polyline.strokeWidth = 5.f;
polyline.map = self.mapview ;

}

即使版本1.1.0也没有实现任何指导工具,所以我建议使用URL方案以获得最佳性能。

I think now it is possible to draw routes / direction using Google Maps iOS SDK . 我想现在可以使用Google Maps iOS SDK绘制路线/方向。
Follow official video tutorial here: 在这里关注官方视频教程:
http://talkminer.com/viewtalk.jsp?videoid=AdV7bCWuDYg&q=&row=4&N=5#.Ui2H1mSAZTE http://talkminer.com/viewtalk.jsp?videoid=AdV7bCWuDYg&q=&row=4&N=5#.Ui2H1mSAZTE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM