Click here to Skip to main content
15,670,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

JavaScript
$("#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

by add this i found my solution:
JavaScript
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;
}
 
Share this answer
 
v2

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

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900