Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a marker tap/click on map using Flutter/Dart?

I just started to take a look into flutter/dart. Coming from HTML5/Javascript, I wonder what would be an equivalent to:

google.maps.event.addListener(map, 'click', function(event) {
 placeMarker(event.latLng);
});

function placeMarker(location) {
var marker = new google.maps.Marker({
    position: location, 
    map: map
});

}

I've looked all around the internet, and I found many examples of adding markers, but not on map click.(e.g. Example 1, Example 2). The google_maps_flutter plugin doesn't mention anything about this yet. Is it possible to add the marker by tapping the map, or is this something that's still not available?

Thanks in advance.

like image 508
cubanGuy Avatar asked Jan 18 '19 22:01

cubanGuy


People also ask

How do you add a marker on tap in Flutter?

In Syncfusion Flutter Maps, you can add markers to the tapped locations by following these steps. . Step 1: Add the syncfusion_flutter_maps packages to your dependencies in the pubspec. yaml file. Step 2: Wrap SfMaps to the GestureDetector widget.

How do I add a marker to my current location in Flutter?

In Syncfusion Flutter Maps, you can add a marker in the current location easily. Step 1: Add location and syncfusion_flutter_maps packages to your dependencies in pubspec. yaml file. Step 2: Import the below libraries in your dart file.


2 Answers

The plugin has finally added an onTap property for the GoogleMap Class.

final ArgumentCallback<LatLng> onTap

Example:

GoogleMap(
    markers: _markers,
    initialCameraPosition: _theSecretLocation,
    onMapCreated: (GoogleMapController controller) {
      _controller.complete(controller);
    },
    onTap: _handleTap,
  ),
  ...

 _handleTap(LatLng point) {
setState(() {
  _markers.add(Marker(
    markerId: MarkerId(point.toString()),
    position: point,
    infoWindow: InfoWindow(
      title: 'I am a marker',
    ),
    icon:
        BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueMagenta),
  ));
});
}
like image 147
cubanGuy Avatar answered Nov 14 '22 04:11

cubanGuy


      List _markers = List.generate(10, (index) {
      Map result = results[index];
      Map location = result["geometry"]["location"];
      LatLng latLngMarker = LatLng(location["lat"], location["lng"]);

      return Marker(
          markerId: MarkerId("marker$index"),
          position: latLngMarker,
          onTap: () {
                     //Your code here...
                    },
          infoWindow: InfoWindow(title: result["name"],));
    });

   
like image 27
Tomal Avatar answered Nov 14 '22 05:11

Tomal