Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions example/src/examples/SymbolCircleLayer/CustomIconNativeAsset.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import React, { useRef, memo, useState } from 'react';
import { Text } from 'react-native';
import MapboxGL, {
MapView,
Camera,
ShapeSource,
SymbolLayer,
Images,
} from '@rnmapbox/maps';
import { featureCollection, feature, point } from '@turf/helpers';

import Bubble from '../common/Bubble';
import type { ExampleWithMetadata } from '../common/ExampleMetadata';

const styles = {
icon: {
iconImage: 'pin',
iconAllowOverlap: true,
},
};

const CustomIconNativeAsset = memo(() => {
const cameraRef = useRef<Camera>(null);
const [stateFeatureCollection, setStateFeatureCollection] =
useState<GeoJSON.FeatureCollection>(
featureCollection([point([-73.970895, 40.723279])]),
);

const onPress = (e: GeoJSON.Feature) => {
const aFeature = feature(e.geometry);
aFeature.id = `${Date.now()}`;

setStateFeatureCollection(
featureCollection([...stateFeatureCollection.features, aFeature]),
);
};

const onSourceLayerPress = (e: any) => {
console.log(
'You pressed a layer here are your features:',
e.features,
e.coordinates,
e.point,
);
};

return (
<>
<MapView
style={{ flex: 1 }}
styleURL={MapboxGL.StyleURL.Light}
onPress={onPress}
>
<Camera
ref={cameraRef}
defaultSettings={{
zoomLevel: 9,
centerCoordinate: [-73.970895, 40.723279],
}}
/>
<ShapeSource
id="symbolLocationSource"
hitbox={{ width: 20, height: 20 }}
onPress={(e) => onSourceLayerPress(e)}
shape={stateFeatureCollection}
>
<SymbolLayer
id="symbolLocationSymbols"
minZoomLevel={1}
style={styles.icon}
/>
<Images nativeAssetImages={['pin']} />
</ShapeSource>
</MapView>
<Bubble>
<Text>Tap on Map to add an icon</Text>
</Bubble>
</>
);
});

export default CustomIconNativeAsset;
/* end-example-doc */

const metadata: ExampleWithMetadata['metadata'] = {
title: 'Custom Icon Native Asset',
tags: [
'ShapeSource',
'ShapeSource#onPress',
'SymbolLayer',
'Images',
'SymbolLayer#iconImage',
],
docs: `
Renders a symbol layer with custom icon (native asset) defined using the Images component. Clicking a location on a map add a new icon.
`,
};

(CustomIconNativeAsset as unknown as ExampleWithMetadata).metadata = metadata;
1 change: 1 addition & 0 deletions example/src/examples/SymbolCircleLayer/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as CustomIcon } from './CustomIcon';
export { default as CustomIconNativeAsset } from './CustomIconNativeAsset';
export { default as DataDrivenCircleColors } from './DataDrivenCircleColors';
export { default as Earthquakes } from './Earthquakes';
export { default as ImageScaleTests } from './ImageScaleTests';
Expand Down
4 changes: 2 additions & 2 deletions ios/RNMBX/RNMBXImages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ open class RNMBXImages : UIView, RNMBXMapComponent {
if !sameImage(oldValue: oldImages[name], newValue: images[name]) {
missingImages[name] = images[name]
} else {
if hasImage(style: style, name: name) {
if !hasImage(style: style, name: name) {
logged("RNMBXImages.addImagePlaceholder") {
try style.addImage(placeholderImage, id: name, stretchX: [], stretchY: [])
missingImages[name] = images[name]
Expand Down Expand Up @@ -243,7 +243,7 @@ open class RNMBXImages : UIView, RNMBXMapComponent {
func addNativeImages(style: Style, nativeImages: [NativeImageInfo]) {
for imageInfo in nativeImages {
let imageName = imageInfo.name
if hasImage(style: style, name:imageName) {
if !hasImage(style: style, name: imageName) {
if let image = UIImage(named: imageName) {
logged("RNMBXImage.addNativeImage: \(imageName)") {
try style.addImage(image, id: imageName, sdf: imageInfo.sdf,
Expand Down