Google Maps for Flutter - Get new position of marker after dragging
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
add a comment |
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
add a comment |
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
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
google-maps dart
asked Jan 2 at 7:14
UdeshUKUdeshUK
4621614
4621614
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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!
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 3 at 1:16
Randal SchwartzRandal Schwartz
21.6k22951
21.6k22951
add a comment |
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
answered 2 days ago
DaviDavi
234
234
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 13 at 11:47
Gopal krishanGopal krishan
612
612
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
