Search code examples
javascriptc#winformshtmlelements

Create a java script function in c#


I'm developing a winform application and I want to use the google api v3 to calculate the barycenter of a polygon. and I have the lng and lat in my database. I want to call a java script function in c# I'm new but I found that using HtmlElement can help me do that here is the code I tried it doesn't give any result.

WebBrowser webBrowser1 = new WebBrowser();
        string url = ("C:\\Users/guenafai/Desktop/TOPApplication/TOPApplication/BaryScripts.htm");
        Console.WriteLine("je suis laaaaaaaaaaaa");
        webBrowser1.Navigate(url);
        HtmlElement head = webBrowser1.Document.CreateElement("head");
        HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
        scriptEl.SetAttribute("src","https://maps.googleapis.com/maps/api/js?key=AIzaSyAXBy6YDvNZOu0TK2RkrDmbNEbN3gn1sVk");
        IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
        string script = @"
        function BaryC(){
            var boundss = new google.maps.LatLngBounds();
            var i;
            var polygonCoords = [";
            foreach (CONTRACT c in Clus)
            {
                script = script + @"
            new google.maps.LatLng(" + 45.501689 + "," + -73.567256 + @"),"
            }
            script = script + @"new google.maps.LatLng(" + 45.501689 + "," + -73.567256 + @")];

            for (i = 0; i < polygonCoords.length; i++) {
                boundss.extend(polygonCoords[i]);
            }
            var lat = boundss.getCenter().lat() ;
            var lng = boundss.getCenter().lng();
            return boundss.getCenter().toString() ;

        }";
        Console.WriteLine(script);
        element.text = script;
        head.AppendChild(scriptEl);
        string onclickstring = (string)webBrowser1.Document.InvokeScript("BaryC");
        Console.WriteLine(onclickstring);

Solution

  • I used this API for c# instead http://www.codeproject.com/Articles/32643/GMap-NET-Great-Maps-for-Windows-Forms-and-Presenta