Skip to content

Commit

Permalink
Map Animated Panning + MapPlaceholder
Browse files Browse the repository at this point in the history
  • Loading branch information
ashuvssut committed Jun 1, 2021
1 parent 35f4978 commit 1bce0d9
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 44 deletions.
125 changes: 83 additions & 42 deletions src/pages/Home/sections/Contact/components/Map/Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,103 @@ import {
Popup,
useMap,
LayersControl,
useMapEvent,
} from "react-leaflet";
import "./map.scss";
import { FiCrosshair } from "react-icons/fi";
const position = [13.0827, 80.2707];

function reCenter(map) {
map.flyTo(position, map.getZoom());
function ReCenterButton() {
const map = useMap();

return (
<label title="Re Center">
<button
className="reCenter"
onClick={() => map.flyTo(position, map.getZoom())}
>
<FiCrosshair size={30} color="#cccccc" />
</button>
</label>
);
}

function Comp() {
const map = useMap();
function SetViewOnClick() {
const map = useMapEvent("click", e => {
map.setView(e.latlng, map.getZoom(), {
animate: true,
});
});

return null;
}

const MapLayers = () => {
return (
<LayersControl position="topright">
<LayersControl.BaseLayer name="OSM Light">
<TileLayer
attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
</LayersControl.BaseLayer>
<LayersControl.BaseLayer checked name="Thunderfrost">
<TileLayer url="https://{s}.tile.thunderforest.com/transport-dark/{z}/{x}/{y}.png" />
</LayersControl.BaseLayer>
<LayersControl.BaseLayer name="Alidade Dark">
<TileLayer url="https://tiles.stadiamaps.com/tiles/alidade_smooth_dark/{z}/{x}/{y}{r}.png" />
</LayersControl.BaseLayer>
<LayersControl.BaseLayer name="Stamen Dark">
<TileLayer url="https://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}{r}.png" />
</LayersControl.BaseLayer>
<LayersControl.BaseLayer name="Carto Dark">
<TileLayer url="https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png" />
</LayersControl.BaseLayer>
<Marker position={position}>
<Popup>
<p>Doc2Pen</p>
<a href="mailto:[email protected]">[email protected]</a>
</Popup>
</Marker>
</LayersControl>
);
};

const MapTooltip = () => {
return (
<div className="mapToolTip">
<p>Doc2Pen</p>
<br />
<br />
<a href="mailto:[email protected]">[email protected]</a>
</div>
);
};

function MapPlaceholder() {
return (
<button className="reCenter" onClick={() => reCenter(map)}>
<FiCrosshair size={30} color="rgb(0, 153, 255)" />
</button>
<p>
Map of Chennai, India.
<noscript>You need to enable JavaScript to see this map.</noscript>
</p>
);
}

function Map() {
return (
<MapContainer center={position} zoom={15} scrollWheelZoom={false}>
<div className="mapToolTip">
<p>Doc2Pen</p>
<br />
<br />
<a href="mailto:[email protected]">[email protected]</a>
</div>
<LayersControl position="topright">
<LayersControl.BaseLayer name="OSM Light">
<TileLayer
attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
</LayersControl.BaseLayer>
<LayersControl.BaseLayer checked name="Thunderfrost">
<TileLayer url="https://{s}.tile.thunderforest.com/transport-dark/{z}/{x}/{y}.png" />
</LayersControl.BaseLayer>
<LayersControl.BaseLayer name="Alidade Dark">
<TileLayer url="https://tiles.stadiamaps.com/tiles/alidade_smooth_dark/{z}/{x}/{y}{r}.png" />
</LayersControl.BaseLayer>
<LayersControl.BaseLayer name="Stamen Dark">
<TileLayer url="https://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}{r}.png" />
</LayersControl.BaseLayer>
<LayersControl.BaseLayer name="Carto Dark">
<TileLayer url="https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png" />
</LayersControl.BaseLayer>
<Marker position={position}>
<Popup>
<p>Doc2Pen</p>
<a href="mailto:[email protected]">[email protected]</a>
</Popup>
</Marker>
</LayersControl>
<Comp />
</MapContainer>
<>
<MapContainer
center={position}
zoom={15}
scrollWheelZoom={false}
placeholder={<MapPlaceholder />}
>
<MapTooltip />
<MapLayers />
<ReCenterButton />
<SetViewOnClick />
</MapContainer>
</>
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/pages/Home/sections/Contact/components/Map/map.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
.reCenter {
border-radius: 100%;
right: 10px;
background: colors.$light-grey;
background: colors.$dark-grey;
padding: 5px;
border: none;
box-shadow: 2px 2px 3px colors.$black-thick-tint;
box-shadow: 0px 0px 3px colors.$black-thick-tint;
}

0 comments on commit 1bce0d9

Please sign in to comment.