flutter how to get marker id when marker click? - flutter

I am using flutter. I want to get the clicked marker id when a marker is clicked on Google Maps. Or I would like to know if I clicked a marker or a map area. How can I do it?

First, you will create some variables,
one that will be the value of the markerId: MarkerId(markerValue1):
String markerValue1 = 'Your Marker Id';
one that will store the latest markerId value:
String latestMarkerValue;
Then you will add in your Marker widget theonTap: method:
onTap: (){
latestMarkerValue=markerValue1;
}
You will do this for every marker.
If you want to have all the clicked markers' ides then you will change the value of the latestMarkerValue
latestMarkerValue= latestMarkerValue==null?latestMarkerValue= 'markerValue1':latestMarkerValue='$latestMarkerValue $markerValuen'

Related

Flutter GoogleMapsAPI - Change Route with Tap on InfoWindow

I am pretty new to Flutter and I am trying to build an App based on GoogleMaps. Now I added multiple markers and when you tap them the first time a info window pops up. Then, after tapping the info window, I want to change route to another screen, which then includes the description of the place etc.
However, I am struggling now with making it possible to change route after tapping the infowindow, meaning nothing happens when I tap it.
Is it possible to use the 'OnTap' function on the infowindow? I'm happy for any solutions.
My current code for the marker section:
markers.add(Marker( //add first marker
markerId: MarkerId(showLocation.toString()),
position: showLocation, //position of marker
infoWindow: InfoWindow( //popup info
title: 'Marker Title First ',
snippet: 'My Custom Subtitle',
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
},
),
icon: BitmapDescriptor.defaultMarker, //Icon for Marker
));
I just found a supposedly working solution on this problem (Problem with google_maps_flutter, marker onTap(){} doesn't work), however, I guess due to the implementation of null safety it is not working anymore, since I had to initialize the field 'BuildContext'. I did so using 'late' before it, but it still does not work.
We found the problem, I have given multiple markers the same MarkerId. After I changed it, it worked fine. Sorry guys, case closed!

How to create a custom marker which load image Url with Background Clip shape in flutter

I want to code a custom marker which can load a profile pic of a user which is from API and the background should bind with the clip shape which is customized....
click here for view design
It looks like you are trying to use the google maps package, the marker in the package takes BitmapDescriptor, so basically you can load an image from asset. I did not try to add a margin to the marker icon, in my case I just edited the image.
final Uint8List markerIcon = await getBytesFromAsset('assets/images/map_pin.png', 600);
BitmapDescriptor customIcon = BitmapDescriptor.fromBytes(markerIcon);
marker.add(Marker(
markerId: MarkerId(currentLocation!.latitude.toString()),
position: currentLocation!,
draggable: true,
icon: customIcon,
onDragEnd: (LatLng latlng) {
currentLocation = latlng;
}));
I think you want ClipPath https://www.youtube.com/watch?v=oAUebVIb-7s and using some custom paths.
And then, position the map and your marker with a Stack widget.

How to get LatLng position by clicking the maps in flutter

I am using google_maps_flutter to show the maps of user location, I come up with an idea where user can click anything in maps and show the the coordinates they clicked. What I mean with the coordinates here is Lat and Lng position that the click from the maps. Is that possible to do that ? and are there some articles as guide for me to do that ?
use onTap callback of GoogleMap widget like below to get coordinates from map where use clicks.
GoogleMap(
onTap: (LatLng latLng) {
final lat = latLng.latitude;
final long = latLng.longitude;
},
);

flutter google map marker for current position

How could I have goolge map's style marker for current location , it can even show the direction of current position ?
this is my code, the marker style of my code is just a pin, i want
to have google map's marker style even to show the direction of current position .
_markerSet.add(
Marker(
markerId: MarkerId('current_Postion'),
infoWindow: InfoWindow(title: 'Current Position'),
position: _userCurrentPositionLatLng,
icon: BitmapDescriptor.defaultMarkerWithHue(
BitmapDescriptor.hueGreen,
),
),
This is how my marker looks like
enter image description here
This is the google marker which i want to have
enter image description here
Any hints for suggestions?
Edit:
Set myLocationEnabled: true, on the GoogleMap widget. It will automatically take care of that.
This works for a custom marker:
You cannot directly get the location from Google Map.
You can use this location package to get the users location (latitude and longitude) and then add the marker accordingly to the map.

How to remove a specific marker from google maps in flutter?

I am Vaibhav Pathak. I am working on a Flutter app in which I add markers in-app based on data change in firestore database and I want to remove the previous marker from the map using its marker id but I don't understand how to do it. I had watched many Youtube videos and blogs but I don't get to know this because of API changes in google maps flutter plugin. For your kind information, I want to tell that I am using the latest version of the google_maps flutter plugin.
My Code for Making Markers :
showLiveLocation(LatLng latLng) {
_markers.add(
Marker(
markerId: MarkerId(latLng.toString()),
position: latLng,
draggable: false,
infoWindow: InfoWindow(
title: "Live Order Location",
snippet: "Dear customer your order is live at this place."),
icon: liveLocation,
visible: true,
),
);
}
Thanks for everyone's help.
github : github#vkpdeveloper
You need to find that specific marker in your _markers list (e.g. by firstWhere()) and then remove it from the list of markers.
Edit:
Marker marker = _markers.firstWhere((marker) => marker.markerId.value == "myId",orElse: () => null);
setState(() {
_markers.remove(marker);
});
This will trigger a rebuild of your map where the marker is no longer included.
Inspired by Thomas, my solution was:
setState(() {
_markers.removeWhere((key, marker) => marker.markerId.value == "myMarkerId");
});