Flutter : Mapbox symbol on click open google maps

江枫思渺然 提交于 2020-12-15 02:59:14

问题


I have project using MapBox to show address location in map. I have latitude and longitude (-6.192461941069894,106.97593586545025) like this , i want if i click right on marker, i want open Google Maps Apps based on latitude and longitude i have. But the problem , i can't open google maps after click symbol because latitude&longitude after click maps not same with latitude&longitude i have.

Logic Source Code

onMapClick: (point, latlng) {
        if (latlng.latitude == latitude && latlng.longitude == longitude) {
          launchGoogleMaps(latitude: latitude, longitude: longitude);
        }
        print(
            "From Map ${latlng.latitude} |${latlng.latitude} \nFrom Server $latitude||$longitude \n\n");
      },

I think when clicking points on the map are close to each other, I can directly open the Google Maps application. How i can do this ?

Full Source Code

class ExampleMapBox extends StatefulWidget {
  @override
  _ExampleMapBoxState createState() => _ExampleMapBoxState();
}

class _ExampleMapBoxState extends State<ExampleMapBox> {
  MapboxMapController mapController;
  double latitude, longitude;

  @override
  void initState() {
    super.initState();
    latitude = -6.192461941069894;
    longitude = 106.97593586545025;
  }

  void _onMapCreated(MapboxMapController mapboxMapController) {
    mapController = mapboxMapController;
  }

  @override
  Widget build(BuildContext context) {
    return MapboxMap(
      onMapCreated: _onMapCreated,
      initialCameraPosition: CameraPosition(target: LatLng(latitude, longitude), zoom: 10),
      onStyleLoadedCallback: () => addSymbol(mapController),
      onMapClick: (point, latlng) {
        if (latlng.latitude == latitude && latlng.longitude == longitude) {
          launchGoogleMaps(latitude: latitude, longitude: longitude);
        }
        print(
            "From Map ${latlng.latitude} |${latlng.latitude} \nFrom Server $latitude||$longitude \n\n");
      },
    );
  }

  void addSymbol(MapboxMapController mapBoxController) {
    mapBoxController.addSymbol(
      SymbolOptions(
        geometry: LatLng(latitude, longitude),
        iconImage: "assets/images/custom-icon.png",
        iconSize: 2,
      ),
    );
  }

  void launchGoogleMaps({@required double latitude, @required double longitude}) async {
    String googleUrl = 'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude';

    if (await canLaunch(googleUrl)) {
      await launch(googleUrl);
    } else {
      throw 'Could Not Open The Map';
    }
  }
}


回答1:


Generating the exact same coordinates up to a 15 digit precision by clicking on a map is far from possible.

I would recommend to compute the deviation in latitude and longitude and fire the launchGoogleMaps function when the deviation is below a certain threshold. Please try something like the below:

deviation_lat = (latlng.latitude - latitude) * (latlng.latitude - latitude);
deviation_lon = (latlng.longitude - longitude) * (latlng.longitude - longitude);

if (deviation_lat < 1 && deviation_lon < 1) {
          launchGoogleMaps(latitude: latitude, longitude: longitude);
        }


来源:https://stackoverflow.com/questions/61403824/flutter-mapbox-symbol-on-click-open-google-maps

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!