-
Notifications
You must be signed in to change notification settings - Fork 884
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// @flow | ||
|
||
import React, { Component } from 'react' | ||
import { Map, SVGOverlay } from '../../src' | ||
|
||
const center = [51.505, -0.09] | ||
const rectangle = [[51.49, -0.08], [51.5, -0.06]] | ||
|
||
export default class SVGOverlayExample extends Component<{}> { | ||
render() { | ||
return ( | ||
<Map center={center} zoom={13}> | ||
<SVGOverlay bounds={rectangle}> | ||
<rect x="0" y="0" width="100%" height="100%" fill="blue" /> | ||
<circle r="5" cx="10" cy="10" fill="red" /> | ||
<text x="50%" y="50%" fill="white"> | ||
text | ||
</text> | ||
</SVGOverlay> | ||
</Map> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// @flow | ||
|
||
import { Layer, LatLngBounds, SVGOverlay as LeafletSVGOverlay } from 'leaflet' | ||
|
||
import { createPortal } from 'react-dom' | ||
import type { LeafletContext, SVGOverlayProps } from './types' | ||
import { withLeaflet } from './context' | ||
import MapComponent from './MapComponent' | ||
|
||
type SVGOverlayTypes = { | ||
attribution?: string, | ||
bounds: LatLngBounds, | ||
opacity?: number, | ||
zIndex?: number, | ||
} | ||
|
||
export class SVGOverlay< | ||
LeafletElement: LeafletSVGOverlay, | ||
Props: SVGOverlayProps, | ||
> extends MapComponent<LeafletElement & SVGOverlayTypes, Props> { | ||
contextValue: ?LeafletContext | ||
leafletElement: LeafletElement | ||
container: SVGSVGElement | ||
|
||
constructor(props: Props) { | ||
super(props) | ||
this.leafletElement = this.createLeafletElement(props) | ||
} | ||
|
||
get layerContainer(): Layer { | ||
return this.props.leaflet.layerContainer || this.props.leaflet.map | ||
} | ||
|
||
createLeafletElement(_props: Props): LeafletElement { | ||
this.container = document.createElementNS( | ||
'http://www.w3.org/2000/svg', | ||
'svg', | ||
) | ||
return new LeafletSVGOverlay( | ||
this.container, | ||
_props.bounds, | ||
this.getOptions(_props), | ||
) | ||
} | ||
|
||
updateLeafletElement(_fromProps: Props, _toProps: Props) { | ||
if (_toProps.bounds !== _fromProps.bounds) { | ||
this.leafletElement.setBounds(_toProps.bounds) | ||
} | ||
if (_toProps.opacity !== _fromProps.opacity) { | ||
this.leafletElement.setOpacity(_toProps.opacity) | ||
} | ||
if (_toProps.zIndex !== _fromProps.zIndex) { | ||
this.leafletElement.setZIndex(_toProps.zIndex) | ||
} | ||
} | ||
|
||
componentDidMount() { | ||
super.componentDidMount() | ||
this.layerContainer.addLayer(this.leafletElement) | ||
} | ||
|
||
componentDidUpdate(prevProps: Props) { | ||
super.componentDidUpdate(prevProps) | ||
|
||
if (this.props.attribution !== prevProps.attribution) { | ||
const { map } = this.props.leaflet | ||
if (map != null && map.attributionControl != null) { | ||
map.attributionControl.removeAttribution(prevProps.attribution) | ||
map.attributionControl.addAttribution(this.props.attribution) | ||
} | ||
} | ||
|
||
this.updateLeafletElement(prevProps, this.props) | ||
} | ||
|
||
componentWillUnmount() { | ||
super.componentWillUnmount() | ||
this.layerContainer.removeLayer(this.leafletElement) | ||
this.container = null | ||
} | ||
|
||
render() { | ||
const { children } = this.props | ||
if (children == null || this.container == null) { | ||
return null | ||
} | ||
|
||
return createPortal(children, this.container) | ||
} | ||
} | ||
|
||
export default withLeaflet(SVGOverlay) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters