简体   繁体   English

在 flutter 中使用谷歌地图移动和 web

[英]Using Google Maps in flutter for both mobile and web

I'm working on an flutter application that should have a common codebase for web and mobile.我正在开发一个 flutter 应用程序,该应用程序应该具有 web 和移动设备的通用代码库。

My app will have a google map and as far as I've seen there's not a single package to satisfy all platforms.我的应用程序将有一个谷歌 map,据我所知,没有一个 package 可以满足所有平台。

google_maps_flutter - seems to work only for mobile (IOS / Android)
google_maps_flutter_web - seems to work only for web

So most probably I have to create two separate MapWidgets, one for the web and one for mobile using these separate packages.所以很可能我必须创建两个单独的 MapWidgets,一个用于 web,另一个用于使用这些单独包的移动设备。

For mobile:对于手机:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class MapSample extends StatefulWidget {
  MapSample({Key? key}) : super(key: key);

  @override
  State<MapSample> createState() => MapSampleState();
}

class MapSampleState extends State<MapSample> {
  final Completer<GoogleMapController> _controller = Completer();

  static const CameraPosition _kGooglePlex = CameraPosition(
    target: LatLng(37.42796133580664, -122.085749655962),
    zoom: 14.4746,
  );

  @override
  Widget build(BuildContext context) {
    return GoogleMap(
      mapType: MapType.hybrid,
      initialCameraPosition: _kGooglePlex,
      onMapCreated: (GoogleMapController controller) {
        _controller.complete(controller);
      },
    );
  }
}

For the web, it's a bit more complicated, it seems that google_maps_flutter_web isn't actually an usable version, from what I understand, (correct me if I'm wrong) and it actually uses another package that's not developed by the flutter team google_maps 6.0.0 .对于 web,它有点复杂,据我了解, google_maps_flutter_web似乎实际上不是一个可用版本(如果我错了请纠正我)并且它实际上使用另一个 package 不是由 flutter 团队google_maps 6.0.0开发的google_maps 6.0.0

The objective of google_maps_flutter_web probably is to have the same api as google_maps_flutter ( google_maps_flutter_platform_interface ) and use it seamlessly, but I couldn't really find an example of how to use it... google_maps_flutter_web的目标可能是拥有与google_maps_flutter ( google_maps_flutter_platform_interface ) 相同的 api 并无缝使用它,但我真的找不到如何使用它的示例......

How should I go about this?我应该怎么go一下这个? Any change I'm mistaken about google_maps_flutter_web and it actually works?我对 google_maps_flutter_web 有什么误解,它真的有效吗? Or I should just try to use google_maps which actually works for the web and just switch widgets based on kIsWeb ?或者我应该尝试使用实际适用于google_maps的 google_maps 并仅根据kIsWeb切换小部件?

Eventually I found a workaround using google_maps and this answer as inspiration:最终我找到了一个使用google_maps的解决方法,并将这个答案作为灵感:

  1. Abstract MapWidget抽象地图控件
    import 'package:client_ojp4danube/map/map_widget_stub.dart'
        if (dart.library.html) 'package:client_ojp4danube/map/map_web_widget.dart'
        if (dart.library.io) 'package:client_ojp4danube/map/map_widget.dart';
    
    import 'package:flutter/material.dart';
    
    abstract class MapWidget extends StatefulWidget {
      factory MapWidget() => getMapWidget();
    }
  1. WebMap widget that uses google_maps:使用 google_maps 的 WebMap 小部件:
    import 'dart:html';
    
    import 'package:client_ojp4danube/map/abstract_map_widget.dart';
    import 'package:flutter/cupertino.dart';
    import 'package:google_maps/google_maps.dart';
    import 'dart:ui' as ui;
    
    Widget getMap() {
      String htmlId = "7";
    
      // ignore: undefined_prefixed_name
      ui.platformViewRegistry.registerViewFactory(htmlId, (int viewId) {
        final myLatlng = new LatLng(30.2669444, -97.7427778);
    
        final mapOptions = new MapOptions()
          ..zoom = 8
          ..center = new LatLng(30.2669444, -97.7427778);
    
        final elem = DivElement()
          ..id = htmlId
          ..style.width = "100%"
          ..style.height = "100%"
          ..style.border = 'none';
    
        final map = GMap(elem, mapOptions);
    
        Marker(MarkerOptions()
          ..position = myLatlng
          ..map = map
          ..title = 'Hello World!');
    
        return elem;
      });
    
      return HtmlElementView(viewType: htmlId);
    }
    
    class WebMap extends StatefulWidget implements MapWidget {
      WebMap({Key? key}) : super(key: key);
    
      @override
      State<WebMap> createState() => WebMapState();
    }
    
    class WebMapState extends State<WebMap> {
      @override
      Widget build(BuildContext context) {
        return getMap();
      }
    }
    
    MapWidget getMapWidget() {
      print("Intra in get map web ");
      return WebMap();
    }
  1. Mobile Map Widget移动 Map 小部件
    import 'dart:async';
    
    import 'package:client_ojp4danube/map/abstract_map_widget.dart';
    import 'package:flutter/material.dart';
    import 'package:google_maps_flutter/google_maps_flutter.dart';
    
    class MobileMap extends StatefulWidget implements MapWidget {
      MobileMap({Key? key}) : super(key: key);
    
      @override
      State<MobileMap> createState() => MobileMapState();
    }
    
    class MobileMapState extends State<MobileMap> {
      final Completer<GoogleMapController> _controller = Completer();
    
      static const CameraPosition _kGooglePlex = CameraPosition(
        target: LatLng(37.42796133580664, -122.085749655962),
        zoom: 14.4746,
      );
    
      @override
      Widget build(BuildContext context) {
        return GoogleMap(
          mapType: MapType.hybrid,
          initialCameraPosition: _kGooglePlex,
          onMapCreated: (GoogleMapController controller) {
            _controller.complete(controller);
          },
        );
      }
    }
    
    MapWidget getMapWidget() {
      return MobileMap();
    }
  1. getMapWidget - stub getMapWidget - 存根
    import 'package:client_ojp4danube/map/abstract_map_widget.dart';
    
    // Created because importing dart.html on a mobile app breaks the build
    MapWidget getMapWidget() => throw UnsupportedError(
        'Cannot create a map without dart:html or google_maps_flutter');
  1. Actually using the abstract widget that will return the widget suited for the platform实际使用将返回适合平台的小部件的抽象小部件
import 'package:client_ojp4danube/map/abstract_map_widget.dart';


class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(child: MapWidget()),
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

EDIT: A new official plugin has been released: https://pub.dev/packages/google_maps_flutter_web .编辑:发布了一个新的官方插件: https://pub.dev/packages/google_maps_flutter_web It already works with the existing google_maps_flutter plugin, just add your api script in the web/index.html.它已经可以与现有的 google_maps_flutter 插件一起使用,只需在 web/index.html 中添加您的 api 脚本。

As the user exilonX suggested, the current way (Apr '22) to use Google Maps on both Flutter web and mobile, is to load the library dynamically based on the device.正如用户 exilonX 所建议的那样,目前(22 年 4 月)在 Flutter web 和移动设备上使用 Google 地图的方式是根据设备动态加载库。 However, his answer lacks of some important details.但是,他的回答缺少一些重要的细节。 It took me almost 1h to make his work working, therefore I'm sharing here a clearer and more organized solution, hopefully it'll save you some time (I couldn't edit his answer due to long edit queue).我花了将近 1 小时才让他的工作正常进行,因此我在这里分享一个更清晰、更有条理的解决方案,希望它能为您节省一些时间(由于编辑队列很长,我无法编辑他的答案)。


Folder structure:文件夹结构:

\widget
   \map_widget.dart
   \web_map_widget.dart
   \mob_map_widget.dart
   \map_widget_stub.dart

MapWidget:地图小部件:

In the file map_widget.dart you'll have the abstract MapWidget:在文件map_widget.dart ,您将拥有抽象的 MapWidget:

import 'package:flutter/material.dart';

import 'map_widget_stub.dart'
    if (dart.library.html) 'web_map_widget.dart'
    if (dart.library.io) 'mob_map_widget.dart';

abstract class MapWidget extends StatefulWidget {
  factory MapWidget() => getMapWidget();
}

NOTE: the only semicolumn you need on the conditional import is at the end of the second if.注意:条件导入所需的唯一半列是在第二个 if 的末尾。


Web MapWidget: Web 地图小部件:

This file will contain the google map shown on web:此文件将包含 web 上显示的 google map:

import 'dart:html';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:google_maps/google_maps.dart';

import 'map_widget.dart';

MapWidget getMapWidget() => WebMap();

class WebMap extends StatefulWidget implements MapWidget {
  WebMap({Key? key}) : super(key: key);

  @override
  State<WebMap> createState() => WebMapState();
}

class WebMapState extends State<WebMap> {
  @override
  Widget build(BuildContext context) {
    final String htmlId = "map";

    // ignore: undefined_prefixed_name
    ui.platformViewRegistry.registerViewFactory(htmlId, (int viewId) {
      final mapOptions = MapOptions()
        ..zoom = 15.0
        ..center = LatLng(35.7560423, 139.7803552);

      final elem = DivElement()..id = htmlId;
      final map = GMap(elem, mapOptions);

      map.onCenterChanged.listen((event) {});
      map.onDragstart.listen((event) {});
      map.onDragend.listen((event) {});

      Marker(MarkerOptions()
        ..position = map.center
        ..map = map);

      return elem;
    });
    return HtmlElementView(viewType: htmlId);
  }
}

Here you can find more details about the web implementation. 在这里您可以找到有关 web 实现的更多详细信息。


Mobile MapWidget:移动地图小部件:

This file contains the implementation for mobile (android/ios):此文件包含移动设备(android/ios)的实现:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

import 'map_widget.dart';

MapWidget getMapWidget() => MobileMap();

class MobileMap extends StatefulWidget implements MapWidget {
  MobileMap({Key? key}) : super(key: key);

  @override
  State<MobileMap> createState() => MobileMapState();
}

class MobileMapState extends State<MobileMap> {
  final Completer<GoogleMapController> _controller = Completer();

  static const CameraPosition _kFalentexHouse =
      CameraPosition(target: LatLng(44.497858579692135, 11.336362079086408));

  @override
  Widget build(BuildContext context) {
    return GoogleMap(
      mapType: MapType.hybrid,
      initialCameraPosition: _kFalentexHouse,
      onMapCreated: (GoogleMapController controller) {
        _controller.complete(controller);
      },
    );
  }
}

Stub存根

Finally, you need a stub:最后,你需要一个存根:

import 'map_widget.dart';

//the error is shown in case of wrong version loaded on wrong platform
MapWidget getMapWidget() => throw UnsupportedError(
    'Cannot create a map without dart:html or google_maps_flutter');

Usage用法

Now you can use the widget MapWidget as a normal widget:现在您可以将小部件 MapWidget 用作普通小部件:

Scaffold(
      body: Center(
        child: SizedBox(
          height: 300,
          width: 300,
          child: MapWidget(),
        ),
      ),
    );

NOTE: in order to make the map work you need to set it up with the key.注意:为了使 map 正常工作,您需要使用密钥进行设置。 See the official library documentation for mobile and web .请参阅mobileweb的官方库文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM