Skip to content

Instantly share code, notes, and snippets.

@frehner
Created April 16, 2017 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frehner/1b6bece8a0aa81f352c26781fa97c869 to your computer and use it in GitHub Desktop.
Save frehner/1b6bece8a0aa81f352c26781fa97c869 to your computer and use it in GitHub Desktop.
Leaflet-react
import React, { Component } from 'react';
import { ImageOverlay, Map, Marker, Popup } from 'react-leaflet';
import L from 'leaflet';
import './App.css';
import '../node_modules/leaflet/dist/leaflet.css';
import customImage from './test-map.png';
import customPortrait from './portrait.jpg';
class CustomMap extends Component {
state = {
draggable: false,
position: [100,100]
};
handleOnclick = (ev) => {
this.setState({
draggable: !this.state.draggable,
});
};
onDragend = (ev) => {
const tempLatLng = ev.target.getLatLng();
console.log(ev.target.options.id);
this.setState({
position: [tempLatLng.lat, tempLatLng.lng]
});
};
render () {
const bounds = [[0, 0], [1000, 1000]];
const customIcon = L.icon({
iconUrl: customPortrait,
iconSize: [60,90]
});
return (
<div>
<Map
crs={ L.CRS.Simple }
bounds={ bounds }
id='mapContainer'
maxBounds={ bounds }
maxBoundsViscosity={.7}
maxZoom={4}
>
<ImageOverlay url={customImage} bounds={ bounds } />
<Marker
position={this.state.position}
icon={customIcon}
draggable={this.state.draggable}
ondragend={this.onDragend}
id="STEVE"
>
<Popup>
<span> Hi </span>
</Popup>
</Marker>
</Map>
<button onClick={this.handleOnclick}>Click</button>
</div>
);
}
}
export default CustomMap;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment