2

我试图显示取自 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。

4

1 回答 1

1

在添加样式图层之前,您是否使用该-[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 工作⚠️

于 2019-04-15T15:20:12.813 回答