Skip to content

Commit

Permalink
[add] Geo Location & Address Serialization
Browse files Browse the repository at this point in the history
[optimize] update Upstream packages
  • Loading branch information
TechQuery committed Sep 1, 2023
1 parent 4ffe5a4 commit b509ae4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 44 deletions.
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "open-react-map",
"version": "0.6.0",
"version": "0.6.3",
"license": "LGPL-3.0-or-later",
"author": "shiy2008@gmail.com",
"description": "Compatible Map component supports Geo services with Freedom or Open API, based on TypeScript, MobX & React.",
Expand All @@ -25,14 +25,13 @@
},
"source": "source/index.ts",
"types": "dist/index.d.ts",
"module": "dist/index.esm.js",
"main": "dist/index.js",
"dependencies": {
"@swc/helpers": "^0.5.1",
"@types/leaflet": "^1.9.3",
"@types/leaflet": "^1.9.4",
"koajax": "^0.8.6",
"leaflet": "^1.9.4",
"mobx-react-helper": "^0.2.3",
"mobx-react-helper": "^0.2.7",
"react-leaflet": "^3.2.5",
"web-utility": "^4.1.0"
},
Expand All @@ -48,13 +47,13 @@
"@types/react": "^17.0.65",
"@types/react-dom": "^17.0.20",
"husky": "^8.0.3",
"idea-react": "^1.0.0-rc.21",
"idea-react": "^1.0.0-rc.22",
"koapache": "^2.2.2",
"lint-staged": "^14.0.1",
"mobx": "^6.10.0",
"mobx-react": "^9.0.0",
"mobx": "^6.10.2",
"mobx-react": "^9.0.1",
"parcel": "^2.9.3",
"prettier": "^3.0.2",
"prettier": "^3.0.3",
"prismjs": "^1.29.0",
"process": "^0.11.10",
"react": "^17.0.2",
Expand Down
68 changes: 34 additions & 34 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 59 additions & 2 deletions source/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { HTTPClient } from 'koajax';
import { LatLngTuple } from 'leaflet';
import * as MobX from 'mobx';
import { computed, observable } from 'mobx';
import { buildURLData } from 'web-utility';

export type CoordinateValue = `${number}.${number}`;
Expand Down Expand Up @@ -60,12 +62,37 @@ export class OpenReactMapModel {
responseType: 'json'
});

@MobX.observable
@observable
currentLocation?: LatLngTuple = undefined;

@observable
searchList: PossibleLocation[] = [];

@MobX.observable
@observable
reversedAddress: AddressLocation = {} as AddressLocation;

@computed
get reversedAddressText() {
return (
this.reversedAddress &&
OpenReactMapModel.addressTextOf(this.reversedAddress)
);
}

/**
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition}
*/
async locate() {
const {
coords: { latitude, longitude }
} = await new Promise<GeolocationPosition>((resolve, reject) =>
navigator.geolocation.getCurrentPosition(resolve, reject, {
enableHighAccuracy: true
})
);
return (this.currentLocation = [latitude, longitude]);
}

/**
* @see https://nominatim.org/release-docs/develop/api/Search/
*/
Expand All @@ -85,4 +112,34 @@ export class OpenReactMapModel {
);
return (this.reversedAddress = body);
}

static addressTextOf({
address: {
country,
state,
state_district,
town,
village,
road,
neighbourhood,
building,
house_number,
amenity
}
}: AddressLocation) {
return [
country,
state,
state_district,
town,
village,
road,
neighbourhood,
building,
house_number,
amenity
]
.filter(Boolean)
.join(' ');
}
}

0 comments on commit b509ae4

Please sign in to comment.