Flutter GoogleMaps - dynamic colouring of Custom Markers - google-maps

Flutter Google Maps allows the use of the standard/default Marker to be used on the map. You can also apply a colour to this marker programatically to add a variety of colours to your app.
However, what I can't seem to do is use this same technique to dynamically apply a colour to a custom (white) marker. Rather than make 10 custom markers with different colours I would prefer to have a plain white one, and dynamically colour it with a Hue or colour blend.
Default Marker
bool bToggle = true;
_markers.add(Marker(
markerId: MarkerId(markerid),
position: newPos,
onTap: () {
...
},
infoWindow: InfoWindow(
...
),
icon: BitmapDescriptor.defaultMarkerWithHue(
(bToggle) ? BitmapDescriptor.hueYellow : BitmapDescriptor.hueRed
),
));
Custom Marker
_markers.add(Marker(
markerId: MarkerId(markerid),
position: newPos,
onTap: () {
...
},
infoWindow: InfoWindow(
...
),
icon: BitmapDescriptor.fromAssetImage(ImageConfiguration(devicePixelRatio: 2.5), 'assets/markers/pin-blue.png')
),
));
Many thanks

The reason why the marker doesn't change its color to either BitmapDescriptor hues is because the screen needs to be rebuilt to detect the value change on bToggle. What you can do here is call setState() on value change.

Related

Unable to drag marker on google map in flutter

I'm using google_maps_flutter: ^0.5.24+1 plugin. I can't drag the marker inside map even though draggable property is set true. Whats wrong here? My code :
return GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(placemark.position.latitude,
placemark.position.longitude),
zoom: 18,
),
markers: Set<Marker>.of(
<Marker> [
Marker(
markerId: MarkerId("home"),
position: LatLng(placemark.position.latitude, placemark.position.longitude),
icon: BitmapDescriptor.defaultMarker,
infoWindow: InfoWindow(
title: placemark.name
),
draggable: true,
),
]
),
onMapCreated: (mapController) {
googleMapController = mapController;
},
);
You need to long press the Marker to trigger the drag event. To debug, you can use the onDrag() property to see if the drag event has been started.

How to determine which marker was pressed on the map

I am unable to determine which marker was tapped by the user on Google maps. I've added onTap: _onMarkerTapped(MarkerId markerId) event where I try to open another page with a more detailed Google Map based on which marker user has tapped. When I add bunch of markers (as a Set of markers) to my Google map, I do use unique markerId and that's what I pass in as an argument to a private custom method _onMarkerTapped(). The issue is, inside thsi event I always ended up getting the last markerId i.e. the last marker which I added to the Set. Is there any way to get the current markerid which user has tapped?
add onTap event handler for every marker I added in a set of Markers
for (var campusData in Campuses.campusLocations) {
campusDetails = json.decode(campusData);
campus = Campus.fromJson(campusDetails);
markerId = MarkerId(i.toString());
mrkr = new Marker(
markerId: markerId, // a string for marker unique id
icon: BitmapDescriptor.fromAsset(
'assets/wsu#2x.png'), // options for hues and custom imgs
position:
LatLng(campus.latitude, campus.longitude), // lat and long doubles
onTap: () {
_onMarkerTapped(markerId);
});
campusMarkers.add(mrkr);
i++;
}
return GoogleMap(
initialCameraPosition: cameraPosition,
onMapCreated: _onMapCreated,
mapType: MapType.normal,
markers: campusMarkers,
onTap: (lng) => _onMapTapped(lng),
);
void _onMarkerTapped(MarkerId markerId) {
final Marker tappedMarker = campusMarkers.elementAt(int.parse(markerId.value));
if (tappedMarker != null) {
}
else{
print("Tapped marker is NULL..");
}
}
Looking for ways to determine which marker was tapped by the user
Pay attention to these two line:
markerId = MarkerId(i.toString());
...
onTap: () {
_onMarkerTapped(markerId);
}
You are not creating a uniqueMarkerId for each loop, you are just reassigning it, so the last one you assigned will always be used by _onMarkerTapped(markerId). To fix this, use:
final markerId = MarkerId(i.toString());
For anyone who come across this issue, please follow the code example at https://github.com/flutter/plugins/blob/master/packages/google_maps_flutter/example/lib/place_marker.dart .. especially the _add() method and _onMarkerTapped() methods. PLEASE make sure to use a Map to declare your set of markers e.g. Map<MarkerId, Marker> markers = <MarkerId, Marker>{};

Adding markers dynamically to flutter google map

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_flutter: ^0.5.7
I understand that adding markers to the map works like this:
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
Marker _marker = new Marker(
icon: BitmapDescriptor.defaultMarker,
markerId: _markerId,
position: LatLng(34.024441,-5.0310968),
infoWindow: InfoWindow(title: "userMarker", snippet: '*'),
);
GoogleMap _map;
#override
void initState(
markers[_markerId] = _marker;
_map = new GoogleMap(
/* other properties irrelevant to this prob */
markers: Set<Marker>.of(_markers.values),
);
);
The above snippet does work, I get to see the marker on the map. But modifying the marker or trying to add another marker like in the snippet below does not work.
FloatingActionButton(
onPressed: () {
setState(() {
_marker = new Marker(
icon: BitmapDescriptor.defaultMarker,
markerId: _markerId,
position: LatLng(currentLocation.latitude, currentLocation.longitude),
infoWindow: InfoWindow(title: "userMarker", snippet: '*'),
onTap: () {
debugPrint("Marker Tapped");
},
);
markers[_markerId] = _marker; // What I do here is modify the only marker in the Map.
});
markers.forEach((id,marker) { // This is used to see if the marker properties did change, and they did.
debugPrint("MarkerId: $id");
debugPrint("Marker: [${marker.position.latitude},${marker.position.longitude}]");
});
});
)
My intention here is using another plugin (geoLocator) to get location data of the user and change the only marker I have so it can track his movements. The debugPrint shows that the data is indeed changing, but I see no change in the map (the initial marker that I change uses a different location than my own when I test).
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.

How to customize myLocationEnabled button on Google Maps?

I need to create a button which is going to show me the current location of the user. That's why I use Google Maps, which have the options such this one. But, I need to customize the MyLocation Button but don't know how to do it. Could you please help me with this?
I am pretty new in Flutter though :D
I couldn't find easy way of doing it, but since everything is widget in flutter, you can put your google maps in your stack, and add iconbutton or whatever custom button you need to your stack.
GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition:
CameraPosition(target: LatLng(0.0, 0.0)),
markers: markers,
),
IconButton(
icon: Icon(Icons.battery_charging_full),
onPressed: () async {
final center = await getUserLocation();
getNearbyPlaces(center);
Marker myPosition = Marker(
markerId: MarkerId('myLocation'),
position: center == null
? LatLng(0, 0)
: LatLng(center.latitude, center.longitude),
icon: BitmapDescriptor.fromAsset(
'assets/logo.png'));
setState(() {
markers.add(myPosition);
});
},
),
],
)
So what I'm doing here is basicly,
I have Stack which helps me put a IconButton over GoogleMap. When user presssed on that button, I add a new Marker to show current position, There is a Set of Markers on my State, called markers, I'm creating a new Marker by getting the current location of the user, and adding a custom icon to that marker, and then add it to my markers(Set), to display it on GoogleMap.
my getUserLocation function:
Future<LatLng> getUserLocation() async {
LocationManager.LocationData currentLocation;
final location = LocationManager.Location();
try {
currentLocation = await location.getLocation();
final lat = currentLocation.latitude;
final lng = currentLocation.longitude;
final center = LatLng(lat, lng);
return center;
} on Exception {
currentLocation = null;
return null;
}
}
I have location: ^2.1.0 package and using it as LocationManager import 'package:location/location.dart' as LocationManager;

Stop google maps markers from having pointer cursor style on hover?

How can I stop my google maps markers from having the pointer cursor style on hover? Currently the markers behave like links in that if you hover the mouse over them the cursor changes to a hand.
Here is the code that creates the marker:
function addMarkerLargeMap(latlng, myTitle, html, price) {
markers.push(new google.maps.Marker({
position: latlng,
map: map,
title: myTitle,
icon: "path-to-my-icon.png",
html: "<div style='width:100px;height:auto'>" + html + "</div>",
}));
Set clickable to false in the marker options
function addMarkerLargeMap(latlng, myTitle, html, price) {
markers.push(new google.maps.Marker({
position: latlng,
clickable: false,
map: map,
title: myTitle,
icon: "path-to-my-icon.png",
html: "<div style='width:100px;height:auto'>" + html + "</div>",
}));
Alternatively, you can set the cursor property in the marker options, if you want to use a specific type of cursor instead of the default. e.g.
cursor: 'crosshair'
Although this only seems to work if the marker is clickable.
#main_map img {cursor:default!important;}
I am using the Google Maps JavaScript API V3 Reference. As of this date, 2022-08-22, version 3.47 is the oldest for which documentation exists.
The MarkerOptions interface supports a cursor property. This supports any CSS cursor property value. As I wanted to use the grabby-hand icon that is the default of the Google Map so that users know they can grab and drag the map around, I used cursor: "grab" in my Marker constructor options:
new google.maps.Marker({
cursor: "grab",
icon: "path-to-my-icon.png",
map: map,
position: latlng,
title: myTitle
});