Я использую Mapbox iOS SDK
для отображения карт в моем приложении.Я хочу отобразить маршрут, который я получаю с сервера (закодированная полилиния из Mapbox Javascript SDK
).
Не могу заставить его работать так, как Google Maps SDK
, где я мог бы просто сделать
GMSPolyline *line = [GMSPolyline polylineWithPath:self.currentPath];
и показать этот маршрут на экземпляре GMSMapView
.
Я знаю, что могу рассчитать направления, используя MapboxDirections SDK
и получить MBRoute
, но я не хочу этого делать, так как уже получаюзакодированная полилиния из бэкэнда.
Обновление:
+ (NSMutableArray*)decodePolyLine:(NSMutableString*)encoded {
[encoded replaceOccurrencesOfString:@"\\" withString:@"\/" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
options:NSLiteralSearch
range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init] ;
NSInteger lat=0;
NSInteger lng=0;
while (index < len) {
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitude = [[NSNumber alloc] initWithDouble:lat * 1e-5];
NSNumber *longitude = [[NSNumber alloc] initWithDouble:lng * 1e-5];
// printf("[%f,", [latitude doubleValue]);
// printf("%f]", [longitude doubleValue]);
CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude doubleValue] longitude:[longitude doubleValue]];
[array addObject:loc];
}
return array;
}
Я обнаружил этот алгоритм, чтобы получить точки местоположений из закодированной полилинии, и теперь я могу показать маршрут на картено маршрут немного отличается от реальных дорог.Я могу понять, почему, я пробовал их игровую площадку, и она показывает там отлично, но у меня это не работает.
Пожалуйста, помогите, как я могу исправить эту проблему маршрута.
Спасибо