Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
make geolocation promise
Browse files Browse the repository at this point in the history
  • Loading branch information
nina992 committed Dec 11, 2022
1 parent 5d16692 commit a87b887
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
17 changes: 0 additions & 17 deletions src/components/molecules/Visualizer/Engine/Cesium/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,6 @@ export const vo = (
[""]: undefined,
}[o || ""]);

// export const getCurrentLocation = () => {
// let myLocation;
// if (navigator.geolocation) {
// navigator.geolocation.getCurrentPosition(function (position) {
// if (!position) return undefined;
// myLocation = {
// lat: position.coords.latitude,
// lng: position.coords.longitude,
// height: position.coords.altitude ?? 5000,
// };
// return myLocation;
// });
// }
// if (myLocation !== undefined) return myLocation;
// return undefined;
// };

export const getCurrentLocation = () => {
const currentLocation = useGeolocation;
if (currentLocation?.()?.lat == undefined && currentLocation?.()?.lng == undefined)
Expand Down
1 change: 1 addition & 0 deletions src/components/molecules/Visualizer/storybook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export const context: ProviderProps = {
onMouseEvent: act("onMouseEvent"),
captureScreen: act("captureScreen"),
getLocationFromScreen: act("getLocationFromScreen"),
getCurrentLocation: act("getCurrentLocation"),
enableScreenSpaceCameraController: act("enableScreenSpaceCameraController"),
lookHorizontal: act("lookHorizontal"),
lookVertical: act("lookVertical"),
Expand Down
40 changes: 29 additions & 11 deletions src/util/geolocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,38 @@ type CurrentLocation = {
lng: number;
height: number;
};
const initialLocation: CurrentLocation = {
lat: 5.70249,
lng: 39.7622,
height: 5000,
};
const goSuccess = (position: GeolocationPosition) => {
return {
lat: position.coords.latitude,
lng: position.coords.longitude,
heigh: position.coords.altitude ?? 5000,
};
};

const goError = (err: GeolocationPositionError) => {
console.error("Error Code = " + err.code + " - " + err.message);
return initialLocation;
};

const getPosition = () =>
new Promise((resolve, reject) =>
navigator.geolocation.getCurrentPosition(
pos => resolve(goSuccess(pos)),
err => reject(goError(err)),
),
);
export const useGeolocation = (): CurrentLocation | undefined => {
const [location, setLocation] = useState<CurrentLocation>();

const handleSuccess = (position: any) => {
const { lat, lng, height } = position.coords;
setLocation({
lat,
lng,
height,
});
};

navigator.geolocation.getCurrentPosition(handleSuccess);

getPosition()
.then((position: CurrentLocation | any) => {
setLocation({ ...position });
})
.catch(error => console.error("error:", error.message));
return location;
};

0 comments on commit a87b887

Please sign in to comment.