From 9df6027a765f448023ff1f4855b2725aec2457d4 Mon Sep 17 00:00:00 2001 From: GabrielTrettel Date: Tue, 1 Jun 2021 21:14:12 -0300 Subject: [PATCH] Attempt to add markers with custom icon dynamically by react native --- src/app/components/map/LeafLetMap.js | 21 ++++++++--- src/app/components/map/Map.html | 47 ++++++++++++------------- src/app/components/map/OpenStreetMap.js | 13 ++++--- src/app/screens/MapFeedScreen.js | 3 +- 4 files changed, 47 insertions(+), 37 deletions(-) diff --git a/src/app/components/map/LeafLetMap.js b/src/app/components/map/LeafLetMap.js index 699463a..a025cdf 100644 --- a/src/app/components/map/LeafLetMap.js +++ b/src/app/components/map/LeafLetMap.js @@ -1,5 +1,6 @@ import { Asset } from "expo-asset"; import * as FileSystem from "expo-file-system"; +import Base64 from "Base64"; const HTML_FILE_PATH = require(`./Map.html`); @@ -43,20 +44,30 @@ function handleEvent(event) { return code_to_function[payload.code](payload); } -function insertMarker(mapRef, ID, cords) { - mapRef.injectJavaScript(` +function insertMarker(mapRef, ID, cords, icon) { + console.log(icon); + mapRef.injectJavaScript( + ` + let customIcon = L.divIcon({ + html: '` + + icon + + `', + iconSize: 10, + }); + + // Check if there is no other marker with same ID already in map if (!(${ID} in markers)) { // Creates marker object - markers[${ID}] = L.marker([${cords.lat}, ${cords.long}], - {ID: ${ID}}); + markers[${ID}] = L.marker([${cords.lat}, ${cords.long}], {ID: ${ID}, icon: customIcon }); // Add marker to map and bing callback event to its function markers[${ID}].addTo(map).on('click', onPopupClick); } - `); + ` + ); } export { loadHTMLFile, handleEvent, insertMarker, goToRegion }; diff --git a/src/app/components/map/Map.html b/src/app/components/map/Map.html index 7f375b7..ef17f18 100644 --- a/src/app/components/map/Map.html +++ b/src/app/components/map/Map.html @@ -5,11 +5,11 @@ - + + + + + diff --git a/src/app/components/map/OpenStreetMap.js b/src/app/components/map/OpenStreetMap.js index 4c98e43..f7ad5fc 100644 --- a/src/app/components/map/OpenStreetMap.js +++ b/src/app/components/map/OpenStreetMap.js @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from "react"; +import React, { useState } from "react"; import { View } from "react-native"; import WebView from "react-native-webview"; import { @@ -11,10 +11,10 @@ import { function bindEventsToListeners(event, clickListener, markerListener) { switch (event.object) { case "click": - clickListener(event.cords); + clickListener && clickListener(event.cords); break; case "marker": - markerListener(event.id); + markerListener && markerListener(event.id); break; default: break; @@ -22,7 +22,6 @@ function bindEventsToListeners(event, clickListener, markerListener) { } export default function OpenStreetMap({ - initialRegion, markersList, animateToPosition, clickListener, @@ -32,13 +31,13 @@ export default function OpenStreetMap({ const [webviewContent, setWebviewContent] = useState(null); if (mapRef != null) { - initialRegion != null && goToRegion(mapRef, initialRegion); - animateToPosition != null && goToRegion(mapRef, animateToPosition); markersList != null && markersList.length > 0 && - markersList.map(({ ID, cords }) => insertMarker(mapRef, ID, cords)); + markersList.map(({ ID, cords, icon }) => + insertMarker(mapRef, ID, cords, icon) + ); } loadHTMLFile() diff --git a/src/app/screens/MapFeedScreen.js b/src/app/screens/MapFeedScreen.js index 185cc77..e04121f 100644 --- a/src/app/screens/MapFeedScreen.js +++ b/src/app/screens/MapFeedScreen.js @@ -17,10 +17,12 @@ export default function MapFeedScreen() { lat: 51.505, long: -0.09, }, + icon: ` `, }, { ID: "2", title: "Casa do Daniel", + icon: ` `, cords: { lat: 51.5032, long: -0.09589, @@ -30,7 +32,6 @@ export default function MapFeedScreen() { return (