Click here to Skip to main content
14,328,894 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

My Question is how to find lat long inside the drawn polygon or not i have mutliple records in my database , when app started then all markers visible and when we draw shape and apply then only marker inside the polygon show ,,this is very important for me please help me.

Thanks in advance

What I have tried:

$("#poly_apply").on("click", function (e) {
        $(".loader-container").show();
        records = [];
        polySides = myaary.length - 1;
        for (var i = 0; i < myaary.length; i++) {
            latarr.push(myaary[i].lat());
            lanarr.push(myaary[i].lng());
        };
        var minlt = Math.min.apply(null, latarr);
        var maxlt = Math.max.apply(null, latarr);
        var minln = Math.min.apply(null, lanarr);
        var maxln = Math.max.apply(null, lanarr);
        var vald = {
            mn_lt: minlt,
            mx_lt: maxlt,
            mn_ln: minln,
            mx_ln: maxln
        }

        $.getJSON("/MapSearch/Get_Result/", vald, function (data) {
            var filtered = []; 
           for (var j = 0; j < records.length; j++) {
            var item = records[j];

            if (pointInPolygon(polySides, latarr, lanarr, item.lat, item.lng)) {
              
 var marker = new google.maps.Marker({
                        position: new google.maps.LatLng(item.lat, item.lng),
                        icon: img.jpg,
                        title: 'Click me',
                        map: map                       
                    });
            }
        }
        });
    });
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;}
Posted
Updated 29-Jun-16 0:01am
v6
Comments
Patrice T 7-Jun-16 12:04pm
   
What is the problem in the code ?
ErBhati 8-Jun-16 6:00am
   
markers shows outside the drawn polygon.
Patrice T 8-Jun-16 8:04am
   
it means that pointInPolygon fails.
Try to set a square as polygon and test with a point inside and one outside.
ErBhati 9-Jun-16 6:13am
   
we can't restrict user to draw a square as polygon.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

by add this i found my solution:
function pointInPolygon(polySides, polyX, polyY, x, y) {
    polySides = polyX.length;
    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;
}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100