Skip to content

Commit

Permalink
fix mylocationfor web side #560
Browse files Browse the repository at this point in the history
  • Loading branch information
liodali committed Sep 17, 2024
1 parent 60092cc commit 32bd7ec
Showing 4 changed files with 28 additions and 23 deletions.
43 changes: 24 additions & 19 deletions flutter_osm_web/lib/src/asset/map.html
Original file line number Diff line number Diff line change
@@ -213,25 +213,30 @@
}

async function getMyLocation() {
var position = await new Promise((resolve, reject) => {
if (!navigator.geolocation) {
reject({ error: true, message: 'Geolocation is not supported by your browser' });
} else {
navigator.geolocation.getCurrentPosition(
resolve,
(position) => {
reject({ error: true, message: 'Unable to retrieve your location' });
}
);
}
});
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;

return {
lat: latitude,
lon: longitude
};
try {
var position = await new Promise((resolve, reject) => {
if (!navigator.geolocation) {
reject({ error: true, message: 'Geolocation is not supported by your browser' });
} else {
navigator.geolocation.getCurrentPosition(
resolve,
(position) => {
reject({ error: true, message: 'Unable to retrieve your location' });
}
);
}
});
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
console.log("getMyLocation "+latitude + ":" + longitude)
return {
lat: latitude,
lon: longitude
};
}catch(e){
console.error(err.message);
return e
}
}
async function getGeoPoints() {
var listGeoPoint = [];
2 changes: 1 addition & 1 deletion flutter_osm_web/lib/src/asset/map.js
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ async function centerMap(mapId) {
async function locateMe(mapId) {
var iframe = getIframe(mapId);
var geoAsync = await iframe.contentWindow.getMyLocation();
return geoAsync;
return JSON.stringify(geoAsync);
}

async function changeTileLayer(mapId,tile) {
2 changes: 1 addition & 1 deletion flutter_osm_web/lib/src/interop/osm_interop.dart
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ external JSPromise getBounds(
);

@JS('locateMe')
external JSPromise locateMe(
external JSPromise<JSString> locateMe(
JSNumber mapId,
);

4 changes: 2 additions & 2 deletions flutter_osm_web/lib/src/mixin_web.dart
Original file line number Diff line number Diff line change
@@ -154,8 +154,8 @@ mixin WebMixin {
}

Future<GeoPoint> myLocation() async {
final locateResp = await interop.locateMe(mapIdMixin.toJS).toDart;
Map<String, dynamic>? value = locateResp.dartify() as Map<String, dynamic>?;
final locateResp = (await interop.locateMe(mapIdMixin.toJS).toDart).toDart;
Map<String, dynamic>? value = json.decode(locateResp);
if (value!.containsKey("error")) {
throw Exception(value["message"]);
}

0 comments on commit 32bd7ec

Please sign in to comment.