Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

MGLShapeSource.shape is always nil #9759

Closed
nitrag opened this issue Aug 12, 2017 · 2 comments
Closed

MGLShapeSource.shape is always nil #9759

nitrag opened this issue Aug 12, 2017 · 2 comments
Labels
iOS Mapbox Maps SDK for iOS

Comments

@nitrag
Copy link
Contributor

nitrag commented Aug 12, 2017

iOS
Mapbox 3.6.1 (test 3.5.0 as well)

Every time I run this function it adds GeoJSON to the map. I take the GeoJSON data of the existing MGLShapeSource using geoJSONData() and manually merge with the new GeoJSON and replace the entire source with the merged. When I adding the second set of data I noticed that it replaced the Features instead of appending. Debugging, I noticed that MGLShapeSource.shape is always nil so there is no old data for a merge. I have attached an Example project where you can Add Source 1 and Add Source 2 but they will always replace the previous data.

oldShape is always nil.

func load(_ filename: String){
    guard let parkData = self.mapView.style?.source(withIdentifier: "park-data") as? MGLShapeSource else {
        print("Can't find Park source")
        return
    }
    let oldShape = parkData.shape   //  <<<<<< Always nil?!?!?!
    if(oldShape != nil){
        print("Old shape available, yay!!!")
    }else{
        print("Old shape is empty, is this correct?")
    }
    
    let oldData = oldShape?.geoJSONData(usingEncoding: String.Encoding.utf8.rawValue)
    
    let path = Bundle.main.url(forResource: filename, withExtension: "geojson")!
    if let newData = try? Data(contentsOf: path ) {
        if let mergedData = mergeGeoJSONfeatures(old: oldData, new: newData), let newShape = try? MGLShape(data: mergedData, encoding: String.Encoding.utf8.rawValue) {
            print("Loading GeoJSON")
            DispatchQueue.main.async {
                parkData.shape = newShape
            }
        }
    }else{
        print("error opening file")
    }
}

Download example project

@Guardiola31337 Guardiola31337 added the iOS Mapbox Maps SDK for iOS label Aug 13, 2017
@1ec5
Copy link
Contributor

1ec5 commented Aug 22, 2017

This is a limitation of GeoJSON sources in core GL. You can keep your own reference to the MGLShapeSource object, which keeps track of the shape with which it was initialized. Otherwise, if you rely on MGLStyle.source(withIdentifier:) to obtain an MGLShapeSource object, that’s actually a different object that has no access to the original shape data (which has since been cut into tiles): #7376.

@nitrag
Copy link
Contributor Author

nitrag commented Aug 25, 2017

Ok Thanks!

For others with this issue: I added a class variable to store the reference. On each mapDidFinishLoading() (first load, plus and map style changes) I'm re-init'ing the MGLShapeSource and assigning it to that variable so the rest of the class has a reference.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
iOS Mapbox Maps SDK for iOS
Projects
None yet
Development

No branches or pull requests

3 participants