Skip to content

Commit

Permalink
Merge pull request #358 from systemli/add-fit-bounds-functionality
Browse files Browse the repository at this point in the history
🚸 Set map view to defined point(s)
  • Loading branch information
0x46616c6b authored Oct 16, 2022
2 parents 8bfe8a9 + d5c69c0 commit 4b7ab23
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/components/message/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Moment from 'react-moment'
import { Message as MessageType } from '../../api/Message'
import Lightbox from 'react-image-lightbox'
import 'react-image-lightbox/style.css'
import { replaceMagic } from '../../lib/helper'
import { replaceMagic } from '../../lib/replaceLinksHelper'
import MessageModalDelete from './MessageModalDelete'
import MessageMap from './MessageMap'
import { Ticker } from '../../api/Ticker'
Expand Down
8 changes: 7 additions & 1 deletion src/components/message/MessageMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { FC } from 'react'
import { GeoJSON, MapContainer, TileLayer } from 'react-leaflet'
import { Message } from '../../api/Message'
import { Ticker } from '../../api/Ticker'
import { leafletOnDataAddFitToBounds } from '../../lib/leafletFitBoundsHelper'

interface Props {
message: Message
Expand All @@ -29,7 +30,12 @@ const MessageMap: FC<Props> = ({ message, ticker }) => {
return (
<MapContainer center={position} scrollWheelZoom={false} zoom={7}>
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<GeoJSON data={JSON.parse(message.geo_information)} />
<GeoJSON
data={JSON.parse(message.geo_information)}
eventHandlers={{
add: leafletOnDataAddFitToBounds,
}}
/>
</MapContainer>
)
}
Expand Down
8 changes: 7 additions & 1 deletion src/components/message/MessageMapModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { EditControl } from 'react-leaflet-draw'
import { Button, Modal } from 'semantic-ui-react'
import { FeatureCollection, Geometry } from 'geojson'
import { Ticker } from '../../api/Ticker'
import { leafletOnDataAddFitToBounds } from '../../lib/leafletFitBoundsHelper'

interface Props {
callback: (features: FeatureCollection<Geometry, any>) => void
Expand Down Expand Up @@ -54,7 +55,12 @@ const MessageMapModal: FC<Props> = props => {
<Modal.Content style={{ padding: 0 }}>
<MapContainer center={position} style={{ height: 600 }} zoom={zoom}>
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<GeoJSON data={props.map} />
<GeoJSON
data={props.map}
eventHandlers={{
add: leafletOnDataAddFitToBounds,
}}
/>
<FeatureGroup ref={onFeatureGroupUpdate}>
<EditControl
draw={{ circle: false, circlemarker: false }}
Expand Down
19 changes: 19 additions & 0 deletions src/lib/leafletFitBoundsHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { LeafletEvent } from 'leaflet'

export const leafletOnDataAddFitToBounds = (event: LeafletEvent) => {
const leafletLayer = event.target
const features = Object.values(leafletLayer._layers)

if (
features.length === 1 &&
// type is currently not defined
// @ts-ignore
features[0].feature.geometry.type === 'Point'
) {
// @ts-ignore
const coords = features[0].feature.geometry.coordinates
leafletLayer._map.setView([coords[1], coords[0]], 13)
} else if (features.length > 1) {
leafletLayer._map.fitBounds(leafletLayer.getBounds())
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { replaceMagic } from './helper'
import { replaceMagic } from './replaceLinksHelper'

describe('helper', function () {
test('replace links', function () {
Expand Down
File renamed without changes.

0 comments on commit 4b7ab23

Please sign in to comment.