“onLocationChanged”回调从未被调用(flutter_osm_plugin),但GPS定位正常工作。

huangapple go评论63阅读模式
英文:

"onLocationChanged" callback is never called (flutter_osm_plugin) but the gps localization is working

问题

我正在尝试构建一个Flutter应用程序,需要在用户更改位置时更新其内部状态。为此,我已安装了flutter_osm_plugin,并创建了一个名为OSMFlutter的小部件,其中"trackMyPosition"设置为True,还设置了"onLocationChanged"为一个函数。

每当用户移动时,我必须刷新地图上的标记,如文档所述:
"onLocationChanged: (callback) 当您激活跟踪并且用户位置已更改时触发"
但问题是它不起作用,它只在地图加载时运行一次。我确信地理定位权限正常工作,因为用户在地图上正确显示,并且当用户移动时,地图上的标记也会移动,所以我不知道我做错了什么。

实际上,我不需要使用该软件包提供的"onLocationChanged",我只需要在用户移动时运行一个函数,以便我可以从我的数据库中获取新数据。是否有其他人遇到过这个问题?我该如何解决它?以下是我的代码:

清单

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

小部件(在此示例中,我只是尝试递增一个计数器,以查看是否会在屏幕上更新)

OSMFlutter( 
    controller: controller,
    trackMyPosition: true,
    initZoom: 12,
    minZoomLevel: 8,
    maxZoomLevel: 19,
    stepZoom: 1.0,
    onLocationChanged: (p0) => setState(() {
          _counter++;
        }),
    onGeoPointClicked:(p0) => print("test" + p0.toString()),
    userLocationMarker: UserLocationMaker(
    personMarker: const MarkerIcon(
        icon: Icon(
            Icons.location_history_rounded,
            color: Colors.red,
            size: 100,
            ),
        ),
    directionArrowMarker: const MarkerIcon(
        icon: Icon(
                Icons.double_arrow,
                size: 48,
                ),
            ),
        ),
     roadConfiguration: const RoadOption(
                roadColor: Colors.yellowAccent,
        ),
     markerOption: MarkerOption(
         defaultMarker: const MarkerIcon(
             icon: Icon(
                  Icons.person_pin_circle,
                  color: Colors.blue,
                  size: 56,
                  ),
                )
)

感谢任何可以帮助我的人。

英文:

I'm trying to build a Flutter app which needs to update its internal state every time the user change position. To do so, I have installed flutter_osm_plugin and I have created widget OSMFlutter which has "trackMyPosition" set to True and also has "onLocationChanged" set to a function.

Every time the user moves I have to refresh the markers on the map, and as stated in the documentation:
onLocationChanged: (callback) it is fired when you activate tracking and user position has been changed
but the things is that it doesn't work, it will run just one time when the map is loaded. I'm sure that the gelocalization permissions are working because the user is correctly placed on the map, and when the user moves the marker on the map moves, so I don't know what I'm doing wrong.

I actually don't need to use the "onLocationChanged" provided with the package, I just need to run a function when the user moves so I can fetch new data from my database. Has anyone else encountered this problem? How can I solve it? Here's my code:

manifest

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

the widget (in this example I'm just trying to increment a counter to see if it will update on the screen)

OSMFlutter( 
    controller: controller,
    trackMyPosition: true,
    initZoom: 12,
    minZoomLevel: 8,
    maxZoomLevel: 19,
    stepZoom: 1.0,
    onLocationChanged: (p0) => setState(() {
          _counter++;
        }),
    onGeoPointClicked:(p0) => print("test" + p0.toString()),
    userLocationMarker: UserLocationMaker(
    personMarker: const MarkerIcon(
        icon: Icon(
            Icons.location_history_rounded,
            color: Colors.red,
            size: 100,
            ),
        ),
    directionArrowMarker: const MarkerIcon(
        icon: Icon(
                Icons.double_arrow,
                size: 48,
                ),
            ),
        ),
     roadConfiguration: const RoadOption(
                roadColor: Colors.yellowAccent,
        ),
     markerOption: MarkerOption(
         defaultMarker: const MarkerIcon(
             icon: Icon(
                  Icons.person_pin_circle,
                  color: Colors.blue,
                  size: 56,
                  ),
                )
)

Thanks to anyone that can help me

答案1

得分: 0

Looks like this issue: https://github.com/liodali/osm_flutter/issues/339

I also found this: https://github.com/liodali/osm_flutter/issues/243 with the solution to set the isPicker flag to true, but tbh. i dont know why this should fix the problem.

Since you only want to know when your position changes, you can also use the geolocator package (Flutter Favorite).

There you can easily query for position changes by implementing the PositionStream.

final LocationSettings locationSettings = LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 100,
);
StreamSubscription positionStream = Geolocator.getPositionStream(locationSettings: locationSettings).listen(
(Position? position) {
print(position == null ? 'Unknown' : '${position.latitude.toString()}, ${position.longitude.toString()}');
});

英文:

Looks like this issue: https://github.com/liodali/osm_flutter/issues/339

I also found this: https://github.com/liodali/osm_flutter/issues/243 with the solution to set the isPicker flag to true, but tbh. i dont know why this should fix the problem.

Since you only want to know when your position changes, you can also use the geolocator package (Flutter Favorite).

There you can easily query for position changes by implementing the PositionStream.

final LocationSettings locationSettings = LocationSettings(
  accuracy: LocationAccuracy.high,
  distanceFilter: 100,
);
StreamSubscription<Position> positionStream = Geolocator.getPositionStream(locationSettings: locationSettings).listen(
    (Position? position) {
        print(position == null ? 'Unknown' : '${position.latitude.toString()}, ${position.longitude.toString()}');
    });

huangapple
  • 本文由 发表于 2023年5月10日 21:16:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定