18

I am exploring flutter-web and want to add google map in it. Although, I have used google map using google_maps_flutter in flutter-app but that works for Android and iOS.

Maxi
  • 207
  • 3
  • 15
Muhammad Zeeshan
  • 333
  • 2
  • 3
  • 11
  • 2
    I posted a solution here: https://stackoverflow.com/a/57488553/3268303 – dazza5000 Aug 14 '19 at 13:28
  • This solution is easier: https://stackoverflow.com/a/57746754/3268303 – dazza5000 Jan 31 '20 at 02:12
  • Does this answer your question? [How to use the Google Maps library with Flutter Web?](https://stackoverflow.com/questions/57155881/how-to-use-the-google-maps-library-with-flutter-web) – dazza5000 Jan 31 '20 at 02:12

3 Answers3

23

First, you need the Google Map api keys. Before initializing, you have to have a project in your Google Cloud Platform. Create one if you don't have one.

enter image description here

Next, search for "Javascript Map" and enable it. Otherwise, the api key will shout an error that says the google map service is not activated.

enter image description here

Initialize the google map js sdk in our index.html file located inside web folder of your project tree.

<script src="https://maps.googleapis.com/maps/api/js?key=<YOUR-API-KEY>"></script>

And import google_maps in pubspec.yaml file:

dependencies:
  google_maps: ^3.4.1

And here is how you create a google map widget.

import 'dart:html';
import 'package:flutter/material.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 = LatLng(1.3521, 103.8198);

    final mapOptions = MapOptions()
      ..zoom = 10
      ..center = LatLng(1.3521, 103.8198);

    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);
}

htmlId - a unique id to name the div element

ui.platformViewRegistry.registerViewFactory - creates a webview in dart

LatLng(1.3521, 103.8198) - class that takes latitude and longitude of a location

DivElement() - class to create a dive element

GMap - creates a google map element

Marker() - the red icon that shows in your LatLng in google map

HtmlElementView() - creates a platform view for Flutter Web

More about google_maps package found here :

https://pub.dev/packages/google_maps

A quick tutorial on how to use it found here :

https://dev.to/happyharis/flutter-web-google-maps-381a

Hope this helps. Thanks

Rami Mohamed
  • 2,505
  • 3
  • 25
  • 33
15

A new official plugin has been released recently: 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 . Do take note its limitations for now - no rotation, no map toolbar, no my location function.

Sha-agi
  • 184
  • 2
  • 5
  • 2
    Is there any way to get the same api for all supported platforms - web, android, ios? Separate plugin for web is against multiplatform functionality promised by Flutter. If I want web only, I don't need Flutter for that – user1209216 Dec 18 '20 at 09:56
  • @user1209216 As of now it's not yet endorsed to the google maps plugin so it needs the dependency. [Check readme.](https://github.com/ditman/plugins/tree/master/packages/google_maps_flutter/google_maps_flutter_web) Though I haven't tried it myself, maybe you can try to use a webview. – Sha-agi Dec 21 '20 at 11:50
  • Is it safe to add the google maps api key in the header of the index.html file? – AlienDecoy Oct 26 '22 at 16:20
  • @AlienDecoy You have to restrict the key for your domain in the Google Cloud API Credential settings. – Martin Braun Jan 06 '23 at 11:43
  • @user1209216 You can share a key between the platforms, but then you cannot restrict it. You actually want to use 3 keys (Android, iOS, web) so you can restrict each of them to their platform and app fingerprint / domain. – Martin Braun Jan 06 '23 at 11:44
-1

I haven't tested it yet but if you want a cross-platform solution maybe you can give this a shot: https://pub.dev/packages/flutter_google_maps

Its description says:

A Flutter plugin for integrating Google Maps in iOS, Android and Web applications. It is a wrapper of google_maps_flutter for Mobile and google_maps for Web.
josue.0
  • 775
  • 1
  • 10
  • 23