How to get LatLng from ScreenCoordinate's in Flutter while using apple_maps_flutter?

127 views Asked by At

GoogleMapsController provides this method called, _googleMapController.getLatLng(_screenCoordinates) ScreenCoordinate's as parameters and returns respective lat-long of the coordinate points. But to achieve the similar feature on Apple Maps, which I'm rendering using apple_maps_flutter plugin, the plugin doesn't seen to offer any such methods.

Has anyone tried this? My main motive is to be able to allow free hand polygon drawing on maps. Would love some suggestions and insights on how this could be achieved.

1

There are 1 answers

0
0xAPPA On

1. Provide functionality on host (platform=ios)

in AppleMapController.swift

public class AppleMapController : NSObject, FlutterPlatformView, MKMapViewDelegate {
    private func setMethodCallHandlers() {
           channel.setMethodCallHandler({ [unowned self] (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
               if let args: Dictionary<String, Any> = call.arguments as? Dictionary<String,Any> {
                   switch(call.method) {
                   case "map#getLatLng":
                       if let x = args["x"] as? Double,
                          let y = args["y"] as? Double {
                           let screenPoint = CGPoint(x: x, y: y)
                           if let coordinate = self.getCoordinateFromScreenPoint(screenPoint: screenPoint) {
                               result(["latitude": coordinate.latitude, "longitude": coordinate.longitude])
                           } else {
                               result(nil)
                           }
                       } else {
                           result(nil)
                       }
                       break
                   default:
                       result(FlutterMethodNotImplemented)
                       break
                   }
               }
           })
    }
    private func getCoordinateFromScreenPoint(screenPoint: CGPoint) -> CLLocationCoordinate2D? {
           let coordinate = self.mapView.convert(screenPoint, toCoordinateFrom: self.mapView)
           return coordinate
    }
}

2. call native code in your flutter app over method channel

in controller.dart

class AppleMapController {
  Future<LatLng> getLatLng(ScreenCoordinate screenCoordinate) async {
    print('getLatLng input: ${screenCoordinate.toJson()}');
    final Map<dynamic, dynamic>? result =
        await channel.invokeMethod<Map<dynamic, dynamic>>(
            'map#getLatLng', screenCoordinate.toJson());

    if (result != null) {
      double? latitude = result['latitude'] as double?;
      double? longitude = result['longitude'] as double?;

      if (latitude != null && longitude != null) {
        print('getLatLng() result: $latitude, $longitude');
        return LatLng(latitude, longitude);
      }
    }

    // Handle errors or invalid data
    print('Error getting LatLng');
    return const LatLng(0, 0);
  }
}

ScreenCoordinate implementation can be found here