-
Notifications
You must be signed in to change notification settings - Fork 889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to write a component which outputs Markers? #103
Comments
I don't know if it's the right approach in React world, but I did the following and it worked: import React, {Component} from 'react';
import {Marker, Popup} from 'react-leaflet';
class MyMarker extends Marker {
componentWillMount() {
super.componentWillMount();
}
render () {
return (
<Marker position={this.props.position}>
<Popup>
<span>A pretty CSS3 popup.<br/>Easily customizable.</span>
</Popup>
</Marker>
)
}
}
export default MyMarker; You were simply missing |
@surkova, this does not help. But I updated the repo with |
Then yet another suggestion: import React, {Component} from 'react';
import {marker} from 'leaflet';
import {MapLayer} from 'react-leaflet';
class MyMarker extends MapLayer {
componentWillMount() {
super.componentWillMount();
const { map, position, popupText, ...props } = this.props;
this.leafletElement = marker(position, props).bindPopup(popupText);
}
render() {
return this.renderChildrenWithProps({
layerGroup: this.leafletElement,
});
}
}
export default MyMarker; const builtMarker = (function() {
const position = [51.520, -0.11];
const popupText = "A pretty CSS3 popup.<br/>Easily customizable."
return (
<MyMarker position={position} popupText={popupText}/>
);
})(); |
Thank you very much, but the point is to use |
As written in the documentation, you need to pass the Ex: const MyMarker = ({ map, position }) => (
<Marker map={map} position={position}>
<Popup>
<span>A pretty CSS3 popup.<br/>Easily customizable.</span>
</Popup>
</Marker>
);
...
<Map center={center} zoom={13}>
<TileLayer
url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
<MyMarker position={position} />
</Map> |
@PaulLeCam, thank you for the answer. This example works. But I'm also wondering how to insert a variable or a set of Markers. This would be nice to have an example in |
What do you mean by variable and set of markers? Ex: const MyPopupMarker = ({ map, position, children }) => (
<Marker map={map} position={position}>
<Popup>
<span>{children}</span>
</Popup>
</Marker>
);
const MyMarkersList = ({ map, markers }) => {
const items = markers.map(({ key, ...props }) => (
<MyPopupMarker key={key} map={map} {...props} />
));
return <div style={{display: 'none'}}>{items}</div>;
};
...
class MyMap extends Component {
...
render() {
// Probably generated dynamically, but the logic is the same
const markers = [
{key: 'marker1', position: [51.5, -0.1], children: 'My first popup'},
{key: 'marker2', position: [51.51, -0.1], children: 'My second popup'},
];
return (
<Map ...>
<TileLayer .../>
<MyMarkersList markers={markers} />
</Map>
);
}
} The only constraint if you want to render a list in a component is to wrap it in a container, this is purely a React constraint. |
Thank you very much! Now it's clear. |
Glad it helps! |
map and layerContainer is not passed to child componet in react-leaflet 1.1.1 version ??? |
I have set up map like this
What i require is to have access to my GeoJSON Layer via
|
I have a question in this regard. How can I render these sets on leaflet map asynchronously. What is the best approach? |
How a custom react component which outputs Marker on map should be written? I need to have smth like this working:
I made
MyMarker
component like that https://github.com/varya/react-leaftlet-test/blob/master/src/MyMarker.js but this gets error:I guess the component should not only extend
MapLayer
but also provide special interface. What is missing? I could not find similar example in the docs.Also, what should I do to output several Markers? I mean, in React it's required to be in a single wrapper. But it cannot be just
<div>
for the map. How this wrapper should be written?PS: This is the repo where I demonstrate my case https://github.com/varya/react-leaftlet-test
The text was updated successfully, but these errors were encountered: