Closed the100rabh closed 2 years ago
It works quite well if I put the LocationMarkerLayerWidget before MarkerClusterLayerWidget.
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
import 'package:flutter_map_marker_cluster/flutter_map_marker_cluster.dart';
import 'package:latlong2/latlong.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final PopupController _popupController = PopupController();
List<Marker> markers = [];
int pointIndex = 0;
List points = [
LatLng(51.5, -0.09),
LatLng(49.8566, 3.3522),
];
@override
void initState() {
pointIndex = 0;
markers = [
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: points[pointIndex],
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(53.3498, -6.2603),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(53.3488, -6.2613),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(53.3488, -6.2613),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(48.8566, 2.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(49.8566, 3.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(49.8566, 3.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(49.8566, 3.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(49.8566, 3.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(49.8566, 3.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(49.8566, 3.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(49.8566, 3.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(49.8566, 3.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(49.8566, 3.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(49.8566, 3.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(49.8566, 3.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(49.8566, 3.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(49.8566, 3.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
];
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
pointIndex++;
if (pointIndex >= points.length) {
pointIndex = 0;
}
setState(() {
markers[0] = Marker(
point: points[pointIndex],
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
builder: (ctx) => Icon(Icons.pin_drop),
);
markers = List.from(markers);
});
},
child: Icon(Icons.refresh),
),
body: FlutterMap(
options: MapOptions(
center: points[0],
zoom: 5,
maxZoom: 15,
plugins: [
MarkerClusterPlugin(),
],
onTap: (_, __) => _popupController
.hideAllPopups(), // Hide popup when the map is tapped.
),
children: <Widget>[
TileLayerWidget(
options: TileLayerOptions(
urlTemplate: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: ['a', 'b', 'c'],
),
),
LocationMarkerLayerWidget(),
MarkerClusterLayerWidget(
options: MarkerClusterLayerOptions(
spiderfyCircleRadius: 80,
spiderfySpiralDistanceMultiplier: 2,
circleSpiralSwitchover: 12,
maxClusterRadius: 120,
rotate: true,
size: Size(40, 40),
anchor: AnchorPos.align(AnchorAlign.center),
fitBoundsOptions: FitBoundsOptions(
padding: EdgeInsets.all(50),
maxZoom: 15,
),
markers: markers,
polygonOptions: PolygonOptions(
borderColor: Colors.blueAccent,
color: Colors.black12,
borderStrokeWidth: 3),
popupOptions: PopupOptions(
popupSnap: PopupSnap.markerTop,
popupController: _popupController,
popupBuilder: (_, marker) => Container(
width: 200,
height: 100,
color: Colors.white,
child: GestureDetector(
onTap: () => debugPrint('Popup tap!'),
child: Text(
'Container popup for marker at ${marker.point}',
),
),
)),
builder: (context, markers) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
color: Colors.blue),
child: Center(
child: Text(
markers.length.toString(),
style: TextStyle(color: Colors.white),
),
),
);
},
),
),
],
),
);
}
}
I can confirm it works for me, it must be an issue with layers ordering :+1:
That is a strange behavior. Probably should be document somewhere. Please feel free to close it.
Hi,
I tried to use this with flutter_map_marker_cluster plugin but after this markers are not visible. Can someone help me why this might be happening. Following code either does not show the location marker or the marker cluster is not clickable. Not entirely sure who is to be blamed for this but it would be great if someone could find a fix for this for me.