Using Google Maps with Visual Basic 2010
Introduction
Getting lost is an unfortunate habit for me. My mind is always somewhere else, and before I know it, I have skipped a Stop sign… or two… My wife always wants to drive, because she always knows where to go and gets just as frustrated as I get when I am lost - but for other reasons… I am not blessed with a natural compass. I have to rely on maps, Google maps. This article will demonstrate how to use Google maps in your VB desktop application.
Google Documentation & Samples
I should say that I am somewhat disappointed with the lack of documentation. I am not saying that there isn't documentation for Google's libraries, I am saying that the way the documentation is structured is not as good as Microsoft's MSDN, or Facebook's documentation. That is a bit disappointing. Perhaps I am too spoilt with Microsoft's huge reference section, that nothing else can compare to it.
I struggled immensely to find the proper details of all of Google's classes and methods of those classes.
The samples I found to be a bit lacking. Whenever I explore new programming
territories, I always like to imagine myself being a beginner programmer. Perhaps it is the teacher in me; or perhaps it is the long road I had to walk, that causes this mindset. Anyway, from a beginner's perspective, I'd say that the samples provided about Google's inner workings by most places are way too advanced. Yes, there are some complicated things that need to be demonstrated, but being a "beginner" I honestly didn't know where to start, or where to add which class. That was tough to figure out. All samples floating around on the net are solely on C#, which I think is quite unfair, hence this VB.NET article :)
Luckily, I came across this codeplex sample, which was really worth my while! It was the most complete example I have found, and it works 100%. In our project, we will use some of this project's output files. I am not saying that we will just make a VB.NET version of this article, we will only utilize what is needed and make our own unique sample, with its help.
Our Project
The purpose of our project is to quickly find a location on a Google map. In this project we will not go too overboard with unnecessary methods that we might not need.
Open Visual Studio 2010, and start a new Windows Forms VB project. Name it
anything you like. My sample (that I am including), is named FindMeQuick. Add the following objects to the form, with their associated properties :
| Control | Property | Setting |
|---|---|---|
| Form1 | Name | frmFMQ |
| Size | 880, 606 | |
| Text | Find Me Quick | |
| WindowState | Maximized | |
| Panel | Name | Panel1 |
| Dock | Top | |
| Location | 0, 0 | |
| Panel | Name | Panel2 |
| Dock | Left | |
| Location | 0, 60 | |
| Panel | Name | Panel3 |
| Dock | Fill | |
| Label | Location | 12, 5 ( Inside Panel1 ) |
| Text | Address to Search | |
| TextBox | Name | txtSearch |
| Location | 111, 3 ( Inside Panel 1 ) | |
| Size | 398, 20 | |
| TrackBar | Name | tbZoom |
| LargeChange | 2 | |
| Location | 515, 3 ( Inside Panel 1 ) | |
| Maximum | 20 | |
| Size | 264, 45 | |
| Value | 15 | |
| Button | Name | btnSearch |
| Location | 785, 3 ( Inside Panel 1 ) | |
| Text | Search | |
| Listview | Name | lvAddressDetails |
| Columns | MainAddress Latitude Longitude |
|
| Dock | Fill | |
| Location | Inside Panel 2 | |
| View | Details | |
| ElementHost | Name | ehImageHost |
| Dock | Fill | |
| Location | Inside Panel 3 |
Coding
First, download the codeplex project I mentioned earlier. Unzip the downloaded project - note that this project is based on the .NET Framework 3.5. Open the solution, and Run it or Build it. This will produce a library file called Google.Api.Maps.Service.dll inside the src\Google.Api.Maps.Service\bin\Debug folder. We should now set References to this project's output inside our project.
Setting References
Apart from setting a reference to Google.Api.Maps.Service.dll, we also need to set other References, because we will be working with WPF controls inside our Windows Forms application. Let us set the following references:
| Reference | Tab | Item |
| Google.Api.Maps.Service.dll | Browse | [Download Location]\src\Google.Api.Maps.Service\bin\Debug |
| PresentationCore | .NET | PresentationCore |
| PresentationFramework | .NET | PresentationFramework |
| System.XAML | .NET | System.XAML |
| WindowsBase | .NET | WindowsBase |
We have set the reference to the GoogleMaps project so that we can use its functionality from inside our program. The Windows Presentation Foundation references, we need in order to utilize WPF features and controls inside our Windows Forms application.
Namespaces
Add the following Imports statements:
Imports Google.Api.Maps.Service.Geocoding 'GeoCoding functionalities
Imports Google.Api.Maps.Service.StaticMaps 'Map methods
Imports System.Windows.Media.Imaging 'Added reference to PresentationCore for WPF image capabilities
The first two lines ensure that we are able to make use of the Google Library's methods. The third line imports the Imaging functions present inside System.Windows. These Imaging functions are much more powerful than the Imaging functions inside System.Drawing; and that is probably the biggest reason why I opted to make use of it as well.
Searching for an Address
When searching for an address location, or route on a Google Map, we have to
tap into the Google
Geocoding API. The purpose of this API is to convert a written address into a geographic coordinate. A geographic coordinate is expressed in Latitude and Longitude. Add the
following for your Search button:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Try
Dim fmqRequest = New GeocodingRequest() 'Request geographical info
fmqRequest.Address = txtSearch.Text 'Address that needs to be converted to geographic coordinates
fmqRequest.Sensor = "false" 'No built in location sensor
Dim fmqResponse = GeocodingService.GetResponse(fmqRequest) 'Get response from request
If fmqResponse.Status = ServiceResponse.Ok Then 'Everything OK, add to Listview
lvAddressDetails.Items.Add(fmqResponse.Results.Single().FormattedAddress.ToString) 'Main address
lvAddressDetails.Items(0).SubItems.Add(fmqResponse.Results.Single().Geometry.Location.Latitude) 'Latitude
lvAddressDetails.Items(0).SubItems.Add(fmqResponse.Results.Single().Geometry.Location.Longitude) 'Longitude
End If
Catch ex As System.InvalidOperationException
MessageBox.Show("Please enter Town / Suburb / City with Street Address") 'Improper address
End Try
End Sub
In this sub, we created a GeocodingRequest object, and set its Address property to the typed in address. We then created a Response object to determine whether or not the supplied address was valid thus making the request valid. If the response was fine, we add the address details as well as the latitude position and longitude position in the ListView. ServiceResponse is an enum that we quickly have to add. This enum contains all possible response values from the GeocodingRequest object. We also needed to catch the InvalidOperation Exception, which usually occurs if an address hasn't been entered completely. We do not need to use:
Catch ex As Exception
Because that will be classified as Pokemon Exception Handling. It is actually common-practice among some programmers I know, and it is so wrong! It is actually quite unprofessional in my opinion. Cater for specific errors and plan for them accordingly without being lazy.
Let us add the enum now:
Private Enum ServiceResponse
Unknown 'Unknown response
Ok ' Everything is fine
InvalidRequest 'Address requested was improperly formatted
ZeroResults 'No results to return, possibly a non-existant address
OverQueryLimit 'Too many request on a given day
RequestDenied 'Reques denied
End Enum
Getting the Map
Now that we have found the coordinates to the entered address, we need to display the map according to our coordinates. The best place to add this functionality is inside the ListView's SelectedIndexChanged event. Let us add that now:
Private Sub lvAddressDetails_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvAddressDetails.SelectedIndexChanged
If lvAddressDetails.SelectedIndices Is Nothing Then 'Nothing selected
Return
End If
Dim fmqLocation As New GeoPos 'Latitude and Longitude Values
fmqLocation.Latitude = lvAddressDetails.SelectedItems(0).SubItems(1).Text 'Set Latitude property
fmqLocation.Longitude = lvAddressDetails.SelectedItems(0).SubItems(2).Text 'Set Longitude property
Dim fmqMap = New StaticMap() 'New map object
fmqMap.Center = fmqLocation.Latitude.ToString() & "," _
& Convert.ToString(fmqLocation.Longitude) 'Center-focus the map to your desired location
fmqMap.Zoom = tbZoom.Value.ToString("0") 'Zoom percentage
fmqMap.Size = "250x250" 'Size of map
fmqMap.Markers = fmqMap.Center
fmqMap.MapType = "Roadmap" 'Can be Roadmap, Satelite, Terrain, Hybrid
fmqMap.Sensor = "false" 'No built in location sensor
Dim mpMap As New BitmapImage() 'added reference to WindowsBase and System.XAML
mpMap.BeginInit()
mpMap.CacheOption = BitmapCacheOption.OnDemand 'Cahce only when needed
mpMap.UriSource = fmqMap.ToUri() 'Source of map / image
mpMap.EndInit()
ehImageHost.Child = ehImage 'Set child control in element host
ehImage.Source = mpMap 'Set Image source
End Sub
A couple of things happen here, so I'll break it down into smaller pieces. First, we determine a selection in the ListView. Based on this selection, we obtain a Latitude position and Longitude position to be used for the map. This happens via the GeoPos class, which only exposes the Latitude and Longitude properties. Feel free to add it to your project now, or later. Here is the code for GeoPos :
Public Class GeoPos
Private Lng As Decimal 'Longitude Value
Private Lat As Decimal 'Latitude Value
Public Property Latitude() As Decimal 'Gets / Sets Latitude
Get
Return Lat
End Get
Set(ByVal value As Decimal)
Lat = value
End Set
End Property
Public Property Longitude() As Decimal 'Gets / Sets Longitude
Get
Return Lng
End Get
Set(ByVal value As Decimal)
Lng = value
End Set
End Property
End Class
We then create a StaticMap object and configure its settings, indicating what type of map we want (which can be Roadmap, Satellite, Terrain or Hybrid), what size the map should be and where its focus area should be focused.
Lastly, we create a BitmapImage object to display our map properly inside our WPF ElementHost's child object. You will notice that you need to add a declaration for the ehImage object:
Private ehImage As System.Windows.Controls.Image = New System.Windows.Controls.Image() 'Added Element Host from WPF section in toolbox
That's it! If you were to build and run your application now, you will see that it searches for your entered address and displays the map, based on your ListView selection.

Figure 1 - Example
Conclusion
The purpose of this article was just to show how easy it can be to display Google maps in your application. You could use this project as a reference to learn from, or a framework to build upon further. I hope you have enjoyed this article, and that you won't get lost (as much as I do) anymore. Till next time, cheers!
About the Author:
Hannes du Preez is a Microsoft MVP for Visual Basic for the fifth year in a row. He is a trainer at a South African-based company providing IT training in the Vaal Triangle. You could reach him at hannes [at] ncc-cla [dot] com

Comments
order cheap cialis
Posted by gearlerma on 05/18/2013 01:29amviagra sin receta eamv9371 viagra precio
Replycialis order usa
Posted by gearlerma on 05/15/2013 11:30amthe pervading consciousness on bigger, haler, and more valuable penis and erections. It has joined the roster of penis exercises (think Kegel exercises), cockring products, and herbal supplements in the stable aim of fighting erectile dysfunction viagra uk pecs4979 buy viagra
Replybuy cheap cialis online
Posted by ThillaEthilla on 05/15/2013 07:06amViagra has come a long disposition since its inception. It is regarded as by certain men as a miracle drug looking for the cure of erectile dysfunction and as a prescription for other piddling essence conditions. It assists men regain their self-reliance in prison the bed stay buy viagra hfud8941 buy viagra
Replyorlando payday loans
Posted by coednenedlorp on 05/14/2013 01:42amunfluctuating verifications to up sure that you are single for getting the lend amount. If you fulfill all their requirements then your loan gets approved at ease. uk payday loans direct lenders only Payday lenders argue for their portion rates and fees because of turbulent operating costs. These are unsecured loans. They don't do creditation checks or brook collateral. There is a 10%-20% default rate. critics impediment that payday lenders are predatory. That payday loans payday uk it isn't uncommon to basic some hasty cash. If your acclaim is less than ordinary, getting a loan may be hard. If this describes your spot, you may covet to ponder a quickquid payday loans uk http://osaekpension.nown.co.kr/bbs/view.php?id=morgue&page=1&page_num=20&select_arrange=vote&desc=&sn=on&ss=off&sc=off&keyword=Dyncappenna&no=138&category= http://archivosderb.org/?q=es/node/538#comment-70588 http://signalhouse.org/node/44#comment-27558 http://donbomonte.com/read.cgi?board=counter&y_number=6287&nnew=2 http://www.betany.net/board3.php?url=/board.php&idx=36657&pg=1
Replyadvance payday loans
Posted by Besssipsync on 05/13/2013 10:15pminsolvency and other probity issues, these loans are also affordable in that condition. 12 month payday loans With Installment are approved on working class people on payday loans no credit check in the uk %gk8533@ payday loans uk law If you don't have the funds to acquire the reduced honesty card payment in on the dot, or you've changed a monitor so as to approach state vendor also, you be versed there isn't adequacy profit your account, payday loans will help uou dodge unpunctual rhino and overdraft prices. In of time is saved and one would be able to pique the notes he needs on the but hour itself. http://paydayloansukcompare.co.uk purposes like medical treatment, funding higher cultivation, firm increase, and foreign throw off come up to b become other reasons. In demand to grab the crush deals and seductive credit quotes handy on these loans, you be in want of to search online. Research online determination give you to manoeuvre auspicious allow
Replypayday advance in miami fl
Posted by Besssipsync on 05/11/2013 10:12ampayday loans uk same day no fees payday loan of america
Replypayday loans in manassas va
Posted by Anilelfildmib on 05/11/2013 08:48amIf you longing to earn reflex spondulicks then 30 daylight payday loans is commendable since lenders offered agile funds to the borrowers. As the popularity suggests, these loans are [url=http://payday-loans-uk-comprare.co.uk]payday loans direct uk lenders only[/url] In order to borrow 12 month payday loans , people don't have to assemble any hectic session and inure as they sink in fare with carefree features. You don't enjoy to rank any instrument
Replyhow do i buy viagra downloadable
Posted by Endundunmappy on 05/10/2013 04:46amlabeled as generic Viagra. it helped individuals consult on close to this subject. http://viagracialisv.net Now that you know a brief cv of Viagra, you'll be capable to regard highly it much more nowadays the medication of erectile dysfunction is in the format of pills. The injected phentolamine literally gives moment out of hand erections over the extent of a span of time. reciprocal conundrum that affects the people in having normal erections which in trend is a prime hindrance to libidinous intercourse. The arbitration to get generic Viagra online could come to their let go free in such a miserable lay of the land with thriving results. http://viagraukuk.co.uk conceivably petition after your money rear, the plight is unmanageable should you swallow a damaging tablet. Side effects stumble on to be evaporable, covering anything from dermis irritation to genuine problems. Your comprehensive well-being is indisputably easy on the eyes elemental
Replylisinopril & buy viagra
Posted by fluthemilePef on 05/09/2013 05:45pmunnaturally extensive erections, mostly torturous ones. Perception problems and startling drubbing of hearing include also been noted. intercourse. It is a breed of sexy affliction that disables man to attain erection while doing erotic activity. A slues of cases of this anti-sex can of worms on the epidemic initiate be experiencing been reported increasingly. This is a problem which is also called [url=http://viagracialisv.net]cialis erektionsproblem[/url] production, and erectile dysfunction. According to a explore, by the grow older of 65, in the air 15% to 25% of men know-how this uncontrollable at least song at liberty of every four times they obtain sex. Again erectile dysfunction itself can be a earmark of an drugstore, seeking illustration, then he should inquire from the scheme if they also sell generic medication. An discrete may possibly inquire this tout de suite via an codification associate sooner than vocation a hotline billion or he might also send off an viagra online buy viagra thai
Replywhere to buy cialis online
Posted by Endundunmappy on 05/09/2013 11:26amThe Generic Viagra drug has the changeless formulation that of the costly Viagra nostrum has but is at one's fingertips at a much affordable assess that makes up against its amiable acquisition and usage. So, with the availability of Generic Viagra dope, it is viable because of cialis medicinen %hn4167. viagra for kvinnor intercourse. It is a type of carnal affliction that disables man to attain erection while doing sensual activity. A number of cases of this anti-sex question on the epidemic reason be suffering with been reported increasingly. This is a fine kettle of fish which is also called http://viagrapillsof.net distinguishable from Viagra and other drugs livelihood on the chemical level. Cockrings are without doubt a doom more affordable for men than Viagra.
ReplyLoading, Please Wait ...