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

Document that MGLShape has a concrete initializer #7614

Closed
nitrag opened this issue Jan 6, 2017 · 6 comments
Closed

Document that MGLShape has a concrete initializer #7614

nitrag opened this issue Jan 6, 2017 · 6 comments
Assignees
Labels
documentation iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS runtime styling
Milestone

Comments

@nitrag
Copy link
Contributor

nitrag commented Jan 6, 2017

I am creating a MGLShape from GeoJSON (NSData). How do I go from MGLShape to MGLPolyline? Shouldn't you be able to cast? If not, MGLShape needs a public method, similar to shape.coordinate, for getting &coordinates which is required to create MGLPolyline.

Platform: iOS/Swift
Mapbox SDK version: 3.4.0-beta6

Steps to trigger behavior

var poly: MGLPolyline? = nil
if let shape = try? MGLShape(data: geojsonData, encoding: .allZeros){
    print("Shaped")
    poly = shape as? MGLPolyline
    print(poly)
}

Expected behavior

Get MGLPolyline

Actual behavior

nil

The docs are confusing, don't I have to use +shapeWithData:encoding:error: to get a MGLShape in the first place? Then how do I create an instance of ...

You do not create instances of this class directly or create subclasses of this class. Instead, you create instances of MGLPointAnnotation, MGLPointCollection, MGLPolyline, MGLMultiPolyline, MGLPolygon, MGLMultiPolygon, or MGLShapeCollection. The shape classes correspond to the Geometry object types in the GeoJSON standard, but some have nonstandard names for backwards compatibility.

/newb

@1ec5
Copy link
Contributor

1ec5 commented Jan 6, 2017

Wow, you’re right @nitrag, this is confusing.

The return type of +shapeWithData:encoding:error: may be an MGLShape, but you can check whether it’s really an MGLPolyline and cast it to one if it is. In Swift, you can accomplish both steps simultaneously using as? MGLPolyline.

+shapeWithData:encoding:error: is a late addition to this API. The documentation needs to be updated to point out that you can use this factory method for converting GeoJSON data into a shape; otherwise, you should create instances of the concrete classes directly, treating MGLShape as a class cluster. In other words, if you have GeoJSON data, use +[MGLShape shapeWithData:encoding:error:]. If you have just an array of coordinates, use +[MGLPolyline polylineWithCoordinates:count:] directly.

(In the future, once we fix #7454, +[MGLShape shapeWithData:encoding:error:] will be split out into a separate class, MGLGeoJSONSerialization.)

@1ec5 1ec5 added this to the ios-v3.4.0 milestone Jan 6, 2017
@1ec5 1ec5 added documentation macOS Mapbox Maps SDK for macOS runtime styling labels Jan 6, 2017
@1ec5 1ec5 changed the title MGLShape -> MGLPolyline? Document that MGLShape has a concrete initializer Jan 6, 2017
@boundsj boundsj self-assigned this Jan 8, 2017
@1ec5
Copy link
Contributor

1ec5 commented Jan 9, 2017

Fixed in #7638 on the release-ios-v3.4.0 branch.

@1ec5 1ec5 closed this as completed Jan 9, 2017
@boundsj
Copy link
Contributor

boundsj commented Jan 9, 2017

@nitrag

#7638 updated the documentation as @1ec5 suggested to clarify that using the factory method on MGLShape to create instances of MGLShape subclasses is intended.

I wanted to point out though that you say that when you tried if let shape = try? MGLShape(data: geojsonData, encoding: .allZeros) the result was nil and it seems like your original question was was about that and not just the confusing documentation. The most likely cause of the nil result was malformed GeoJSON data (also note that you should use String.Encoding.utf8.rawValue as the encoding type instead of .allZeros). You can also wrap your try statement in a Swift Do-Catch block to learn more about why the operation failed.

This was just closed by #7638, but if you can reproduce the nil result with valid GeoJSON please reopen with a sample of the GeoJSON you are attempting to create a MGLPolylineFeature with.

@1ec5
Copy link
Contributor

1ec5 commented Jan 9, 2017

Also, #7627 adds an example of using MGLShape(data:encoding:) in Swift.

@nitrag
Copy link
Contributor Author

nitrag commented Jan 9, 2017

@1ec5 When you get a chance can you check your blog email? (Don't have email listed in github). I sent you a private project. Should take you 5 min to open and reproduce the crash...it might be a separate issue but seeing as it throws the same crash I'm was not sure if I should open a separate issue.

@1ec5
Copy link
Contributor

1ec5 commented Jan 10, 2017

Thanks @nitrag – looking into it.

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

No branches or pull requests

4 participants