Search code examples
vb.netgoogle-mapsbing-mapsbing-api

Adding Bing / Google Maps API in VB or C++


Hi I'm currently using a simple web search function to act as my mapping system, but I would like to add the maps directly into the application to make it look a lot more professional. I have added my original code which is in VB just so you can an understanding of what I already have working and so you can give me an idea on how to take it a step further, I'm also considering rewriting my application in C++.

Private Sub BTNMAP_Click(sender As Object, e As EventArgs) Handles BTNMAP.Click
    Automate = True

    Maps.Automate = Automate
    Maps.SearchInput = PostcodeTextBox.Text
    Maps.WebBrowser1.Navigate("https://www.bing.com")
    Maps.Show()
End Sub

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    If Automate = True Then Automate = False Else Exit Sub


    Dim txt As HtmlElement = WebBrowser1.Document.GetElementById("q")

    Dim btn As HtmlElement = WebBrowser1.Document.GetElementById("go")


    txt.SetAttribute("value", SearchInput)
    btn.InvokeMember("click")
End Sub

Alternatively I would like to change my Map.WebBrowser1.Navigate("https://www.bing.com/mapspreview") but I have been struggling to find the html elements for the text field and the button.

Thank you for help.


Solution

  • If you want to open up a web page in your app, you can find documentation on how to create URLs to Bing Maps here: https://msdn.microsoft.com/en-us/library/dn217138.aspx

    These URLs should work with the map preview as well, however, the preview site uses a lot of HTML5 and I doubt that it will work inside of a WebBrowser in a WPF or WinForm app as I believe the WebBrowser control emulates IE7 which doesn't support HTML5. There are ways to hack around this by changing registry settings: http://weblog.west-wind.com/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version But, unless this app is only going to be used in a couple of apps, this likely isn't an option.

    All that said, using a JavaScript map in your app isn't a great idea. Especially when there is a great Bing Maps WPF control that you can use in your app (can be used in WinForm apps too). You can find documentation on this control here: https://msdn.microsoft.com/en-us/library/hh750210.aspx

    You can also access this control through Nuget: http://www.nuget.org/packages/Microsoft.Maps.MapControl.WPF/

    Here are some additional resources around the Bing Maps WPF control:

    Blog posts: https://blogs.bing.com/maps/?s=WPF

    Code Samples: https://code.msdn.microsoft.com/site/search?query=Bing%20Maps%20WPF&f%5B0%5D.Value=Bing%20Maps%20WPF&f%5B0%5D.Type=SearchText&ac=2

    You will need a Bing Maps key to use the map control. You can get one through the Azure Marketplace here: https://azure.microsoft.com/en-us/marketplace/partners/bingmaps/mapapis/