Adding markers dynamically to flutter google map

前端 未结 1 824
迷失自我
迷失自我 2021-01-17 12:19

I\'m using Flutter to make a mobile application that uses a map. We decided to use Google map and the plugin for flutter we use is:

google_maps_flutte

相关标签:
1条回答
  • 2021-01-17 12:32

    If there's no specific reason for you to use Map data structure, here's what I've done in the past.

    I have a Set of Marker in my State

    Set<Marker> markers = Set();
    

    Give it to the map widget. markers: markers

    GoogleMap(
      onMapCreated: _onMapCreated,
      myLocationEnabled: true,
      initialCameraPosition:
        CameraPosition(target: LatLng(0.0, 0.0)),
      markers: markers,
    )
    

    And then adding the Marker, which I'm building with search result and which you'll be building with your user's location, to Set of Marker in setState method.

    // Create a new marker
    Marker resultMarker = Marker(
      markerId: MarkerId(responseResult.name),
      infoWindow: InfoWindow(
      title: "${responseResult.name}",
      snippet: "${responseResult.types?.first}"),
      position: LatLng(responseResult.geometry.location.lat,
      responseResult.geometry.location.lng),
    );
    // Add it to Set
    markers.add(resultMarker);
    

    Edit: I've just noticed you're using GoogleMap widget in your initState, you need to move it to the build method if you want to rebuild it everytime with the new state values.

    0 讨论(0)
提交回复
热议问题