Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
My markers are not displayed React leaflet

the goal is to displayed marker on map.

I don't understand why my markers are not displayed

I use react-leaflet

The response is okay but nothing is displayed

thanks you in advance have a god day

response my map

Import

import { MapContainer, TileLayer, Marker, ScaleControl } from 'react-leaflet';
import tileLayer from '../util/tileLayer';
import L from "leaflet";
import 'leaflet-fullscreen/dist/Leaflet.fullscreen.js';
import 'leaflet-fullscreen/dist/leaflet.fullscreen.css';
import { useEffect } from 'react';
import newMarker from "../data/asset/pin.png";
import axios from 'axios'

center of first view

const center = [46.227638, 2.213749];

Icon

const pointerIcon = new L.Icon({
  iconUrl: newMarker,
  iconSize: [50, 58], // size of the icon
  iconAnchor: [20, 58], // changed marker icon position
});

Markers

const MyMarkers = ({ data }) => {
  return data.map(({ lat, lng }, index) => (
    <Marker
      key={index}
      position={{ lat, lng }}
      icon={pointerIcon}
    >
    </Marker>
  ));
}

get data with useEffect, async await & axios

const MapWrapper = () => {

  useEffect( async () => {

    markers = (await componentDataMarkers()).data
    console.log(markers);
  }, [])
  const componentDataMarkers = async () => await axios.get(`http://localhost:5000/plane/latlong`)
  var markers = []

React Leaflet component

  return (
    <MapContainer
    fullscreenControl={true}
    center={center}
    zoom={13}
    scrollWheelZoom={true}
    >

      <TileLayer {...tileLayer} />

      <MyMarkers data={markers} />
      <ScaleControl imperial={false} />
    </MapContainer>
  )
}

export default MapWrapper;
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Marker position is type [lat, lng] & NOT {lat, lng}. Example -

<Marker position={[51.505, -0.09]} />

Update: Your data object is an array of arrays. map function seems to be incorrect. It needs to be like markers.map((marker, index) =>

Working sample that you can try at live editor -

const center = [51.505, -0.09]
const markers = [[51.505, -0.10], [51.505, -0.09], [51.505, -0.08]];
const MyMarkers = ({ data }) => {
    return data.map((marker, index) => {
     return (
     <Marker key={index} position={marker}>
      <Popup>
        Marker <br /> Popup.
      </Popup>
    </Marker>
     ); 
     });
}
render(
  <MapContainer center={center} zoom={13} scrollWheelZoom={false}>
    <TileLayer
      attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
      url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
    />
   <MyMarkers data={markers} />
  </MapContainer>,
)
about 4 years ago · Juan Pablo Isaza Denunciar

0

return data.map(({ lat, lng }, index) => (
    <Marker
      key={index}
      position={[ lat, lng ]} // array here
      icon={pointerIcon}
    >
    </Marker>
  ));

Here, why do you extract lat and lng from an array with key 0 and 1 ? You can't use extract { lat, lng } from array, only from object with lat and lng keys.

Can you console.log the lat and lng and see if you have the data here or undefined ?

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda