- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道如何使用 snap to road API 并解析响应,将其显示在 map 上等。但是,snap to road API 有 100 个坐标/请求的限制。
假设我有一个包含 100 多个坐标的数组,并希望所有坐标都与道路对齐。
Snap to Road API docs如果使用超过 100 个坐标,请描述一个解决方法,但它是 Java 代码(我认为)并且我不太理解它。
我如何在 Objective-C 中执行此操作?
在我的实现中,我通过从我的数组中获取 100 个坐标的范围多次调用 snap to roads api。然而,当在 map 上绘制时,这会导致奇怪的多段线:
这是我的代码:
-(void)getDirectionsFrom:(CLLocation*)startLocation to:(CLLocation*)endLocation{
//Create placemarks from the passed in locations
MKPlacemark *start = [[MKPlacemark alloc] initWithCoordinate:locManager.location.coordinate addressDictionary:NULL];
MKPlacemark *finsih = [[MKPlacemark alloc] initWithCoordinate:endLocation.coordinate addressDictionary:NULL];
//Create direction request
MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
[request setSource:[[MKMapItem alloc] initWithPlacemark:start]];
[request setDestination:[[MKMapItem alloc] initWithPlacemark:finsih]];
[request setTransportType:MKDirectionsTransportTypeAutomobile];
//Calculate directions
MKDirections *direction = [[MKDirections alloc]initWithRequest:request];
[direction calculateDirectionsWithCompletionHandler: ^(MKDirectionsResponse *response, NSError *error) {
if (error) {
NSLog(@"Error is %@",error);
}
else {
MKRoute *route = [response.routes firstObject];
NSUInteger pointCount = route.polyline.pointCount;
//Allocate a C array to hold this many points/coordinates...
CLLocationCoordinate2D *routeCoordinates = malloc(pointCount * sizeof(CLLocationCoordinate2D));
//Get the coordinates (all of them)...
[route.polyline getCoordinates:routeCoordinates range:NSMakeRange(0, pointCount)];
NSMutableArray *locationsArray = [[NSMutableArray alloc] init];
CLLocation *lastLocation = [[CLLocation alloc] init];
//Call snap to road with 100 locations at a time
for (int start = 0; start < pointCount; start += 99) {
[locationsArray removeAllObjects];
NSInteger length = MIN(99, pointCount-start);
if(start != 0){
[locationsArray addObject:lastLocation];
}
for(int i = start;i<start+length;i++){
CLLocationCoordinate2D loc2D = routeCoordinates[i];
CLLocation *location = [[CLLocation alloc] initWithLatitude:loc2D.latitude longitude:loc2D.longitude];
[locationsArray addObject:location];
}
lastLocation = [locationsArray lastObject];
[self snapPathToRoad:locationsArray];
}
//Free the memory used by the C array
free(routeCoordinates);
}
}];
}
-(void)snapPathToRoad:(NSMutableArray*)passedInArray{
//Create string to store coordinates in for the URL
NSString *tempcoordinatesForURL = @"";
//Append tempcoordinatesForURL string by the coordinates in the right format
for(int i = 0;i<[passedInArray count];i++){
CLLocationCoordinate2D coordinates = [[passedInArray objectAtIndex:i] coordinate];
NSString *coordinatesString = [NSString stringWithFormat:@"|%f,%f|",coordinates.latitude,coordinates.longitude];
tempcoordinatesForURL = [tempcoordinatesForURL stringByAppendingString:coordinatesString];
}
//Remove unnecessary charchters from tempcoordinatesForURL
NSString *coordinatesForURL = [[tempcoordinatesForURL substringToIndex:[tempcoordinatesForURL length]-1] stringByReplacingOccurrencesOfString:@"||" withString:@"|"];
//Create url by removing last charachter from coordinatesForURL string
NSString *urlPath = [NSString stringWithFormat:@"https://roads.googleapis.com/v1/snapToRoads?path=%@&interpolate=true&key=AIzaSyDrtHA-AMiVVylUPcp46_Vf1eZJJFBwRCY",[coordinatesForURL substringFromIndex:1]];
//Remove unsupproted charchters from urlPath and create an NSURL
NSString *escapedUrlPath = [urlPath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:escapedUrlPath];
//Create request
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//Send request to server
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//If response, parse JSON
if(response){
//Dictionary with the whole JSON file
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//Array of snapped points from the JSON
NSArray *snappedPoints = [result objectForKey:@"snappedPoints"];
//Loop through the snapped points array and add each coordinate to the path
for (int i = 0; i<[snappedPoints count]; i++) {
NSDictionary *location = [[snappedPoints objectAtIndex:i] objectForKey:@"location"];
double latitude = [[location objectForKey:@"latitude"] doubleValue];
double longitude = [[location objectForKey:@"longitude"] doubleValue];
[pathToDraw addCoordinate:CLLocationCoordinate2DMake(latitude, longitude)];
}
//As this method is called multiple times, clear the previous polylines so only the final one will be visible on the map
[self.mapView clear];
//Draw polyline with path
GMSPolyline *polyline = [GMSPolyline polylineWithPath:pathToDraw];
polyline.map = self.mapView;
polyline.strokeColor = [UIColor darkGrayColor];
polyline.strokeWidth = 6;
}
//If error, log it
else if(connectionError){
NSLog(@"%@",connectionError);
}
}];
}
“patToDraw
是一个GMSMutablePath
ivar”
最佳答案
最终,我找到了问题所在。
如果异步调用多个请求,则并非所有请求都会同时完成。因为它们中的每一个都将坐标添加到相同的路径,所以例如坐标较少的请求将更快完成,尽管它们稍后被调用。这会导致坐标以错误的顺序添加到路径中。
关于iOS - 具有 100 多个坐标的 Snap To Road API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36816254/
我是 python 的新手我想匹配来自列表的模式,如果匹配则返回模式。列表包含城市中区域的名称,我需要查找某个区域是否存在于类似 的 url 中 www.abc.com/category-city_a
当有人在表单上的单个字段中输入街道地址时,我想将街道地址转换为标题大小写之类的内容。它不完全是标题大小写,因为直接跟在数字字符串后面的字母应该是大写的。 举个例子,我想要“Flat 3 245A Hi
如果您尝试获取距离: Presidente Vargas 817,Centro RJ rua de santana macaé rj 在 Maps v3 APIcomputeDistanceBetwe
我搜索以下问题的名称(以及后来的算法 ;)):找到从点 z0 到 ze 的最短路径,例如路径停留在“道路”上。下图更好地说明了这一点。道路由点 X=(x1,...,xk) 和 Y=(y1,...,yn
微软商店喜加一限时免费领取《Ryza Roads》 这是一款赛车与飞行模拟的探索的单人游戏 讲述的一玩家在一个海岛上冒险获得更多成就 限时5天免费领取 不玩但是不能没有 有喜欢的直接登陆领取即可
真正的随机问题。 我在玩 Friendly Fire,它允许你在 map 上选择你当前的位置,然后将其转化为游戏数据。它将所有道路变成游戏道路,然后您可以在旁边 build 建筑物。 上次我查看 MK
我正在尝试解决以下难题。我对其中一个测试用例感到困惑。 问题是: Byteland 的国家包含 N 个城市和它们之间的 N - 1 条双向道路,使得任意两个城市之间都有一条路径。 Byteland 的
android native中有没有API可以使用Road Map API来获取平滑的方向路线。我在谷歌地图中发现 JavaScript Google map road apis但不确定适用于 And
我想从 R 中的传单 map 中删除道路。有没有办法删除我们想要的图层,例如道路、湖泊、州边界等? 示例代码: library(leaflet) library(plot3D) top = 45
我最近开始阅读 Keets Doets 和 Jan van Eijck 合着的《通往逻辑、数学和编程的 Haskell 之路》一书(非常非常好的书)。 在其中一个练习中,任务是定义子字符串:我的解决方
我有 3 个 LineString 功能。我正在尝试将坐标从这 3 个 LineString 功能发送到 Roads API,这样我就可以在 map 上获得干净的、捕捉到的线条。我一直在尝试调整 ex
我对这个 CodeJam 问题有疑问:Crossing the Road 我实现了动态规划解决方案,并在“B-small-practice.in”(点击“解决 B-small”获取文件)上比较了运行我
要求的结果: 具有“边”和“节点”两个表的数据库,可用于生成表示特定区域(例如城市)中所有可行驶道路(高速公路)的图表。数据集是从 .osm 文件解析而来的。 背景: 我将第二次尝试创建一个包含两个表
我正在实现谷歌地图道路 API 来获取我在道路上单击的坐标。但它返回的坐标和地点超出了预期。假设我使用方向 API 设置从地点 A 到地点 B 的方向,然后单击道路上的一些点(假设 10 个点)来绘制
我知道如何使用 snap to road API 并解析响应,将其显示在 map 上等。但是,snap to road API 有 100 个坐标/请求的限制。 假设我有一个包含 100 多个坐标的数
我的任务是使用 Google Maps API 创建一个工具,餐厅可以使用该工具来定义送货区域。这是进度:http://codepen.io/keithpickering/pen/NqdzKO 用户应
我正在尝试实现谷歌的 snaptoroad api。但我没有得到任何输出。我正在使用 golang 来实现。我的代码如下: mapClient, err := maps.NewClient(maps.
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 2 年前。 Improve t
如何在不使用帐户中的 map 编辑器的情况下将道路和标签放置在图像叠加层上?我正在使用非自定义 map ,只需要图像上的标签(下面的示例)。我的源代码和覆盖层的示例代码 是: topleftmapbo
以下 Json 有道路附近地方的经度和纬度..我希望这些线能够捕捉到道路...但我尝试过但失败了..只是想在道路上绘制折线,以便计算从一条道路 LatLang 到另一条道路的距离。 { sn
我是一名优秀的程序员,十分优秀!