如何显示在mapbox ios中的源中添加的折线?

时间:2019-03-20 12:21:18

标签: objective-c mapbox mapbox-ios

我试图显示取自GeoJson的折线,然后将其添加到同一来源,但效果不佳。

NSString *jsonString = @"{\"type\": \"FeatureCollection\",\"features\": [{\"type\": \"Feature\",\"properties\": {},\"geometry\": {\"type\": \"LineString\",\"coordinates\": [[4.873809814453125,52.3755991766591],[4.882049560546875,52.339534544106435],[4.94659423828125,52.34708539110632],[4.94659423828125,52.376437538867776],[5.009765625,52.370568669179654]]}},{\"type\": \"Feature\",\"properties\": {},\"geometry\": {\"type\": \"LineString\",\"coordinates\": [[4.73785400390625,52.32694693334544],[4.882049560546875,52.32778621884898],[4.872436523437499,52.29420237796669],[4.9713134765625,52.340373590787394]]}}]}";
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

MGLShapeCollectionFeature *shapeCollectionFeature = (MGLShapeCollectionFeature *)[MGLShape shapeWithData:jsonData encoding:NSUTF8StringEncoding error:NULL];
MGLMultiPolyline *polylines = [MGLMultiPolyline multiPolylineWithPolylines:shapeCollectionFeature.shapes];
MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"transit" shape:polylines options:nil];
[self.mapView.style addSource:source];

MGLLineStyleLayer *lineLayer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"layer" source:source];
[self.mapView.style addLayer:lineLayer];

我记录了源对象,并且内部有两条折线。但是为什么不显示它们呢?我在做什么错了?

我在iOS上使用mapbox sdk 3.7.6。

1 个答案:

答案 0 :(得分:0)

您是否在添加样式层之前使用-[MGLMapViewDelegate mapView:didFinishLoadingStyle]方法来确保地图已正确初始化?如果不是这样,您可能会遇到种族问题,即要添加随样式加载而立即被覆盖的数据。

如果您修改代码以确保未过早添加源代码和样式,我希望您的问题能够解决。

- (void)mapView:(MGLMapView *)mapView didFinishLoadingStyle:(MGLStyle *)style {
    MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"transit" shape:polylines options:nil];
    [self.mapView.style addSource:source];

    MGLLineStyleLayer *lineLayer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"layer" source:source];
    [self.mapView.style addLayer:lineLayer];
}

⚠️免责声明:我目前在Mapbox⚠️

工作