Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

[WIP] Fill pattern example #94

Merged
merged 3 commits into from
Jul 4, 2017
Merged

Conversation

captainbarbosa
Copy link
Contributor

Working on a new example that demonstrates how to add a fill pattern within a MGLFillStyleLayer.

@captainbarbosa
Copy link
Contributor Author

captainbarbosa commented Jun 26, 2017

Here's what its looking like so far. The marked region represents the FCC's no drone zone around Washington, D.C.

simulator screen shot jun 26 2017 12 54 01 pm

},
{
"idiom" : "universal",
"scale" : "2x"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Select “Scales = Single Scale” for vector assets.

mapView.styleURL = MGLStyle.darkStyleURL(withVersion: MGLStyleDefaultVersion)

// Set the map’s center coordinate and zoom level
mapView.setCenter(CLLocationCoordinate2D(latitude: 38.849534447295895, longitude: -77.03922271728514), zoomLevel: 8.5, animated: false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use less precise numbers for the coordinate.

@objc(FillPatternExample_Swift)

class FillPattern_Swift: UIViewController, MGLMapViewDelegate {
var fillPatternImage: UIImage!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is only used in didFinishLoadingStyle, this doesn’t have to be a property of the view controller. Instead, we can do all of the setup in didFinishLoadingStyle.

view.addSubview(mapView)

// Set the UIImage to be used for the fill pattern
fillPatternImage = UIImage(named: "pattern")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered drawing this pattern in code using Core Graphics?

// Add the fill pattern image to used by the style layer
style.setImage(fillPatternImage.withRenderingMode(.alwaysTemplate), forName: "pattern")

let coordinates = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, but it could be less verbose (in Swift, anyway) if the array were structured like this one.

let layer = MGLFillStyleLayer(identifier: "circle", source: source)

// Set the fill pattern and opacity for the style layer
layer.fillPattern = MGLStyleValue<NSString>(rawValue: "pattern")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a little magical — perhaps a more specific name than pattern and a bit more detailed of an explanation would help.

let mapView = MGLMapView(frame: view.bounds)

mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.styleURL = MGLStyle.darkStyleURL(withVersion: MGLStyleDefaultVersion)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example uses a named layer in the dark style, so it should be pinned to the specific version of that style and not the generic default style version (which can be updated with any release).


// Place the style layer below the layer contining city labels, if the
// layer is not found, add the style layer above all layers
if let cityLabels = style.layer(withIdentifier: "place-city-sm") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should note that this is style-specific and may not exist in custom styles.

@@ -5,12 +5,13 @@ import Mapbox
class SimpleMapViewExample_Swift: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

self.navigationController?.isNavigationBarHidden = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes look errant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep 🙈 fixed - was debugging an unrelated issue

@captainbarbosa captainbarbosa force-pushed the fill-pattern-example-yolo branch 7 times, most recently from 2a11d1d to 0808dbf Compare July 3, 2017 22:14
3E085B821EC526E500163C99 /* ExtrusionsExample.m */,
3E3FB66D1F0588B3004512C6 /* LightExample.m */,
3E3FB66F1F0589AD004512C6 /* PointHotspotExample.m */,
055ABCC01EFB139C0063BACA /* FillPatternExample.m */,
968247291C5C1FF800494AB8 /* PointConversionExam
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems bork.

@@ -350,9 +355,12 @@
968247191C5C115000494AB8 /* DrawingAGeoJSONLineExample.m */,
9682470E1C5BF12700494AB8 /* DrawingAMarkerExample.m */,
9682471F1C5C1C0400494AB8 /* DrawingAPolygonExample.m */,
9682471F1C5C1C0400494AB8 /* DrawingAPolygonExample.m */,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dupe — this is usually harmless, but unnecessary nonetheless.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Donezo

// Add the fill pattern image to used by the style layer.
[style setImage:fillPatternImage forName:@"stripe-pattern"];

// "mapbox://examples.0cd7imtl" is a map ID referencing a tileset. For more
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is a map ID referencing a tileset.

That’s what this is technically, but what content does it contain?


[style addSource:source];

// Create a style layer to be used with the vector source.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be used

Would “using” be more precise? We’ve already init’d the source on L38 and are adding it to the layer here.

MGLFillStyleLayer *layer = [[MGLFillStyleLayer alloc] initWithIdentifier:@"drone-restrictions-style" source:source];

// Set the source's identifier using the source name its
// TileJSON metadata: mapbox.com/api-documentation/#retrieve-tilejson-metadata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment seems incomplete.

// Refer to the layers list in Mapbox Studio to confirm which layers are available for
// use when working with a custom style.
MGLStyleLayer *cityLabels = [style layerWithIdentifier:@"place-city-sm"];
if (cityLabels != nil) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking for != nil is necessary in Swift, but not Objective-C.


// Set the UIImage to be used for the fill pattern.
var fillPatternImage: UIImage!
fillPatternImage = UIImage(named: "stripe-pattern")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well combine these two lines in Swift, too.

Copy link
Contributor

@friedbunny friedbunny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎆 🔥 🚒 👩‍🚒 🚑 🏥 🤕 👍

@captainbarbosa captainbarbosa merged commit feba90d into master Jul 4, 2017
@captainbarbosa captainbarbosa deleted the fill-pattern-example-yolo branch July 4, 2017 00:46
// Set the fill pattern and opacity for the style layer. The MGLStyleValue
// object is a generic container for a style attribute value. In this case,
// it is a reference to the fillPatternImage.
layer.fillPattern = MGLStyleValue<NSString>(rawValue: "stripe-pattern")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove <NSString>, since the compiler can infer it.

// Set the map’s size, style, center coordinate, and zoom level.
let mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.styleURL = MGLStyle.darkStyleURL(withVersion: 9)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this example dependent on any implementation details in v9? If not and it’s purely additive, then we can use MGLStyle.darkStyleURL().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per @friedbunny's comment in #94 (comment), I originally had MGLStyle.darkStyleURL(), but it was suggested that we should pin the version to a specific number and explain that relying on simply the default version may become problematic if the current version differs significantly. Although, the place-city-sm label is an important layer that I feel wouldn't be drastically changed anytime soon, so I'm ok reverting it back to MGLStyle.darkStyleURL() , and adding more specific commentary instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, no, what you’ve got makes sense, given the reliance on place-city-sm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I'll keep it then 💯

let layer = MGLFillStyleLayer(identifier: "drone-restrictions-style", source: source)

// Set the source's identifier using the source name retrieved from its
// TileJSON metadata: mapbox.com/api-documentation/#retrieve-tilejson-metadata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mapbox Studio provides a user-friendly way to see these layers without having to construct an API request by hand.

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

Successfully merging this pull request may close these issues.

3 participants