【发布时间】:2015-06-30 21:48:10
【问题描述】:
我正在开发一个 winform 应用程序,我想使用 google api v3 来计算多边形的重心。我的数据库中有 lng 和 lat。 我想在 c# 中调用 java 脚本函数我是新手,但我发现使用 HtmlElement 可以帮助我做到这一点,这是我尝试过的代码,它没有给出任何结果。
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);
【问题讨论】:
-
尝试添加第二个脚本标签,而不是在这里使用
scriptElIHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;如果你设置了 src 属性,那么 script 标签内的代码将被忽略 -
第二个脚本标签当我这样做时我有一个错误告诉我谷歌是未定义的
-
您应该从在您的 C# 代码中集成简单的 javascript 函数调用开始,一旦它起作用,就可以与谷歌库完全集成。请检查此答案是否对您有帮助:stackoverflow.com/questions/5731224/…
-
谢谢我找到了 C# 的 GMAP,所以我就用它了 :)
标签: javascript c# winforms htmlelements