Skip to content

Commit b509ae4

Browse files
committed
[add] Geo Location & Address Serialization
[optimize] update Upstream packages
1 parent 4ffe5a4 commit b509ae4

File tree

3 files changed

+100
-44
lines changed

3 files changed

+100
-44
lines changed

package.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "open-react-map",
3-
"version": "0.6.0",
3+
"version": "0.6.3",
44
"license": "LGPL-3.0-or-later",
55
"author": "[email protected]",
66
"description": "Compatible Map component supports Geo services with Freedom or Open API, based on TypeScript, MobX & React.",
@@ -25,14 +25,13 @@
2525
},
2626
"source": "source/index.ts",
2727
"types": "dist/index.d.ts",
28-
"module": "dist/index.esm.js",
2928
"main": "dist/index.js",
3029
"dependencies": {
3130
"@swc/helpers": "^0.5.1",
32-
"@types/leaflet": "^1.9.3",
31+
"@types/leaflet": "^1.9.4",
3332
"koajax": "^0.8.6",
3433
"leaflet": "^1.9.4",
35-
"mobx-react-helper": "^0.2.3",
34+
"mobx-react-helper": "^0.2.7",
3635
"react-leaflet": "^3.2.5",
3736
"web-utility": "^4.1.0"
3837
},
@@ -48,13 +47,13 @@
4847
"@types/react": "^17.0.65",
4948
"@types/react-dom": "^17.0.20",
5049
"husky": "^8.0.3",
51-
"idea-react": "^1.0.0-rc.21",
50+
"idea-react": "^1.0.0-rc.22",
5251
"koapache": "^2.2.2",
5352
"lint-staged": "^14.0.1",
54-
"mobx": "^6.10.0",
55-
"mobx-react": "^9.0.0",
53+
"mobx": "^6.10.2",
54+
"mobx-react": "^9.0.1",
5655
"parcel": "^2.9.3",
57-
"prettier": "^3.0.2",
56+
"prettier": "^3.0.3",
5857
"prismjs": "^1.29.0",
5958
"process": "^0.11.10",
6059
"react": "^17.0.2",

pnpm-lock.yaml

Lines changed: 34 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/model.ts

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { HTTPClient } from 'koajax';
2+
import { LatLngTuple } from 'leaflet';
23
import * as MobX from 'mobx';
4+
import { computed, observable } from 'mobx';
35
import { buildURLData } from 'web-utility';
46

57
export type CoordinateValue = `${number}.${number}`;
@@ -60,12 +62,37 @@ export class OpenReactMapModel {
6062
responseType: 'json'
6163
});
6264

63-
@MobX.observable
65+
@observable
66+
currentLocation?: LatLngTuple = undefined;
67+
68+
@observable
6469
searchList: PossibleLocation[] = [];
6570

66-
@MobX.observable
71+
@observable
6772
reversedAddress: AddressLocation = {} as AddressLocation;
6873

74+
@computed
75+
get reversedAddressText() {
76+
return (
77+
this.reversedAddress &&
78+
OpenReactMapModel.addressTextOf(this.reversedAddress)
79+
);
80+
}
81+
82+
/**
83+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition}
84+
*/
85+
async locate() {
86+
const {
87+
coords: { latitude, longitude }
88+
} = await new Promise<GeolocationPosition>((resolve, reject) =>
89+
navigator.geolocation.getCurrentPosition(resolve, reject, {
90+
enableHighAccuracy: true
91+
})
92+
);
93+
return (this.currentLocation = [latitude, longitude]);
94+
}
95+
6996
/**
7097
* @see https://nominatim.org/release-docs/develop/api/Search/
7198
*/
@@ -85,4 +112,34 @@ export class OpenReactMapModel {
85112
);
86113
return (this.reversedAddress = body);
87114
}
115+
116+
static addressTextOf({
117+
address: {
118+
country,
119+
state,
120+
state_district,
121+
town,
122+
village,
123+
road,
124+
neighbourhood,
125+
building,
126+
house_number,
127+
amenity
128+
}
129+
}: AddressLocation) {
130+
return [
131+
country,
132+
state,
133+
state_district,
134+
town,
135+
village,
136+
road,
137+
neighbourhood,
138+
building,
139+
house_number,
140+
amenity
141+
]
142+
.filter(Boolean)
143+
.join(' ');
144+
}
88145
}

0 commit comments

Comments
 (0)