-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from DHBW-FN-TIT20/updating-location
Update location every 5s + OutlinePolygon + some smaller changes to the MapHook
- Loading branch information
Showing
2 changed files
with
157 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React from "react"; | ||
import { Polygon } from "react-leaflet"; | ||
|
||
class OutlinePolygon extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
polyLatLngs: [], | ||
}; | ||
} | ||
|
||
async componentDidUpdate(prevProps) { | ||
if (this.props.placeName === prevProps.placeName) return; | ||
this.setState({ polyLatLngs: [] }); | ||
|
||
// fetch the polygon outline from the OpenStreetMap API (Nominatim) | ||
// this is not done in the main component because loading the polygon outline | ||
// takes more time than the normal API call | ||
const response = await fetch( | ||
// eslint-disable-next-line max-len | ||
`https://nominatim.openstreetmap.org/search?q=${this.props.placeName}&format=json&limit=1&polygon_geojson=1&polygon_threshold=0.0005`, | ||
); | ||
// check if the response is suited for a polygon outline | ||
const data = await response.json(); | ||
if ( | ||
data[0]?.geojson?.coordinates === undefined || | ||
data[0]?.geojson?.type === undefined || | ||
data[0]?.boundingbox === undefined || | ||
(data[0]?.geojson?.type !== "Polygon" && data[0]?.geojson?.type !== "MultiPolygon") || | ||
(data[0]?.class !== "boundary" && data[0]?.class !== "place" && data[0]?.class !== "landuse") | ||
) { | ||
this.setState({ polyLatLngs: [] }); | ||
return; | ||
} | ||
|
||
const latLngs = this.convertGeoJsonCoordsToLeafletLatLng(data[0].geojson.coordinates); | ||
this.setState({ polyLatLngs: latLngs }); | ||
} | ||
|
||
/** | ||
* Converts the coordinates of a GeoJSON polygon to the format used by Leaflet | ||
* @param {number[]} coords The coordinates of the GeoJSON polygon | ||
* @returns {number[]} The coordinates for the Leaflet polygon | ||
*/ | ||
convertGeoJsonCoordsToLeafletLatLng = coords => { | ||
if (coords === undefined) return []; | ||
|
||
const transformCoords = coords => { | ||
if ( | ||
Array.isArray(coords) && | ||
coords.length === 2 && | ||
typeof coords[0] === "number" && | ||
typeof coords[1] === "number" | ||
) { | ||
return { lat: coords[1], lng: coords[0] }; | ||
} | ||
return coords.map(transformCoords); | ||
}; | ||
|
||
const latLngs = transformCoords(coords); | ||
|
||
return latLngs; | ||
}; | ||
|
||
render() { | ||
if (this.state.polyLatLngs.length === 0) return null; | ||
|
||
return ( | ||
<Polygon | ||
positions={this.state.polyLatLngs} | ||
color="blue" | ||
weight={1} | ||
opacity={0.2} | ||
fillOpacity={0.05} | ||
smoothFactor={1} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
// define the types of the properties that are passed to the component | ||
OutlinePolygon.prototype.props = /** @type { { | ||
placeName: string, | ||
} } */ ({}); | ||
|
||
export default OutlinePolygon; |
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