1

I am trying to use Google's drawing tools (https://developers.google.com/maps/documentation/javascript/examples/drawing-tools), which allows me to draw on the map and get the coordinates.

Here is fiddle- https://jsfiddle.net/mg0t5x8n/2/

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: {lat: 52.908978, lng: -1.451871},
    zoom: 14
  });


  var drawingManager = new google.maps.drawing.DrawingManager({
    drawingMode: google.maps.drawing.OverlayType.POLYLINE,
    drawingControl: true,
    drawingControlOptions: {
      position: google.maps.ControlPosition.TOP_CENTER,
      // drawingModes: ['marker', 'circle', 'polygon', 'polyline', 'rectangle']
      drawingModes: ['polyline']
    },
    markerOptions: {icon: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'},
    circleOptions: {
      fillColor: '#ffff00',
      fillOpacity: 1,
      strokeWeight: 5,
      clickable: false,
      editable: true,
      zIndex: 1
    }
  });
  drawingManager.setMap(map);

  google.maps.event.addListener(drawingManager, 'polylinecomplete', function(line) {
    console.log(line.getPath().getArray().toString());
    alert(line.getPath().getArray().toString());
  });
};
initMap();

After connecting the last line with the start of the first line you drew, you get list of coordinates. After getting these coordinates i need to run a query in the database (where i have list of locations, with lat and lon values for each) which retrieves all the locations that fall within the given points.

I'm not sure if this is possible with sql or requires something different.

Sorry if the question is bit vague, I'm at a very early stage with this and just wanted to get some info see if its possible to do and if so how i would go about it.

****** UPDATE ******

After retrieving the lat and lon value of the polygon - I came across this php function(http://assemblysys.com/php-point-in-polygon-algorithm/#comment-1243). Its sort of doing what i was after - some modification needed. Might be helpful to someone else looking to achieve something similar.

S.Simkhada
  • 116
  • 1
  • 2
  • 13
  • It would be *much* much easier if you only allowed drawing a fixed shape, like a circle or rectangle. It can be difficult to determine if a point is inside a polygon, when that polygon can be any shape at all. – Reinstate Monica Cellio Apr 27 '17 at 12:02
  • Check this out, http://stackoverflow.com/questions/17073156/mysql-query-points-within-polygon-no-results – Mr. J Apr 27 '17 at 12:02
  • @Archer Yes, with fixed shapes like circle and rectangle i could work out the centre point and search for certain radius, but I'm just trying to to see if i can make it work with polygon. – S.Simkhada Apr 27 '17 at 12:14
  • Thanks @Mr.J i will check it – S.Simkhada Apr 27 '17 at 12:15
  • You should look into the maths behind what you're asking then, rather than a specific programming question. It may be more complex than you've expected. – Reinstate Monica Cellio Apr 27 '17 at 12:20
  • Google maps does actually have a function that will help you here. You need to ensure you include the geometry library when you include the maps api. Look at the containsLocation function (https://developers.google.com/maps/documentation/javascript/geometry#containsLocation). The means you can check any latLng location is within the drawn polygon. As for how to do it fully, you'll need to do something like get all of the potential locations from the db and then see if within the polygon - so far from ideal. – Paul Thomas Apr 27 '17 at 13:16
  • Thanks for the link @PaulThomasGC – S.Simkhada Apr 28 '17 at 15:26

0 Answers0