0

I know how to find near by locations from MySQL database using Round circle radius query and I have given answer of the same on another SO question as well here.

But I wish to do some different thing from this now. Here what happened is the query returns the result from center of the point which includes entire circle of radius. I wish to get points only of the half circle. I know this is possible and its all mathematical calculation and I am little weak in it that's why asking for experts help.

See this image, it will give very clear idea.Only front part location is needed, not the back side part. Need to ignore the back side part.

As you can see in the image only front part location is needed, not the back side part. Need to ignore the back side part. Also I have divided the radius in different color to make them appear as zones - like red is zone1, orange is zone 2 and yellow is zone 3. This are virtual zones to filter the data (locations).

All suggestions are welcome.

Community
  • 1
  • 1
Scorpion
  • 6,831
  • 16
  • 75
  • 123

1 Answers1

1

You can plot points inside a segment using Haversine/Spherical Law of Cosines for the radius. Then use pointInPolygon() to find only those within segment. You will also require function to create polygon.

polySides = number of sides in polygon
pointLatArr = Lat of point in in polygon array
pointLngArr = Lng of point in in polygon array
dat.lat = Lat from Haversine results
dat.lng = Lng from Haversine results

if (pointInPolygonpolySides,pointLatArr,pointLngArr,dat.lat,dat.lng)){
     var latlng = new google.maps.LatLng(dat.lat,dat.lng); 
     addMarker(latlng,dat.name);
     bounds.extend(latlng);
  }

 function pointInPolygon(polySides,polyX,polyY,x,y) {
 var j = polySides-1 ;
  oddNodes = 0;
  for (i=0; i<polySides; i++) {
    if (polyY[i]<y && polyY[j]>=y  ||  polyY[j]<y && polyY[i]>=y) {
        if (polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x)  {
            oddNodes=!oddNodes; 
        }
    }
   j=i; }

  return oddNodes;
}    

Function for segment polygon

function drawSegment(start,end,radius) { 
var d2r = Math.PI / 180;
pointLatArr = new Array();
pointLngArr = new Array();
polyLatLngs = new Array(); // latLngs of polygon
var polyLat = (radius /3963.189) / d2r; // miles
var polyLng = polyLat / Math.cos(center.lat() * d2r);
var centerLatLng = new google.maps.LatLng(center.lat(),center.lng());//Center to start
pointLatArr.push(center.lat());
pointLngArr.push(center.lng());
polyLatLngs.push(centerLatLng);
bounds.extend(centerLatLng);
// Create polygon points (extra point to close polygon)
for (var i = start; i < end; i++) {
    // Convert degrees to radians
    var theta = i * d2r;
    var pointLat = center.lat() + (polyLat * Math.sin(theta));
    var pointLng = center.lng() + (polyLng * Math.cos(theta));
    var pointLatLng = new google.maps.LatLng(
    parseFloat(pointLat), parseFloat(pointLng));
    polyLatLngs.push(pointLatLng);
    pointLatArr.push(pointLat);
    pointLngArr.push(pointLng);
    bounds.extend(pointLatLng);
}
var centerLatLng = new google.maps.LatLng(center.lat(),center.lng());//End to center
polyLatLngs.push(centerLatLng);
pointLatArr.push(center.lat());
pointLngArr.push(center.lng());
polySides = polyLatLngs.length;

Map using this technique enter image description here }

See Demo

david strachan
  • 7,174
  • 2
  • 23
  • 33