0

I wanted to add a circle and some markers with Google Maps via Flutter. I found some resource . And I simplified it to:

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class CircleScreen extends StatefulWidget {
  @override
  _CircleScreenState createState() => _CircleScreenState();
}

class _CircleScreenState extends State<CircleScreen> {
  GoogleMapController? mapController;

  var lng, lat, loading;
  bool sitiosToggle = false;

  Set<Marker> allMarkers = Set();

  void _onMapCreated(GoogleMapController controller) {
    mapController = controller;
  }

  Set<Circle>? circles;

  initMarker() {
    allMarkers.add(Marker(
      markerId: MarkerId("mymarker1"),
      draggable: false,
      infoWindow: InfoWindow(title: "mymarker1", snippet: "mymarker1"),
      position: LatLng(45.749122, 17.659750),
    ));

    allMarkers.add(Marker(
      markerId: MarkerId("mymarker2"),
      draggable: false,
      infoWindow: InfoWindow(title: "mymarker2", snippet: "mymarker2"),
      position: LatLng(45.770854, 17.626963),
    ));

    allMarkers.add(Marker(
      markerId: MarkerId("mymarker3"),
      draggable: false,
      infoWindow: InfoWindow(title: "mymarker3", snippet: "mymarker3"),
      position: LatLng(45.776937, 17.637949),
    ));

    allMarkers.add(Marker(
      markerId: MarkerId("mymarker4"),
      draggable: false,
      infoWindow: InfoWindow(title: "mymarker4", snippet: "mymarker4"),
      position: LatLng(45.739266, 17.637434),
    ));
  }

  @override
  initState() {
    initMarker();
    circles = Set.from([
      Circle(
          circleId: CircleId("myCircle"),
          radius: 2000,
          center: LatLng(45.729366, 17.627534),
          fillColor: Color.fromRGBO(171, 39, 133, 0.35),
          strokeColor: Color.fromRGBO(171, 39, 133, 0.5),
          onTap: () {
            print('circle pressed');
          })
    ]);

    loading = true;
    lat = 45.759266;
    lng = 17.657434;
    loading = false;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: new AppBar(
          centerTitle: true,
          title: Text(
            'YOUR NEAREST STORES',
            textAlign: TextAlign.center,
          ),
        ),
        body: Stack(
          children: <Widget>[
            loading == false
                ? GoogleMap(
              mapType: MapType.normal,
              circles: circles!,
              myLocationButtonEnabled: true,
              myLocationEnabled: true,
              onMapCreated: _onMapCreated,
              zoomGesturesEnabled: true,
              compassEnabled: true,
              scrollGesturesEnabled: true,
              rotateGesturesEnabled: true,
              tiltGesturesEnabled: true,
              initialCameraPosition: CameraPosition(
                target: LatLng(lat, lng),
                zoom: 12.0,
              ),
              markers: allMarkers,
            )
                : Center(
              child: CircularProgressIndicator(),
            ),
          ],
        ),
      ),
    );
  }
}

And the result is okay for me:

result

My question is; I have allMarkers (which includes these 4 markers) as shown on code and only the 4th marker is in the circle. How can I write a function that returns a boolean value regarding is Xth marker in the circle or not? For example; isInCircle(allMarkers[0]) will give false and isInCircle(allMarkers[3]) will give true.

Note: There will many markers and the user can be in any location.

I also found geofencing but i dont know is this real answer, I didn't get it.

Can you help me please?

Wicaledon
  • 710
  • 1
  • 11
  • 26

0 Answers0