Nadk
Nadk

Reputation: 173

Google Maps API with polygon

(excuse me for my bad english) the tops of my polygon are stored into my database (latitude, longitude). so I recovered the latitude and longitude in a table and then I draw my polygon. my problem is that with 3 tops it works fine, but when it is more then 3 it gives this result:

Map

this is my code :

success: function(data) {
                             //   alert(data); 
                    var tableauPointsPolygone = new Array ();                          
                    var j=0;
                    var somme=0;
                    $.each(data, function(i, item) {
                                tableauPointsPolygone.push(new google.maps.LatLng(item.latitude, item.longitude));
                                somme+= parseInt(item.valeur);
                                j++;
                                addMarker(item.latitude,item.longitude,item.adr);
                                center = bounds.getCenter();
                                map.fitBounds(bounds);
                    // alert(item.latitude);
});
var monPolygone = new google.maps.Polygon();
monPolygone.setPaths( tableauPointsPolygone );
monPolygone.setMap( map );
if (somme/j<5)
monPolygone.setOptions({strokeWeight: 3,strokeColor: '#582900' ,fillColor: '#1FA055'});
else
monPolygone.setOptions({strokeWeight: 3,strokeColor: '#582900' ,fillColor: '#A91101'});
                                },

Upvotes: 6

Views: 421

Answers (1)

natchkebiailia
natchkebiailia

Reputation: 631

The problem I see is the arrangement of polygon vertices. In case if 3 the order of point is not essential its triangle but in case of 4 and more you have to sort the points and then push into array.

Here is a helpful question about convex (non-concave) polygon

Upvotes: 1

Related Questions