a14n / dart-google-maps

A library to use Google Maps JavaScript API v3 from Dart scripts.
Apache License 2.0
130 stars 66 forks source link

cannot import library of google maps with Flutter Web #69

Closed zeexan-dev closed 3 years ago

zeexan-dev commented 5 years ago

As soon as I include the import statement

import 'package:google_maps/google_maps.dart';

I get the following error. Screenshot_1

Trying to use this in flutter web

darran-kelinske-fivestars commented 5 years ago

Did you get it working?

zeexan-dev commented 5 years ago

@darran-kelinske-fivestars Nops

darran-kelinske-fivestars commented 5 years ago

This seems to be working for me on flutter_web

a14n commented 5 years ago

Feel free to link a repo with a simple working project.

zeexan-dev commented 5 years ago

This seems to be working for me on flutter_web

Could you please share code, or preferably working repo?

juanjoserodrigolazaro commented 5 years ago

yes please, can you share the code? need to have it working on flutter web

dazza5000 commented 5 years ago

Please see the solution here:

https://stackoverflow.com/a/57746754/3268303

The solution outline below is outdated, but still works.

https://stackoverflow.com/a/57488553/3268303

Screenshot from 2019-08-14 00-18-11

I was able to get a workable solution without this library, but it isn't pretty. Here is the majority of the code.

import 'dart:html';

import 'package:flutter_web/material.dart';
import 'package:lift_ai/base/screen_state.dart';
import 'package:lift_ai/feature/property_common/property_contract.dart';
import 'package:lift_ai/feature/property_common/property_presenter_impl.dart';
import 'package:lift_ai/model/car_status.dart';
import 'package:lift_ai/model/property.dart';
import 'package:flutter_web_ui/ui.dart' as ui;
import 'package:lift_ai/util/widget_util.dart';

class PropertyMapPage extends StatefulWidget {
  final CarStatus carStatus;

  PropertyMapPage(Key key, this.carStatus) : super(key: key);

  @override
  _PropertyMapPageState createState() => _PropertyMapPageState(carStatus);
}

class _PropertyMapPageState extends State<PropertyMapPage>
    implements PropertyListView {
  PropertyPresenter _propertyListPresenter;
  List<Property> properties = [];
  ScreenState screenState = ScreenState.LOADING;
  CarStatus carStatus;
  String createdViewId = 'hello-world-html';
  bool inProgress = true;

  _PropertyMapPageState(this.carStatus) {
    _propertyListPresenter = PropertyPresenterImpl(this);
  }

  @override
  void initState() {
    super.initState();
    _propertyListPresenter.getProperties(carStatus, "");
  }

  @override
  void dispose() {
    super.dispose();
    _propertyListPresenter = null;
  }

  @override
  Widget build(BuildContext context) {
    print("Creating html view");

    if (inProgress) {
      return Center(child: CircularProgressIndicator());
    }

    return Row(
      children: <Widget>[
        Container(
            width: MediaQuery.of(context).size.width - 400,
            child: HtmlView(
              viewType: createdViewId,
            )),
        Container(
          width: 400,
          child: properties.isEmpty
              ? WidgetUtil.getEmptyPropertiesView(context)
              : ListView.builder(
                  padding: EdgeInsets.all(8.0),
                  itemCount: properties.length,
                  itemBuilder: (_, index) {
                    return WidgetUtil.buildListRow(
                        context, _propertyListPresenter, properties[index]);
                  },
                ),
        ),
      ],
    );
  }

  @override
  void showProperties(List<Property> properties) {
    String markers = "";

    for (Property property in properties) {
      String marker =
          "var marker = new google.maps.Marker({position: new google.maps.LatLng(${property.lat}, ${property.lng}), map: map, title: 'Hello ${property.id}!'});\n";
      markers += marker;
    }

    String createdViewUpdate = DateTime.now().toString();

    rootBundle.loadString('map.html').then((value) {
      value = value.replaceAll(new RegExp(r'markers'), markers);

      ui.platformViewRegistry.registerViewFactory(
          createdViewId,
          (int viewId) => IFrameElement()
            ..width = (MediaQuery.of(context).size.width - 400).toString()
            ..height = MediaQuery.of(context).size.height.toString()
            ..srcdoc = value
            ..style.border = 'none');
    });

    setState(() {
      inProgress = false;
      this.createdViewId = createdViewUpdate;
      this.properties = properties;
    });
  }

  @override
  void updateScreenState(ScreenState screenState) {
    setState(() {
      this.screenState = screenState;
    });
  }

  @override
  void showException(String string) {
    // TODO: implement showException
  }
}
zeexan-dev commented 5 years ago

@dazza5000 Thanks

Ionys320 commented 5 years ago

I would also need a widget to add simplier and faster the plugin in my web app.

dazza5000 commented 4 years ago

There is an easier solution outlined here: https://stackoverflow.com/a/57746754/3268303

ditman commented 4 years ago

I'm working on a web version of the google_maps_flutter plugin.

It uses this package as the underlying JS-interop library with the Maps JS SDK, and offers a similar API to the current iOS / Android implementation.

Track progress here.

ditman commented 4 years ago

Hey, the google_maps_flutter_web plugin has been published, do check it out! https://pub.dev/packages/google_maps_flutter_web