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);
4
  • 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 Commented Apr 23, 2015 at 16:15
  • second script tag when I do that I have an error telling me the google is undefined Commented Apr 24, 2015 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: stackoverflow.com/questions/5731224/… Commented Jul 4, 2015 at 12:18
  • Thanks I found GMAP for C# so I just used it :) Commented Jul 20, 2015 at 9:13

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.