Assuming a populated C# class of CityPolygon, which contains a list of lat/lng coordinates
namespace MyProject.Global.Models
{
public class CityPolygon
{
//each city polygon contains a set of lat/lon double pairs, defining coordinates of the polygon.
public List<CityPolygonCoordinate> coordinatePoints = new List<CityPolygonCoordinate>();
}
public class CityPolygonCoordinate
{
public double lat;
public double lon;
}
}
And a Javascript method which takes in a GoogleMap, and list of coordinates
//draw a polygon given a set of coordinates. See: https://developers.google.com/maps/documentation/javascript/examples/polygon-simple
function drawMapPolygon(googleMap, coordsArry) {
// Construct the polygon.
const myPolygon = new google.maps.Polygon({
paths: coordsArry,
strokeColor: “#FF0000”,
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: “#FF0000”,
fillOpacity: 0.35,
});
myPolygon.setMap(googleMap);
}
How does one construct an object to pass into drawMapPolygon() Javascript method?
If I were passing a simple rectangle, I could do it like this.
Application.Call(“window.drawMapRectangle”, mobjGoogleMap, curCity.northlat, curCity.southlat, curCity.eastlon, curCity.westlon); // call javascript
But now we need a complex object, with an indeterminate number of coordinates.
I suspect it would involve defining a generic object, but I don’t know how to iterate through a List of CityPolygon objects, and then construct a generic object. Maybe it looks like this?
foreach (CityPolygonCoordinate cpc in cityPolygon)
{
//add lat, lng to generic object
}
var myGooglePolygon = new
{
new {
//set of coordinates that I need to add from the C# CityPolygon object
}
}
Application.Call(“window.drawMapPolygon”, mobjGoogleMap, cityPolgon);
I have passed simple variables to WiseJ javascript, but not complex ones like a list of coordinates. Thanks for any help you can provide…
The answer was this.
var coordsArr = new List<object>();
foreach (CityPolygonCoordinate cpc in cityPolygons[1].coordinatePoints)
{
//add lat, lng to generic object
var pt = new
{
lat = cpc.lat,
lng = cpc.lon
};
coordsArr.Add(pt);
}
and then calling .ToArray() before passing it into the WebMethod.
Application.Call(“window.drawMapPolygon”, mobjGoogleMap, coordsArr.ToArray());
also, the Chrome javascript debugger was useful in verifying that the object datatype matched that of the sample.
Please login first to submit.