Google Maps for Flutter - Get new position of marker after dragging












0















I want to create a location picker in flutter using google Google Maps for Flutter and it's marker. It's possible to create a dragable marker by creating a marker with dragable option set to true. Using the following code segment.



Widget _buildMap(BuildContext context) {
return GoogleMap(
options: GoogleMapOptions(
cameraPosition: CameraPosition(
target: LatLng(7.2906, 80.6337),
zoom: 7.0,
),
compassEnabled: true,
),
onMapCreated: (controller) {
_mapController = controller;
controller.addMarker(
MarkerOptions(
draggable: true,
flat: false,
position: LatLng(7.2906, 80.6337),
),
);
},
);
}


But I can't find a way to get the new location of the marker after dragging the marker. I tried to get the new location of the marker by referring to markers attribute of the MapController but it returns the initial location of the marker.



_mapController.markers.forEach((marker) {
print("Pos: " + marker.options.position.toString())
});

// Prints "Pos: LatLng[7.2906, 80.63369999999998]"


What am I doing wrong here or is there another way to accomplish this use case? Thank you.










share|improve this question



























    0















    I want to create a location picker in flutter using google Google Maps for Flutter and it's marker. It's possible to create a dragable marker by creating a marker with dragable option set to true. Using the following code segment.



    Widget _buildMap(BuildContext context) {
    return GoogleMap(
    options: GoogleMapOptions(
    cameraPosition: CameraPosition(
    target: LatLng(7.2906, 80.6337),
    zoom: 7.0,
    ),
    compassEnabled: true,
    ),
    onMapCreated: (controller) {
    _mapController = controller;
    controller.addMarker(
    MarkerOptions(
    draggable: true,
    flat: false,
    position: LatLng(7.2906, 80.6337),
    ),
    );
    },
    );
    }


    But I can't find a way to get the new location of the marker after dragging the marker. I tried to get the new location of the marker by referring to markers attribute of the MapController but it returns the initial location of the marker.



    _mapController.markers.forEach((marker) {
    print("Pos: " + marker.options.position.toString())
    });

    // Prints "Pos: LatLng[7.2906, 80.63369999999998]"


    What am I doing wrong here or is there another way to accomplish this use case? Thank you.










    share|improve this question

























      0












      0








      0


      1






      I want to create a location picker in flutter using google Google Maps for Flutter and it's marker. It's possible to create a dragable marker by creating a marker with dragable option set to true. Using the following code segment.



      Widget _buildMap(BuildContext context) {
      return GoogleMap(
      options: GoogleMapOptions(
      cameraPosition: CameraPosition(
      target: LatLng(7.2906, 80.6337),
      zoom: 7.0,
      ),
      compassEnabled: true,
      ),
      onMapCreated: (controller) {
      _mapController = controller;
      controller.addMarker(
      MarkerOptions(
      draggable: true,
      flat: false,
      position: LatLng(7.2906, 80.6337),
      ),
      );
      },
      );
      }


      But I can't find a way to get the new location of the marker after dragging the marker. I tried to get the new location of the marker by referring to markers attribute of the MapController but it returns the initial location of the marker.



      _mapController.markers.forEach((marker) {
      print("Pos: " + marker.options.position.toString())
      });

      // Prints "Pos: LatLng[7.2906, 80.63369999999998]"


      What am I doing wrong here or is there another way to accomplish this use case? Thank you.










      share|improve this question














      I want to create a location picker in flutter using google Google Maps for Flutter and it's marker. It's possible to create a dragable marker by creating a marker with dragable option set to true. Using the following code segment.



      Widget _buildMap(BuildContext context) {
      return GoogleMap(
      options: GoogleMapOptions(
      cameraPosition: CameraPosition(
      target: LatLng(7.2906, 80.6337),
      zoom: 7.0,
      ),
      compassEnabled: true,
      ),
      onMapCreated: (controller) {
      _mapController = controller;
      controller.addMarker(
      MarkerOptions(
      draggable: true,
      flat: false,
      position: LatLng(7.2906, 80.6337),
      ),
      );
      },
      );
      }


      But I can't find a way to get the new location of the marker after dragging the marker. I tried to get the new location of the marker by referring to markers attribute of the MapController but it returns the initial location of the marker.



      _mapController.markers.forEach((marker) {
      print("Pos: " + marker.options.position.toString())
      });

      // Prints "Pos: LatLng[7.2906, 80.63369999999998]"


      What am I doing wrong here or is there another way to accomplish this use case? Thank you.







      google-maps dart flutter marker






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 7:14









      UdeshUKUdeshUK

      4621614




      4621614
























          3 Answers
          3






          active

          oldest

          votes


















          1














          That package is a developers preview, version 0.0.3. Don't consider it for production work until it hits 1.0. In the meanwhile, you might file a issue to inform the flutter team of your specific priorities.






          share|improve this answer































            1














            My approach to this problem was to use the camera position to move the marker around and then use the current camera position to get the new coordinates. You'll need to refactor a little bit your code to use the latest changes present in version 0.4 of google maps for flutter, which include this callback that you will need to add to your code:



            onCameraMove: ((_position) => _updateMarker(_position)),                        


            Then you can set your marker's new state each time the user moves the camera around and use this coordinates for any other purpose you need:



            void _updatePosition(CameraPosition _position) {
            Position newMarkerPosition = Position(
            latitude: _position.target.latitude,
            longitude: _position.target.longitude);
            Marker marker = markers["your_marker"];

            setState(() {
            markers["your_marker"] = marker.copyWith(
            positionParam: LatLng(newMarkerPosition.latitude, newMarkerPosition.longitude));
            });
            }


            Tell me if it works!






            share|improve this answer































              0














              I managed this by adding listener and few conditions



                mapController.addListener(() async {
              final cameraCoordinates = mapController.cameraPosition.target;
              if (!mapController.isCameraMoving &&
              widget.selectedPlace.options.position !=
              mapController.cameraPosition.target) {
              mapController.updateMarker(
              widget.selectedPlace, MarkerOptions(position: cameraCoordinates));
              }
              });


              Here mapController is GoogleMapController instance.






              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002583%2fgoogle-maps-for-flutter-get-new-position-of-marker-after-dragging%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                That package is a developers preview, version 0.0.3. Don't consider it for production work until it hits 1.0. In the meanwhile, you might file a issue to inform the flutter team of your specific priorities.






                share|improve this answer




























                  1














                  That package is a developers preview, version 0.0.3. Don't consider it for production work until it hits 1.0. In the meanwhile, you might file a issue to inform the flutter team of your specific priorities.






                  share|improve this answer


























                    1












                    1








                    1







                    That package is a developers preview, version 0.0.3. Don't consider it for production work until it hits 1.0. In the meanwhile, you might file a issue to inform the flutter team of your specific priorities.






                    share|improve this answer













                    That package is a developers preview, version 0.0.3. Don't consider it for production work until it hits 1.0. In the meanwhile, you might file a issue to inform the flutter team of your specific priorities.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 3 at 1:16









                    Randal SchwartzRandal Schwartz

                    21.6k22951




                    21.6k22951

























                        1














                        My approach to this problem was to use the camera position to move the marker around and then use the current camera position to get the new coordinates. You'll need to refactor a little bit your code to use the latest changes present in version 0.4 of google maps for flutter, which include this callback that you will need to add to your code:



                        onCameraMove: ((_position) => _updateMarker(_position)),                        


                        Then you can set your marker's new state each time the user moves the camera around and use this coordinates for any other purpose you need:



                        void _updatePosition(CameraPosition _position) {
                        Position newMarkerPosition = Position(
                        latitude: _position.target.latitude,
                        longitude: _position.target.longitude);
                        Marker marker = markers["your_marker"];

                        setState(() {
                        markers["your_marker"] = marker.copyWith(
                        positionParam: LatLng(newMarkerPosition.latitude, newMarkerPosition.longitude));
                        });
                        }


                        Tell me if it works!






                        share|improve this answer




























                          1














                          My approach to this problem was to use the camera position to move the marker around and then use the current camera position to get the new coordinates. You'll need to refactor a little bit your code to use the latest changes present in version 0.4 of google maps for flutter, which include this callback that you will need to add to your code:



                          onCameraMove: ((_position) => _updateMarker(_position)),                        


                          Then you can set your marker's new state each time the user moves the camera around and use this coordinates for any other purpose you need:



                          void _updatePosition(CameraPosition _position) {
                          Position newMarkerPosition = Position(
                          latitude: _position.target.latitude,
                          longitude: _position.target.longitude);
                          Marker marker = markers["your_marker"];

                          setState(() {
                          markers["your_marker"] = marker.copyWith(
                          positionParam: LatLng(newMarkerPosition.latitude, newMarkerPosition.longitude));
                          });
                          }


                          Tell me if it works!






                          share|improve this answer


























                            1












                            1








                            1







                            My approach to this problem was to use the camera position to move the marker around and then use the current camera position to get the new coordinates. You'll need to refactor a little bit your code to use the latest changes present in version 0.4 of google maps for flutter, which include this callback that you will need to add to your code:



                            onCameraMove: ((_position) => _updateMarker(_position)),                        


                            Then you can set your marker's new state each time the user moves the camera around and use this coordinates for any other purpose you need:



                            void _updatePosition(CameraPosition _position) {
                            Position newMarkerPosition = Position(
                            latitude: _position.target.latitude,
                            longitude: _position.target.longitude);
                            Marker marker = markers["your_marker"];

                            setState(() {
                            markers["your_marker"] = marker.copyWith(
                            positionParam: LatLng(newMarkerPosition.latitude, newMarkerPosition.longitude));
                            });
                            }


                            Tell me if it works!






                            share|improve this answer













                            My approach to this problem was to use the camera position to move the marker around and then use the current camera position to get the new coordinates. You'll need to refactor a little bit your code to use the latest changes present in version 0.4 of google maps for flutter, which include this callback that you will need to add to your code:



                            onCameraMove: ((_position) => _updateMarker(_position)),                        


                            Then you can set your marker's new state each time the user moves the camera around and use this coordinates for any other purpose you need:



                            void _updatePosition(CameraPosition _position) {
                            Position newMarkerPosition = Position(
                            latitude: _position.target.latitude,
                            longitude: _position.target.longitude);
                            Marker marker = markers["your_marker"];

                            setState(() {
                            markers["your_marker"] = marker.copyWith(
                            positionParam: LatLng(newMarkerPosition.latitude, newMarkerPosition.longitude));
                            });
                            }


                            Tell me if it works!







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 2 days ago









                            DaviDavi

                            234




                            234























                                0














                                I managed this by adding listener and few conditions



                                  mapController.addListener(() async {
                                final cameraCoordinates = mapController.cameraPosition.target;
                                if (!mapController.isCameraMoving &&
                                widget.selectedPlace.options.position !=
                                mapController.cameraPosition.target) {
                                mapController.updateMarker(
                                widget.selectedPlace, MarkerOptions(position: cameraCoordinates));
                                }
                                });


                                Here mapController is GoogleMapController instance.






                                share|improve this answer




























                                  0














                                  I managed this by adding listener and few conditions



                                    mapController.addListener(() async {
                                  final cameraCoordinates = mapController.cameraPosition.target;
                                  if (!mapController.isCameraMoving &&
                                  widget.selectedPlace.options.position !=
                                  mapController.cameraPosition.target) {
                                  mapController.updateMarker(
                                  widget.selectedPlace, MarkerOptions(position: cameraCoordinates));
                                  }
                                  });


                                  Here mapController is GoogleMapController instance.






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    I managed this by adding listener and few conditions



                                      mapController.addListener(() async {
                                    final cameraCoordinates = mapController.cameraPosition.target;
                                    if (!mapController.isCameraMoving &&
                                    widget.selectedPlace.options.position !=
                                    mapController.cameraPosition.target) {
                                    mapController.updateMarker(
                                    widget.selectedPlace, MarkerOptions(position: cameraCoordinates));
                                    }
                                    });


                                    Here mapController is GoogleMapController instance.






                                    share|improve this answer













                                    I managed this by adding listener and few conditions



                                      mapController.addListener(() async {
                                    final cameraCoordinates = mapController.cameraPosition.target;
                                    if (!mapController.isCameraMoving &&
                                    widget.selectedPlace.options.position !=
                                    mapController.cameraPosition.target) {
                                    mapController.updateMarker(
                                    widget.selectedPlace, MarkerOptions(position: cameraCoordinates));
                                    }
                                    });


                                    Here mapController is GoogleMapController instance.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 13 at 11:47









                                    Gopal krishanGopal krishan

                                    612




                                    612






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002583%2fgoogle-maps-for-flutter-get-new-position-of-marker-after-dragging%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        The term 'EXEC' is not recognized as the name of a cmdlet Powershell

                                        NPM command prompt closes immediately [closed]

                                        Error binding properties and functions in emscripten