Skip to content

Instantly share code, notes, and snippets.

@StevenConradEllis
Created April 10, 2019 15:10
Show Gist options
  • Save StevenConradEllis/71399e6abc070b2804d0b41b4a754b56 to your computer and use it in GitHub Desktop.
Save StevenConradEllis/71399e6abc070b2804d0b41b4a754b56 to your computer and use it in GitHub Desktop.
Using capacitor
import React from 'react';
import {connect} from 'react-redux';
import {actions, RootState, selectors} from '../store';
import Map from '../components/Map';
import {IonHeader, IonToolbar, IonButtons, IonMenuButton, IonTitle, IonContent} from '@ionic/react';
import {Location} from '../store/locations/types';
import {Plugins} from '@capacitor/core';
type Props = typeof mapDispatchToProps & ReturnType<typeof mapStateToProps>;
const {Geolocation} = Plugins;
const MapPage: React.FC<Props> = (props) => {
function getMyPosition() {
if (Geolocation) {
Geolocation.getCurrentPosition().then(coordinates => {
props.addLocation({
id: 0,
name: 'Your current location',
lat: coordinates.coords.latitude,
lng: coordinates.coords.longitude
})
});
}
}
if (!props.userLocationRetrieved) {
getMyPosition();
}
return (
<>
<IonHeader>
<IonToolbar>
<IonButtons slot="start">
<IonMenuButton></IonMenuButton>
</IonButtons>
<IonTitle>Map</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent class="map-page">
<Map locations={props.locations} mapCenter={props.mapCenter}/>
</IonContent>
</>
);
}
const mapDispatchToProps = {
addLocation: (location: Location) => actions.locations.updateLocations(location)
};
const mapStateToProps = (state: RootState) => ({
locations: selectors.locations.allLocations(state.locations),
mapCenter: selectors.locations.mapCenter(state.locations),
userLocationRetrieved: state.locations.userLocationRetrieved
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(MapPage);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment