获取 Flutter Web 的用户位置
Get user's location for Flutter Web
我目前正在使用 Flutter Geolocator 插件来访问用户的位置 (latitude/longitude),但它似乎还不适用于网络。我试图在 Flutter for web 中找到一些东西来检索用户的位置,但找不到。
有人已经tried/found做这份工作了吗?
为了能够检索我必须使用的 Web 部件的位置
https://pub.dev/packages/js to expose the JavaScript API and combine it with https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API.
所以这是它的样子:
首先创建一个文件(ex:locationJs.dart)暴露JS函数:
@JS('navigator.geolocation') // navigator.geolocation namespace
library jslocation; // library name can be whatever you want
import "package:js/js.dart";
@JS('getCurrentPosition') // Accessing method getCurrentPosition
from Geolocation API
external void getCurrentPosition(Function success(GeolocationPosition pos));
@JS()
@anonymous
class GeolocationCoordinates {
external double get latitude;
external double get longitude;
external double get altitude;
external double get accuracy;
external double get altitudeAccuracy;
external double get heading;
external double get speed;
external factory GeolocationCoordinates(
{double latitude,
double longitude,
double altitude,
double accuracy,
double altitudeAccuracy,
double heading,
double speed});
}
@JS()
@anonymous
class GeolocationPosition {
external GeolocationCoordinates get coords;
external factory GeolocationPosition({GeolocationCoordinates
coords});
}
调用新建的文件
import 'package:Shared/locationJs.dart';
success(pos) {
try {
print(pos.coords.latitude);
print(pos.coords.longitude);
} catch (ex) {
print("Exception thrown : " + ex.toString());
}
}
_getCurrentLocation() {
if (kIsWeb) {
getCurrentPosition(allowInterop((pos) => success(pos)));
}
}
我目前正在使用 Flutter Geolocator 插件来访问用户的位置 (latitude/longitude),但它似乎还不适用于网络。我试图在 Flutter for web 中找到一些东西来检索用户的位置,但找不到。
有人已经tried/found做这份工作了吗?
为了能够检索我必须使用的 Web 部件的位置 https://pub.dev/packages/js to expose the JavaScript API and combine it with https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API.
所以这是它的样子:
首先创建一个文件(ex:locationJs.dart)暴露JS函数:
@JS('navigator.geolocation') // navigator.geolocation namespace library jslocation; // library name can be whatever you want import "package:js/js.dart"; @JS('getCurrentPosition') // Accessing method getCurrentPosition from Geolocation API external void getCurrentPosition(Function success(GeolocationPosition pos)); @JS() @anonymous class GeolocationCoordinates { external double get latitude; external double get longitude; external double get altitude; external double get accuracy; external double get altitudeAccuracy; external double get heading; external double get speed; external factory GeolocationCoordinates( {double latitude, double longitude, double altitude, double accuracy, double altitudeAccuracy, double heading, double speed}); } @JS() @anonymous class GeolocationPosition { external GeolocationCoordinates get coords; external factory GeolocationPosition({GeolocationCoordinates coords}); }
调用新建的文件
import 'package:Shared/locationJs.dart'; success(pos) { try { print(pos.coords.latitude); print(pos.coords.longitude); } catch (ex) { print("Exception thrown : " + ex.toString()); } } _getCurrentLocation() { if (kIsWeb) { getCurrentPosition(allowInterop((pos) => success(pos))); } }