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

问题描述 投票:1回答:1

我试图显示从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];

我记录了源对象,里面有两条折线。但为什么他们没有出现?我究竟做错了什么?

我使用mapbox sdk 3.7.6 for ios。

objective-c mapbox mapbox-ios
1个回答
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⚠️工作

© www.soinside.com 2019 - 2024. All rights reserved.