0

My app is loading a HERE map, and works fine with a set of manually entered coordinates as the centre point.

I have implemented Geolocator in another part of the app to produce the users location which is then implemented into a function to find places near their current location & display on map.

I have now taken the Geolocator position finder and want to use it to open the map on the users location. The code uses async and await to get users coordinates.

The map open function does not want to accept async/ await, yet I have tried building it within its own class and pulling the coords, without success.

I am relatively new and any advice is greatly appreciated

Async & Await are currently red underlined in the 'function to open map', and when I change MapMarkerExample to mapMarkerExample at the top of 'function to open map', _mapMarkerExample = MapMarkerExample (_showDialog, hereMapController); is red underlined on `command to open map'

Command to open map

void _onMapCreated(HereMapController hereMapController) {
    hereMapController.mapScene.loadSceneForMapScheme(MapScheme.normalDay, (MapError error) {
      if (error == null) {
        _mapMarkerExample = MapMarkerExample (_showDialog, hereMapController);
      } else {
        print("Map scene not loaded. MapError: " + error.toString());
      }
    });
  }

Function to open map

MapMarkerExample(ShowDialogFunction showDialogCallback, HereMapController hereMapController) async {
    _showDialog = showDialogCallback;
    _hereMapController = hereMapController;

    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

    var lat = position.latitude;
    var long = position.longitude;

    double distanceToEarthInMeters = 8000;
    _hereMapController.camera.lookAtPointWithDistance(
      GeoCoordinates(lat, long), distanceToEarthInMeters);

    
    _setTapGestureHandler();

    _showDialog("Note", "Tap markers for more.");
    }

al246
  • 220
  • 6
  • 16

1 Answers1

0

The problem is that MapMarkerExample() is a constructor and constructors cannot be executed asynchronously.

One possible way to solve this is to not set the coords in the MapMarkerExample() constructor. Just call it to create the instance. And then in a second method _setMapTarget() you can set the center coords of the map.

_setMapTarget() is marked async, so it will be executed immediately and it will not block _onMapCreated(). But it will do the magic a little bit later, fetch the position and once it has the position, it will update the camera.

If fetching the position takes to long, then you would see for that time a default camera position of the map.

  void _onMapCreated(HereMapController hereMapController) {
    hereMapController.mapScene.loadSceneForMapScheme(MapScheme.normalDay, (MapError? error) {
      if (error == null) {
        _mapMarkerExample = MapMarkerExample(_showDialog, hereMapController);
        _setMapTarget(hereMapController);
      } else {
        print("Map scene not loaded. MapError: " + error.toString());
      }
    });
  }

  Future<void> _setMapTarget(HereMapController hereMapController) async {
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

    var lat = position.latitude;
    var long = position.longitude;

    double distanceToEarthInMeters = 8000;
    hereMapController.camera.lookAtPointWithDistance(GeoCoordinates(lat, long), distanceToEarthInMeters);
  }
Nusatad
  • 3,231
  • 3
  • 11
  • 17