Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to check if the retrieved points are inside polygon #13

Open
MusaMsigwa opened this issue Sep 12, 2021 · 0 comments
Open

to check if the retrieved points are inside polygon #13

MusaMsigwa opened this issue Sep 12, 2021 · 0 comments

Comments

@MusaMsigwa
Copy link

MusaMsigwa commented Sep 12, 2021

I started by checking if the point is inside polygon and it worked well. my goal is to allow a user to draw a polygon then to use that list to check if there is any point that is inside drawn polygon. I archeved to allow user draw the polygon now I want to check if there is any point inside that polygon.

the list of latLang that make the polygon is
List<LatLng> _userPolyLinesLatLngList = [];

i want to create new list that i can use for map tool kit that List should have LatLang from above list.

Help me to convert that List so as I can assign as List of latlang for toolkit. therefore what I want is two variable of List the above List and new List that i can use on toolkit to check if the point is inside the polygon.

         ` //something like this
          List<LatLng> _userPolyLinesLatLngList = [];
          List<mp.LatLng> newListForToolKit = _userPolyLinesLatLngList;
          //this brings error` //then 
          final points = mp.PolygonUtil.containsLocation(
              mp.LatLng(-6.7924, 39.2083), newListForToolKit, true);

              `

And Here are the complete code that allow user to draw polygon. I want to check if the point is inside polygon.
`import 'dart:async';
import 'dart:collection';
import 'dart:convert';
import 'dart:io';
import 'dart:math' as Math;

import 'package:flutter/material.dart';
import 'package:geocoding/geocoding.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:maps_toolkit/maps_toolkit.dart' as mp;

class MyHomePageMap extends StatefulWidget {
MyHomePageMap({Key? key}) : super(key: key);

@OverRide
_MyHomePageMapState createState() => _MyHomePageMapState();
}

class _MyHomePageMapState extends State {
static final Completer _controller = Completer();

static final CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(37.42796133580664, -122.085749655962),
zoom: 14.4746,
);

final Set _polygons = HashSet();
final Set _polyLines = HashSet();

bool _drawPolygonEnabled = false;
List _userPolyLinesLatLngList = [];
// List<mp.LatLng> _userPolyLinesLatLngListsUser = [];

final pointFromGoogleMap = LatLng(90, 0);

bool _clearDrawing = false;
int? _lastXCoordinate, _lastYCoordinate;

var distanceBetweenPoints = mp.SphericalUtil.computeDistanceBetween(
mp.LatLng(51.5073509, -0.1277583), mp.LatLng(48.856614, 2.3522219));

final cityLondon = mp.LatLng(51.5073509, -0.1277583);
final cityParis = mp.LatLng(48.856614, 2.3522219);

final distance = mp.SphericalUtil.computeDistanceBetween(
mp.LatLng(51.5073509, -0.1277583), mp.LatLng(48.856614, 2.3522219)) /
1000.0;
// List polygony = [
// (37.43296265331129, -122.08832357078792),
// (37.43006265331129, -122.08832357078792),
// (37.43006265331129, -122.08332357078792),

// ];

// print('Distance between London and Paris is $distance km.');
// PolygonUtil.containsLocation - computes whether the given point lies inside the specified polygon.
// final distances = mp.SphericalUtil.computeDistanceBetween(mp.LatLng(51.5073509, -0.1277583), 4.3)

List<mp.LatLng> polygonCoord = [
mp.LatLng(37.43296265331129, -122.08832357078792),
mp.LatLng(37.43006265331129, -122.08832357078792),
mp.LatLng(37.43006265331129, -122.08332357078792),
mp.LatLng(37.43296265331129, -122.08832357078792)
];

@OverRide
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onPanUpdate: (_drawPolygonEnabled) ? _onPanUpdate : null,
onPanEnd: (_drawPolygonEnabled) ? _onPanEnd : null,
child: GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _kGooglePlex,
polygons: _polygons,
polylines: _polyLines,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
onPressed: () {
print("The DISTANCE IS $distance");
final points = mp.PolygonUtil.containsLocation(
mp.LatLng(-6.7924, 39.2083), polygonCoord, true);
print("The POIT IS FOUND ON THE FOLLOWING $points");
// final a = mp._userPolyLinesLatLngList;
// print("The POIT IS FOUND ON THE FOLLOWING $a YES");

          List<mp.LatLng> polygonCoords = [
            mp.LatLng(37.43296265331129, -122.08832357078792),
            mp.LatLng(37.43006265331129, -122.08832357078792),
            mp.LatLng(37.43006265331129, -122.08332357078792),
            mp.LatLng(37.43296265331129, -122.08832357078792)
          ];
          List<LatLng> _userPolyLinesLatLngList = [];
          // ignore: unused_local_variable
          double a, b;

          // var b;
          // var lat;
          // List<mp.LatLng> _userPolyLinesLatLngListsUser =
          //     _userPolyLinesLatLngList;

          final pointFromGoogleMap = LatLng(90, 0);
          final pointMp = mp.LatLng(
              pointFromGoogleMap.latitude, pointFromGoogleMap.longitude);

          print("The POIT IS FOUND ON THE FOLLOWING $points YES");
          if (points == true) {
            print("The POIT IS FOUND ON THE FOLLOWING $points YES");
          } else {
            print("The POIT IS FOUND ON THE FOLLOWING $points No");
          }
        },
        tooltip: 'Drawing',
        child: Icon(Icons.menu),
      ),
      FloatingActionButton(
        onPressed: _toggleDrawing,
        tooltip: 'Drawing',
        child: Icon((_drawPolygonEnabled) ? Icons.cancel : Icons.edit),
      ),
    ],
  ),
);

}

_toggleDrawing() {
_clearPolygons();
setState(() => _drawPolygonEnabled = !_drawPolygonEnabled);
}

_onPanUpdate(DragUpdateDetails details) async {
// To start draw new polygon every time.
if (_clearDrawing) {
_clearDrawing = false;
_clearPolygons();
}

if (_drawPolygonEnabled) {
  double? x, y;
  if (Platform.isAndroid) {
    // It times in 3 without any meaning,
    // We think it's an issue with GoogleMaps package.
    x = details.globalPosition.dx * 3;
    y = details.globalPosition.dy * 3;
  } else if (Platform.isIOS) {
    x = details.globalPosition.dx;
    y = details.globalPosition.dy;
  }

  // Round the x and y.
  int xCoordinate = x!.round();
  int yCoordinate = y!.round();

  // Check if the distance between last point is not too far.
  // to prevent two fingers drawing.
  if (_lastXCoordinate != null && _lastYCoordinate != null) {
    var distance = Math.sqrt(Math.pow(xCoordinate - _lastXCoordinate!, 2) +
        Math.pow(yCoordinate - _lastYCoordinate!, 2));
    // Check if the distance of point and point is large.
    if (distance > 80.0) return;
  }

  // Cached the coordinate.
  _lastXCoordinate = xCoordinate;
  _lastYCoordinate = yCoordinate;

  ScreenCoordinate screenCoordinate =
      ScreenCoordinate(x: xCoordinate, y: yCoordinate);

  final GoogleMapController controller = await _controller.future;
  LatLng latLng = await controller.getLatLng(screenCoordinate);
  // mp.LatLng latLngs = await controller.getLatLng(screenCoordinate);
  try {
    // Add new point to list.
    _userPolyLinesLatLngList.add(latLng);
    // _userPolyLinesLatLngListsUser.add(latLngs);

    _polyLines.removeWhere(
        (polyline) => polyline.polylineId.value == 'user_polyline');
    _polyLines.add(
      Polyline(
        polylineId: PolylineId('user_polyline'),
        points: _userPolyLinesLatLngList,
        width: 2,
        color: Colors.blue,
      ),
    );
  } catch (e) {
    print(" error painting $e");
  }
  setState(() {});
}

}

// Yes, you cannot use Google Maps LatLng directly. You should convert Google Maps LatLng into Maps Toolkit LatLng, for ex.:

// import 'package:maps_toolkit/maps_toolkit.dart' as mp;
// import 'package:google_maps/google_maps.dart';
// import 'package:test/test.dart';

// void main() {
// final pointFromGoogleMap = LatLng(90, 0);
// final pointMp = mp.LatLng(pointFromGoogleMap.lat, pointFromGoogleMap.lng);

// bool result = mp.PolygonUtil.containsLocation(pointMp, TOTAL_COVERAGE, false);
// }

_onPanEnd(DragEndDetails details) async {
// Reset last cached coordinate
_lastXCoordinate = null;
_lastYCoordinate = null;

if (_drawPolygonEnabled) {
  _polygons
      .removeWhere((polygon) => polygon.polygonId.value == 'user_polygon');
  _polygons.add(
    Polygon(
      polygonId: PolygonId('user_polygon'),
      points: _userPolyLinesLatLngList,
      strokeWidth: 2,
      strokeColor: Colors.blue,
      fillColor: Colors.blue.withOpacity(0.4),
    ),
  );
  setState(() {
    _clearDrawing = true;
  });
}

}

_clearPolygons() {
setState(() {
_polyLines.clear();
_polygons.clear();
_userPolyLinesLatLngList.clear();
});
}
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant