Skip to content

Commit 906f46b

Browse files
chuchuvaferdicus
andauthored
Fix lineGradient showing wrong colors (#1471)
* Fix lineGradient showing wrong colors `lineMetrics` property was missing. Fixes https://github.com/react-native-mapbox-gl/maps/issues/1272 * Add line gradient example * Re-generate documentation * Update CHANGELOG.md Co-authored-by: Kid Commit <26439946+ferdicus@users.noreply.github.com>
1 parent 7475486 commit 906f46b

13 files changed

Lines changed: 122 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Fix crash with missing okhttp dependency ([#1452](https://github.com/react-nativ
1212
Move from react-native-testing-library => @testing-library/react-native ([#1453](https://github.com/react-native-mapbox-gl/maps/pull/1453))
1313
Feat(camera): maxBounds/(min|max)ZoomLevel can be updated dynamically ([#1462](https://github.com/react-native-mapbox-gl/maps/pull/1462))
1414
Refactor(example): clean up folder structure ([#1464](https://github.com/react-native-mapbox-gl/maps/pull/1464))
15+
Fix lineGradient showing wrong colors ([#1471](https://github.com/react-native-mapbox-gl/maps/pull/1471))
1516
Support tintColor on Android ([#1465](https://github.com/react-native-mapbox-gl/maps/pull/1465))
1617
Feat(android): dynamically update tintColor & add example ([#1469](https://github.com/react-native-mapbox-gl/maps/pull/1469)
1718

android/rctmgl/src/main/java/com/mapbox/rctmgl/components/styles/sources/RCTMGLShapeSource.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class RCTMGLShapeSource extends RCTSource<GeoJsonSource> {
4848
private Integer mMaxZoom;
4949
private Integer mBuffer;
5050
private Double mTolerance;
51+
private Boolean mLineMetrics;
5152

5253
private static Bitmap mImagePlaceholder;
5354
private List<Map.Entry<String, ImageEntry>> mImages;
@@ -121,6 +122,10 @@ public void setTolerance(double tolerance) {
121122
mTolerance = tolerance;
122123
}
123124

125+
public void setLineMetrics(boolean lineMetrics) {
126+
mLineMetrics = lineMetrics;
127+
}
128+
124129
public void onPress(OnPressEvent event) {
125130
mManager.handleEvent(FeatureClickEvent.makeShapeSourceEvent(this, event));
126131
}
@@ -152,6 +157,10 @@ private GeoJsonOptions getOptions() {
152157
options.withTolerance(mTolerance.floatValue());
153158
}
154159

160+
if (mLineMetrics != null) {
161+
options.withLineMetrics(mLineMetrics);
162+
}
163+
155164
return options;
156165
}
157166

android/rctmgl/src/main/java/com/mapbox/rctmgl/components/styles/sources/RCTMGLShapeSourceManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ public void setTolerance(RCTMGLShapeSource source, double tolerance) {
128128
source.setTolerance(tolerance);
129129
}
130130

131+
@ReactProp(name = "lineMetrics")
132+
public void setLineMetrics(RCTMGLShapeSource source, boolean lineMetrics) {
133+
source.setLineMetrics(lineMetrics);
134+
}
135+
131136
@ReactProp(name = "hasPressListener")
132137
public void setHasPressListener(RCTMGLShapeSource source, boolean hasPressListener) {
133138
source.setHasPressListener(hasPressListener);

docs/ShapeSource.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
| maxZoomLevel | `number` | `none` | `false` | Specifies the maximum zoom level at which to create vector tiles.<br/>A greater value produces greater detail at high zoom levels.<br/>The default value is 18. |
1515
| buffer | `number` | `none` | `false` | Specifies the size of the tile buffer on each side.<br/>A value of 0 produces no buffer. A value of 512 produces a buffer as wide as the tile itself.<br/>Larger values produce fewer rendering artifacts near tile edges and slower performance.<br/>The default value is 128. |
1616
| tolerance | `number` | `none` | `false` | Specifies the Douglas-Peucker simplification tolerance.<br/>A greater value produces simpler geometries and improves performance.<br/>The default value is 0.375. |
17+
| lineMetrics | `bool` | `none` | `false` | Whether to calculate line distance metrics.<br/>This is required for line layers that specify lineGradient values.<br/>The default value is false. |
1718
| onPress | `func` | `none` | `false` | Source press listener, gets called when a user presses one of the children layers only<br/>if that layer has a higher z-index than another source layers |
1819
| hitbox | `shape` | `none` | `false` | Overrides the default touch hitbox(44x44 pixels) for the source layers |
1920
| &nbsp;&nbsp;width | `number` | `none` | `true` | `width` of hitbox |

docs/docs.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3720,6 +3720,13 @@
37203720
"default": "none",
37213721
"description": "Specifies the Douglas-Peucker simplification tolerance.\nA greater value produces simpler geometries and improves performance.\nThe default value is 0.375."
37223722
},
3723+
{
3724+
"name": "lineMetrics",
3725+
"required": false,
3726+
"type": "bool",
3727+
"default": "none",
3728+
"description": "Whether to calculate line distance metrics.\nThis is required for line layers that specify lineGradient values.\nThe default value is false."
3729+
},
37233730
{
37243731
"name": "onPress",
37253732
"required": false,
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React from 'react';
2+
import MapboxGL from '@react-native-mapbox-gl/maps';
3+
4+
import sheet from '../../styles/sheet';
5+
6+
import BaseExamplePropTypes from '../common/BaseExamplePropTypes';
7+
import Page from '../common/Page';
8+
9+
class GradientLine extends React.Component {
10+
static propTypes = {
11+
...BaseExamplePropTypes,
12+
};
13+
14+
render() {
15+
return (
16+
<Page {...this.props}>
17+
<MapboxGL.MapView style={sheet.matchParent}>
18+
<MapboxGL.Camera centerCoordinate={[-77.035, 38.875]} zoomLevel={12} />
19+
<MapboxGL.ShapeSource id="source1" lineMetrics={true}
20+
shape={{
21+
type: 'Feature',
22+
geometry: {
23+
type: 'LineString',
24+
coordinates: [
25+
[-77.044211, 38.852924],
26+
[-77.045659, 38.860158],
27+
[-77.044232, 38.862326],
28+
[-77.040879, 38.865454],
29+
[-77.039936, 38.867698],
30+
[-77.040338, 38.86943],
31+
[-77.04264, 38.872528],
32+
[-77.03696, 38.878424],
33+
[-77.032309, 38.87937],
34+
[-77.030056, 38.880945],
35+
[-77.027645, 38.881779],
36+
[-77.026946, 38.882645],
37+
[-77.026942, 38.885502],
38+
[-77.028054, 38.887449],
39+
[-77.02806, 38.892088],
40+
[-77.03364, 38.892108],
41+
[-77.033643, 38.899926],
42+
],
43+
},
44+
}}>
45+
<MapboxGL.LineLayer id="layer1" style={{
46+
lineColor: 'red',
47+
lineCap: "round",
48+
lineJoin: "round",
49+
lineWidth: 14,
50+
lineGradient: [
51+
'interpolate',
52+
['linear'],
53+
['line-progress'],
54+
0,
55+
'blue',
56+
0.1,
57+
'royalblue',
58+
0.3,
59+
'cyan',
60+
0.5,
61+
'lime',
62+
0.7,
63+
'yellow',
64+
1,
65+
'red'
66+
]
67+
}} />
68+
</MapboxGL.ShapeSource>
69+
</MapboxGL.MapView>
70+
</Page>
71+
);
72+
}
73+
}
74+
75+
export default GradientLine;

example/src/scenes/Home.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ import QueryAtPoint from '../examples/FillRasterLayer/QueryAtPoint';
4141
import QueryWithRect from '../examples/FillRasterLayer/QueryWithRect';
4242
import WatercolorRasterTiles from '../examples/FillRasterLayer/WatercolorRasterTiles';
4343

44+
// LINE LAYER
45+
import GradientLine from '../examples/LineLayer/GradientLine';
46+
4447
// MAP
4548
import ChangeLayerColor from '../examples/Map/ChangeLayerColor';
4649
import CreateOfflineRegion from '../examples/Map/CreateOfflineRegion';
@@ -174,6 +177,9 @@ const Examples = [
174177
ChoroplethLayerByZoomLevel,
175178
),
176179
]),
180+
new ExampleGroup('LineLayer', [
181+
new ExampleItem('GradientLine', GradientLine),
182+
]),
177183
new ExampleGroup('Annotations', [
178184
new ExampleItem('Show Point Annotation', ShowPointAnnotation),
179185
new ExampleItem('Marker View', MarkerView),

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ export interface ShapeSourceProps extends ViewProps {
827827
maxZoomLevel?: number;
828828
buffer?: number;
829829
tolerance?: number;
830+
lineMetrics?: boolean;
830831
images?: { assets?: string[] } & { [key: string]: ImageSourcePropType };
831832
onPress?: (event: OnPressEvent) => void;
832833
hitbox?: {

ios/RCTMGL/RCTMGLShapeSource.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
@property (nonatomic, strong) NSNumber *maxZoomLevel;
2626
@property (nonatomic, strong) NSNumber *buffer;
2727
@property (nonatomic, strong) NSNumber *tolerance;
28+
@property (nonatomic, strong) NSNumber *lineMetrics;
2829

2930
@property (nonatomic, copy) RCTBubblingEventBlock onPress;
3031
@property (nonatomic, assign) BOOL hasPressListener;

ios/RCTMGL/RCTMGLShapeSource.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ - (nullable MGLSource*)makeSource
9494
options[MGLShapeSourceOptionSimplificationTolerance] = _tolerance;
9595
}
9696

97+
if (_lineMetrics != nil) {
98+
options[MGLShapeSourceOptionLineDistanceMetrics] = _lineMetrics;
99+
}
100+
97101
return options;
98102
}
99103

0 commit comments

Comments
 (0)