3

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);
  • try add second script tag instead use `scriptEl` here `IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;` if you set _src_ attribute then code inside _script_ tag will be ignored – Grundy Apr 23 '15 at 16:15
  • second script tag when I do that I have an error telling me the google is undefined – gnaoui faiza Apr 24 '15 at 16:30
  • 1
    You should start with integration of simple javascript function call int your C# code, once it works, go to full integration with the google library. pls check if this answer can help you: http://stackoverflow.com/questions/5731224/calling-javascript-function-from-codebehind – Rami Yampolsky Jul 04 '15 at 12:18
  • Thanks I found GMAP for C# so I just used it :) – gnaoui faiza Jul 20 '15 at 09:13

1 Answers1