Drawing Maps with Visual Basic (Scott Wisniewski)

Drawing Maps with Visual Basic (Scott Wisniewski)

  • Comments 13

If you find yourself looking for something to do with your awesome new copy of Visual Studio 2008, you should check out the December 2007 edition of MSDN magazine. It features an article, written by me, that provides a walk through on how to visualize data on a map using VB 9, WPF and LINQ.

To give you a sneak preview of what the app described in the article does, I've included a screen shot below:

 

 

 

Over my next several posts I plan on expanding on the article by showing how you can extend the application by adding new functionality to it. For now, I will give you some time to read through the article, download the code sample, and play around with the app. In my next post I will show how you can make the application multithreaded while still taking full advantage of WPF data binding.

Enjoy!

Leave a Comment
  • Please add 7 and 3 and type the answer here:
  • Post
  • The Visual Basic Team : Drawing Maps with Visual Basic (Scott Wisniewski)

  • PingBack from http://karlisle.consulting23.info/2007/11/26/drawing-maps-with-visual-basic-scott-wisniewski/

  • it seems that there is a Culture Sensitivity problem when running the sample as i get the exception below when i run the sample on a french PC :

    System.InvalidCastException was unhandled

     Message="La conversion de la chaîne "-5396.3399246595609" en type 'Double' n'est pas valide."

     Source="Microsoft.VisualBasic"

     StackTrace:

          à Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat)    à Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value)    à WpfApplication1.Extensions._Closure$__1._Lambda$__2($CLS0 z) dans C:\Users\cbellet\Documents\Visual Studio 2008\Projects\MapsWithVB9WPF\WpfApplication1\Extensions.vb:ligne 33    à System.Linq.Enumerable.<SelectIterator>d__d`2.MoveNext()    à System.Linq.Enumerable.Min(IEnumerable`1 source)    à System.Linq.Enumerable.Min[TSource](IEnumerable`1 source, Func`2 selector)    à WpfApplication1.Extensions.MinDBL[T](IEnumerable`1 x, Func`2 y) dans C:\Users\cbellet\Documents\Visual Studio 2008\Projects\MapsWithVB9WPF\WpfApplication1\Extensions.vb:ligne 33    à WpfApplication1.Window2._Lambda$__19(VB$AnonymousType_7`2 $VB$Key, IEnumerable`1 $VB$Group) dans C:\Users\cbellet\Documents\Visual Studio 2008\Projects\MapsWithVB9WPF\WpfApplication1\Window2.xaml.vb:ligne 118    à System.Linq.Lookup`2.<ApplyResultSelector>d__3`1.MoveNext()    à System.Linq.Enumerable.<SelectIterator>d__d`2.MoveNext()    à System.Linq.Lookup`2.Create[TSource](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)    à System.Linq.GroupedEnumerable`4.GetEnumerator()    à System.Linq.Enumerable.<SelectIterator>d__d`2.MoveNext()    à WpfApplication1.Extensions.AddRange[T](ICollection`1 col, IEnumerable`1 sequence) dans C:\Users\cbellet\Documents\Visual Studio 2008\Projects\MapsWithVB9WPF\WpfApplication1\Extensions.vb:ligne 28    à WpfApplication1.Window2.LoadFile(String file, ICollection`1 list) dans C:\Users\cbellet\Documents\Visual Studio 2008\Projects\MapsWithVB9WPF\WpfApplication1\Window2.xaml.vb:ligne 139    à WpfApplication1.Window2.LoadData() dans C:\Users\cbellet\Documents\Visual Studio 2008\Projects\MapsWithVB9WPF\WpfApplication1\Window2.xaml.vb:ligne 38    à WpfApplication1.Window2.WindowLoaded() dans C:\Users\cbellet\Documents\Visual Studio 2008\Projects\MapsWithVB9WPF\WpfApplication1\Window2.xaml.vb:ligne 107    à WpfApplication1.Window2._Lambda$__5(Object a0, RoutedEventArgs a1) dans C:\Users\cbellet\Documents\Visual Studio 2008\Projects\MapsWithVB9WPF\WpfApplication1\Window2.xaml.vb:ligne 0    à System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)    à System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)    à System.Windows.UIElement.RaiseEventImpl(RoutedEventArgs args)    à System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)    à MS.Internal.FrameworkObject.OnLoaded(RoutedEventArgs args)    à System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)    à System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)    à MS.Internal.LoadedOrUnloadedOperation.DoWork()    à System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()    à System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()    à System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)    à System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)    à System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)    à System.Windows.Interop.HwndTarget.OnResize()    à System.Windows.Interop.HwndTarget.HandleMessage(Int32 msg, IntPtr wparam, IntPtr lparam)    à System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)    à MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)    à MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)    à System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)    à System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)    à System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)    à System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)    à MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)    à MS.Win32.UnsafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)    à System.Windows.Window.ShowHelper(Object booleanBox)    à System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)    à System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)    à System.Windows.Threading.DispatcherOperation.InvokeImpl()    à System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)    à System.Threading.ExecutionContext.runTryCode(Object userData)    à System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)    à System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)    à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)    à System.Windows.Threading.DispatcherOperation.Invoke()    à System.Windows.Threading.Dispatcher.ProcessQueue()    à System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)    à MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)    à MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)    à System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)    à System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)    à System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)    à System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)    à MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)    à MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)    à System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)    à System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)    à System.Windows.Threading.Dispatcher.Run()    à System.Windows.Application.RunInternal(Window window)    à System.Windows.Application.Run(Window window)    à System.Windows.Application.Run()    à WpfApplication1.Application.Main() dans C:\Users\cbellet\Documents\Visual Studio 2008\Projects\MapsWithVB9WPF\WpfApplication1\obj\Debug\Application.g.vb:ligne 61    à System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)    à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)    à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()    à System.Threading.ThreadHelper.ThreadStart_Context(Object state)    à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)    à System.Threading.ThreadHelper.ThreadStart()

     InnerException: System.FormatException

          Message="Le format de la chaîne d'entrée est incorrect."

          Source="Microsoft.VisualBasic"

          StackTrace:

               à Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)    à Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat)

          InnerException:

  • i just had the folowing code in the WindowLoaded Sub and it was OK.

    Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US", False)

  • Bellet,

    Thanks for pointing out the bug, and letting me know about the fix!!!!

  • All I got is just a bunch of errors, is this example working?

    thanks in advanced

  • Hello out there, what a wondreful example!

    I'd searched for such a solution for some while and when I found this example I had the same problems described by the other companions.

    The solution for running the example on the german version is similar. First go to the very first Folder "Projektmappe" and toggle the entry Startprojekt to "WpfApplication1".

    then copy the sampledata to the folder bin (with teh explorer).

    At last you' ll have to go back at VS and add in windows2.xaml.vb at the begining just before the first Class declaration:

    "Imports System.Globalization"

    and also you'll have to change:

       Private Sub WindowLoaded() Handles Me.Loaded

           Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US", False)

           LoadData()

       End Sub    

    I suppose this is nessesary because now you get the right order type for reading the points of all polygons.

    greetings from Germany

  • Hello Scott,

    I had a look at this article about Creating Dynamic Maps with VB 9.0 and WPF. It’s very helpful for me to read your article and to obtain the source code.

    I have two questions:

    1) Were there any updates/effort fixes or extensions for the article or the source code since it was posted?

    2) Would you know about an article or source code that specifically addresses the creation of dynamic RASTER (PIXEL) maps?

    Thanks very much for your help.

    Regards,

    FSPH

  • Hello again,

    I tried to run this program under VB 2008 Express Edition. However, some .dll files required by Scott's program are not installed with the Express Edition.

    Is there a smart alternative to installing each missing .dll file individually based on each error message?

    Thank you for your help.

  • Here is what you need to do to make it work

    1) Set the WPF application as the startup project (To do it, you should right click on the project in the solution explorer and choose "Set as Startup project)

    2) In the file "Window2.xaml.vb" in the function "LoadData()" Update the two calls to "LoadFile" with the full path to files (these files are in sub folders of the WpfApplication1 folder)

    3) In the same Function, update the call the XDocument.Load() with the full path to the applicable file.

  • Hello Avner,

    thank you, the program now works.

    The new technology is awesome, however, it is largely over the top of my head.

    Has anyone experimented with a simpler task, for example, displaying a 100 * 100 cell grid?  

    Thanks,

  • I think its strange that one has to rewrite so much code to make it work....and it still does not. I have tried all of your above written ideas. Now I sit at the "-5396.3399246595609" problem and don't understand what to do.

    How come they write samples which do not function at once or have explanations ready for eventual "local" problems?

    Would be happy to receive some help :)

    BR

    Leif

  • this is a great code even though I have no clue how it works,  is there a way to change the code that when you click on the county within a state to put a put a border on the county ( the same way when you go into a  state , it changes the border) and also to show the name of the county.

Page 1 of 1 (13 items)