2
votes

I'm trying to show the polylines taken from a GeoJson then added to the same source but with poor results.

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];

I logged the source object and inside there are the two polylines. But why are they not shown? what am I doing wrong?

I use mapbox sdk 3.7.6 for ios.

1

1 Answers

1
votes

Are you using the -[MGLMapViewDelegate mapView:didFinishLoadingStyle] method to ensure that your map has properly initialized before you are adding the style layer? If not, you are likely running into a race issue where you are adding data that is immediately overwritten as the style is loading.

If you modify your code to ensure that the source and style aren't being added prematurely, I would expect your issues to resolve.

- (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];
}

⚠️ Disclaimer: I currently work at Mapbox ⚠️