Rajashree Parhi
Rajashree Parhi

Reputation: 43

The method 'removeMarker' and 'addMarker' isn't defined for the class 'GoogleMapController'

I'm adding removeMarker and addMarker but it is showing this- The method 'addMarker' isn't defined for the class 'GoogleMapController'. Try correcting the name to the name of an existing method, or defining a method named 'addMarker'.dart(undefined_method)

The method 'MarkerOptions' isn't defined for the class '_GoogleMapsDemoState'. Try correcting the name to the name of an existing method, or defining a method named 'MarkerOptions'.dart(undefined_method) Quick Fix... Peek Problem

 import 'package:location/location.dart';

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

class GoogleMapsDemo extends StatefulWidget {
  @override
  _GoogleMapsDemoState createState() => _GoogleMapsDemoState();
}

class _GoogleMapsDemoState extends State<GoogleMapsDemo> {
  GoogleMapController mapController;
  Location location = Location();

  Marker marker;

  @override
  void initState() {
    super.initState();
    location.onLocationChanged().listen((location) async {
      if (marker != null) {
        mapController.removeMarker(marker);
      }
      marker = await mapController?.addMarker(MarkerOptions(
        position: LatLng(location["latitude"], location["longitude"]),
      ));
      mapController?.moveCamera(
        CameraUpdate.newCameraPosition(
          CameraPosition(
            target: LatLng(
              location["latitude"],
              location["longitude"],
            ),
            zoom: 20.0,
          ),
        ),
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: <Widget>[
          Container(
            height: MediaQuery.of(context).size.height,
            width: MediaQuery.of(context).size.width,
            child: GoogleMap(

              onMapCreated: (GoogleMapController controller) {
                mapController = controller;
              },
              initialCameraPosition: CameraPosition(
                target: const LatLng(45.521563, -122.677433),
                zoom: 11.0,
              ),
              myLocationButtonEnabled: true,
              myLocationEnabled: true,
            ),
          ),
        ],
      ),
    );
  }
}

The method 'addMarker' isn't defined for the class 'GoogleMapController'. Try correcting the name to the name of an existing method, or defining a method named 'addMarker'.dart(undefined_method) The method 'MarkerOptions' isn't defined for the class '_GoogleMapsDemoState'. Try correcting the name to the name of an existing method, or defining a method named 'MarkerOptions'.dart(undefined_method) Quick Fix... Peek Problem

Upvotes: 3

Views: 4587

Answers (2)

Miguel Gamarra Ramos
Miguel Gamarra Ramos

Reputation: 81

There might be a better way but the following worked for me using the onCameraMove event in order to get the position. If someone has a cleaner way please inform me about it.

class _GoogleMapsDemoState extends State<GoogleMapsDemo> {
  // ...
  LatLng currentLatLng = LatLng(0, 0); //Your inital location -> you need get real location xD


  Widget build(BuildContext context) {
    return GoogleMap(
      // ...
      onCameraMove: (position) {
        print(position);
        currentLatLng = LatLng(position.target.latitude, position.target.longitude);
      },
    );
  }

  void _add() {
    // ...

    final Marker marker = Marker(
      markerId: markerId,
      position: currentLatLng,
      infoWindow: InfoWindow(title: markerIdVal, snippet: '*'),
      onTap: () {
        // ...
      },
      onDragEnd: (LatLng position) {
        //...
      },
    );

    setState(() {
      markers[markerId] = marker;
    });
  }
}

Upvotes: 0

Philip Hahn
Philip Hahn

Reputation: 138

If I'm not totally mistaken, this article should help you. https://stackoverflow.com/a/55000503/11620670

Seems that the flutter team changed something and the Google maps API is now Widget-based and Not controller based anymore.

Have a look at the docs from flutter and the link I posted. I think this should fix your issue because you search for a solution at the wrong end.

Upvotes: 3

Related Questions