C#Windows窗体应用程序显示谷歌地图v3 [英] C# Windows Forms Application displaying Google Maps v3

查看:95
本文介绍了C#Windows窗体应用程序显示谷歌地图v3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用WebBrowser控件的简单C#Windows窗体应用程序中使用Google地图?



据我所知Google Maps API v3不再需要密钥=我不需要外部专用网站&来自Google的密钥。



此处给出的示例( http ://www.codeproject.com/KB/cs/GpsMapping.aspx )非常接近,但显示的是完整的谷歌地图网站,而不是仅限地图的版本(例如:http://code.google.com/apis/maps/documentation/javascript/examples/map-coordinates.html )。



我是否错过了关于C#Forms应用程序中简单API v3集成的重点? :confused:

解决方案

非常感谢您的帮助shakil0304003。但是,我之前在搜索解决方案时已经遇到过这两篇文章。



不幸的是,虽然第一篇参考文章讨论了用于PDA的.Net CompactFramework并且编码很复杂,但第二篇参考文献仍然存在我无法回答的相同问题(在C#WinForm应用程序中通过WebBrowser控件自定义GoogleMaps)。



Sofar我得到(不那么优雅)代码版本:

 private void button1_Click (对象发件人,EventArgs e)
{
// UpdateGoogleMap();
StringBuilder queryAddress = new StringBuilder();
//在webbroswer控件中打开本地存储的html文件,而不是非常优雅的
queryAddress.Append(C:\\GMtest\\ GoogleMapsV3.html);

//打开Goog​​le的示例代码,这需要将HTML代码托管在Web服务器上(不可接受)。
queryAddress.Append(http://code.google.com/apis/maps/documentation/javascript/examples/map-coordinates.html);

//还有其他可能的选择吗?

this.webBrowser1.Navigate(queryAddress.ToString());
}

选项二是直接注入html代码(脏aproach ?,所有JScript智能必须手动插入/编码):

< preprivate void =eventargs =mode =hold/> {
object empty = System.Reflection.Missing.Value;
axWebBrowser1.Navigate(about:blank,ref为空,ref为空,ref为空,ref为空);
// Manuall注入HTML代码而不是使用本地存储的文件:C:\ GMtest \ GoogleMapsV3.html
setBrowserHtml(< h1> Hello world!< / h1>);
}



是否有任何优雅而简单的解决方案可以实现我的目标?


你可以看到这个链接


点击


点击


下面是代码,我在一个项目中使用



 #RegionGoogleMap
Dim GoogleMapLocation As String =
私有 Sub LoadGoogleMap( ByVal 位置作为 字符串
如果 GoogleMapLocation =位置那么 退出 Sub
GoogleMapLocation =位置
Dim a As String = & vbCrLf& _
< html>& vbCrLf& _
< head>& vbCrLf& _
< meta name =viewportcontent =initial-scale = 1.0, user-scalable = no/>& vbCrLf& _
& vbCrLf& _
< script type =text / javascriptsrc =http:// maps.google.com.mx/maps/api/js?sensor=true&language=en\"\"></script>& vbCrLf& _
< script type =text / javascript>& ; vbCrLf& _
& vbCrLf& _
var geocoder;& vbCrLf& _
var map;& vbCrLf& _
& vbCrLf& _
& vbCrLf& _
function initialize(address){& vbCrLf& _
& vbCrLf& _
geocoder = new google.maps.Geocoder();& vbCrLf& _
& vbCrLf& _
var myOptions = {zoom:16,mapTypeId:google.maps.MapTypeId.ROADMAP}& vbCrLf& _
& vbCrLf& _
geocoder.geocode({'address':(地址?地址:& Location& )},function(results,status){& vbCrLf& _
if(status == google.maps.GeocoderStatus.OK){& vbCrLf& _
map.setCenter(results [0] .geometry.location);& vbCrLf& _
var marker = new google.maps.Marker({& vbCrLf& _
map:map,& vbCrLf& _
position:results [0] .geometry.location& vbCrLf& _
});& vbCrLf& _
} else {& vbCrLf& _
& vbCrLf& _
}& vbCrLf& _
});& vbCrLf& _
& vbCrLf& _
map = new google.maps.Map(document.getElementById(map_canvas), myOptions);& vbCrLf& _
}& vbCrLf& _
& vbCrLf& _
& vbCrLf& _
< / script>& vbCrLf& _
< / head>& vbCrLf& _
< body onload =initialize()>& ; vbCrLf& _
< div id =map_canvas =style =>< / div>& vbCrLf& _
& vbCrLf& _
< / body>& vbCrLf& _
< / html>
DisplayHtml(a)
结束 Sub

私人 Sub DisplayHtml( ByVal html As 字符串
WebBrowser1.Navigate( about:blank
如果 WebBrowser1.Document IsNot Nothing 然后
WebBrowser1.Document.Write( String .Empty)
结束 如果
WebBrowser1.DocumentText = html
结束 Sub
#End Region


How can Google Maps be used in a simple C# Windows Forms Application utilising a WebBrowser control?

As I understand Google Maps API v3 no longer requires a key = I won't need an external dedicated web site & key from Google.

The example given here (http://www.codeproject.com/KB/cs/GpsMapping.aspx) gets pretty close but is displaying the full fat google maps web site instead of a map only version (eg: http://code.google.com/apis/maps/documentation/javascript/examples/map-coordinates.html).

Am I missing an important point regarding simple API v3 integration in C# Forms applications? :confused:

解决方案

Many thanks for your help shakil0304003. However, I already came across both articles previously in my search for a solution.

Unfortunately, while the 1st reference talks about a .Net CompactFramework for PDAs and is complex in it's coding, the 2nd reference leaves the same question that I've got unanswered (customised GoogleMaps through WebBrowser control in a C# WinForm application).

Sofar I got to (not so elegant) code versions:

private void button1_Click(object sender, EventArgs e)
{
    // UpdateGoogleMap();
    StringBuilder queryAddress = new StringBuilder();
    // Open locally stored html file in webbroswer control, not very ellegant at all
    queryAddress.Append("C:\\GMtest\\GoogleMapsV3.html");

    //Open Google's example code, this requires the HTML code to be hosted on a web server (not acceptable).
    queryAddress.Append("http://code.google.com/apis/maps/documentation/javascript/examples/map-coordinates.html");

    // Any other possible option? 

    this.webBrowser1.Navigate(queryAddress.ToString());
}

Option two was to directly inject html code (dirty aproach?, everything JScript wise would have to be manually inserted/coded):

<preprivate void="" eventargs="" mode="hold" />{
    object empty = System.Reflection.Missing.Value;
    axWebBrowser1.Navigate("about:blank", ref empty, ref empty, ref empty, ref empty);
    // Manuall inject HTML code instead of using a locally stored file: C:\GMtest\GoogleMapsV3.html 
    setBrowserHtml(" <h1>Hello world!</h1>");
}


Is there any elegant and simple solution to what I want to achieve?


You can see this links

Click

Click


Below is code, I am using in a project

#Region "GoogleMap"
    Dim GoogleMapLocation As String = ""
    Private Sub LoadGoogleMap(ByVal Location As String)
        If GoogleMapLocation = Location Then Exit Sub
        GoogleMapLocation = Location
        Dim a As String = "" & vbCrLf & _
    " <html>  " & vbCrLf & _
    " <head> " & vbCrLf & _
    " <meta name=""viewport"" content=""initial-scale=1.0, user-scalable=no"" /> " & vbCrLf & _
    "  " & vbCrLf & _
    " <script type=""text/javascript"" src=""http://maps.google.com.mx/maps/api/js?sensor=true&language=en""></script> " & vbCrLf & _
    " <script type=""text/javascript""> " & vbCrLf & _
    "  " & vbCrLf & _
    " var geocoder; " & vbCrLf & _
    " var map; " & vbCrLf & _
    "  " & vbCrLf & _
    "  " & vbCrLf & _
    " function initialize(address) { " & vbCrLf & _
    "  " & vbCrLf & _
        " geocoder = new google.maps.Geocoder();  " & vbCrLf & _
    "  " & vbCrLf & _
        "         var myOptions = {zoom: 16, mapTypeId: google.maps.MapTypeId.ROADMAP}  " & vbCrLf & _
    "  " & vbCrLf & _
        " geocoder.geocode({ 'address': (address ? address : """ & Location & """)}, function (results, status) { " & vbCrLf & _
            " if (status == google.maps.GeocoderStatus.OK) { " & vbCrLf & _
                " map.setCenter(results[0].geometry.location); " & vbCrLf & _
                " var marker = new google.maps.Marker({ " & vbCrLf & _
                    " map: map, " & vbCrLf & _
                    " position: results[0].geometry.location " & vbCrLf & _
                " }); " & vbCrLf & _
            " } else { " & vbCrLf & _
                "  " & vbCrLf & _
            " } " & vbCrLf & _
        " }); " & vbCrLf & _
    "  " & vbCrLf & _
        " map = new google.maps.Map(document.getElementById(""map_canvas""), myOptions); " & vbCrLf & _
    " } " & vbCrLf & _
    "  " & vbCrLf & _
    "  " & vbCrLf & _
    " </script> " & vbCrLf & _
    " </head> " & vbCrLf & _
    " <body onload=""initialize()""> " & vbCrLf & _
    "     <div id="" map_canvas="" style=""></div> " & vbCrLf & _
    "  " & vbCrLf & _
    "   </body> " & vbCrLf & _
    " </html> "
        DisplayHtml(a)
    End Sub

    Private Sub DisplayHtml(ByVal html As String)
        WebBrowser1.Navigate("about:blank")
        If WebBrowser1.Document IsNot Nothing Then
            WebBrowser1.Document.Write(String.Empty)
        End If
        WebBrowser1.DocumentText = html
    End Sub
#End Region


这篇关于C#Windows窗体应用程序显示谷歌地图v3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆