Online Service 在线服务
New Articles 最新更新
Hot Articles 热点文章
Google 广告
首页 … 技术文档 Technical Articles
添加Google Map到VB.NET桌面应用程序
(时间:2007-8-3 15:15:20 共有 人次浏览)

Introduction

This project demonstrates a quick and easy way to add mapping to a Windows desktop application (with an available internet connection) using Google Maps as the basis and source for the map. The demonstration application will map a point by either its physical address or its latitude and longitude.  Building the application does not require any sort of agreement or licensing with Google; there is no required API to invoke and using this approach it really won't take more than a few minutes to put a map into your Visual Basic 2005 built desktop application.



Figure 1:  Mapping an Address (
New York).
 

Since the application is based entirely on Google Maps, as an added bonus, the Google Maps site provides all of the view manipulation controls necessary to navigate the map; so with no additional effort you pick up panning and zooming, traffic maps, street maps, raster imagery, and hybrid maps combining street maps with raster.  You even get an overview map, the ability to print the map, email it, or link to it at no extra charge. 

One could also remove the interface and feed specific addresses or coordinates directly to equivalent code and access specific maps directly through a web browser control.



Figure 2:  Mapping a Latitude/Longitude Coordinate Pair (Paris).
 

Getting Started

The solution contains a single project for a C# Windows desktop application. The project is called "VB_QuickMap" and this project contains one form. The form contains all of the controls and code necessary to request and display a map from the Google Maps site.



Figure 3:  Solution Explorer with Project Visible.
 

Form 1 contains a split panel control oriented horizontally; the top pane contains the controls used to enter the search terms.  There are two sets of controls in this panel; the first set will collect an address in the form of the street, city, state, and zip code (5 digits). The second set of controls will collect a latitude and longitude. Both sets of controls have buttons; the click event handlers for these buttons are used to construct a query string used to retrieve the map based on either the address or coordinates supplied by the user. 

The bottom pane of the control contains a panel with a web browser control docked into it. The query string is appended to the basic URL used to query Google Maps; the combined string is set as the navigation URL property for the web browser control. 

Code:  Form 1

The form 1 class starts out with the default class declaration. An import statement was added to pull in the System.Text library; this is required to support the use of string builders within the application.

Imports System.Text

 

Public Class Form1

Following the class declaration, the next bit of code in the class is used to handle the button click event that requests a map of a physical address from Google Maps; that code is as follows: 

''' <summary>

''' Map a physical address

''' </summary>

''' <param name="sender"></param>

''' <param name="e"></param>

''' <remarks></remarks>

Private Sub btnMapIt_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles btnMapIt.Click

 

    Try

        Dim street As String = String.Empty

        Dim city As String = String.Empty

        Dim state As String = String.Empty

        Dim zip As String = String.Empty

        Dim queryAddress As New StringBuilder()

        queryAddress.Append("http://maps.google.com/maps?q=")

 

        ' build street part of query string

        If txtStreet.Text <> String.Empty Then

            street = txtStreet.Text.Replace(" ", "+")

            queryAddress.Append(street + "," & "+")

        End If

 

        ' build city part of query string

        If txtCity.Text <> String.Empty Then

            city = txtCity.Text.Replace(" ", "+")

            queryAddress.Append(city + "," & "+")

        End If

 

        ' build state part of query string

        If txtState.Text <> String.Empty Then

            state = txtState.Text.Replace(" ", "+")

            queryAddress.Append(state + "," & "+")

        End If

 

        ' build zip code part of query string

        If txtZipCode.Text <> String.Empty Then

            zip = txtZipCode.Text.ToString()

            queryAddress.Append(zip)

        End If

 

        ' pass the url with the query string to web browser control

        webBrowser1.Navigate(queryAddress.ToString())

 

    Catch ex As Exception

 

        MessageBox.Show(ex.Message.ToString(), "Unable to Retrieve Map")

 

    End Try

 

End Sub 

A quick examination of this code reveals that the contents of the text boxes used to define the physical address are captured from the form and used to populate variables local to the event handler. A string builder is used to build the URL and query string. 

Once the string builder is initialized, the first part of the URL is appended to it. As each additional section of the address is conditioned for use in the query string, it too is appended to the query string. Once the string is defined, it is passed to the web browser control as its navigate URL. At that point, the web browser will display the address if it can be found or will summon Goggle Maps with optional selections used to narrow down to a single address.

The next bit of code in the application performs a similar function with Latitudes and Longitudes; the click event handler for the button is used to evoke the map based upon the user supplied lat/long values. That code is as follows: 

''' <summary>

''' Map a location by latitude and longitude

''' </summary>

''' <param name="sender"></param>

''' <param name="e"></param>

''' <remarks></remarks>

Private Sub btnMapLatLong_Click(ByVal sender As System.Object, ByVal e As 

System.EventArgs) Handles btnMapLatLong.Click

 

    If txtLat.Text = String.Empty Or txtLong.Text = String.Empty Then

        MessageBox.Show("Supply a latitude and longitude value.""Missing Data")

    End If

 

    Try

        Dim lat As String = String.Empty

        Dim lon As String = String.Empty

        Dim queryAddress As New StringBuilder()

        queryAddress.Append("http://maps.google.com/maps?q=")

 

        ' build latitude part of query string

        If txtLat.Text <> String.Empty Then

            lat = txtLat.Text

            queryAddress.Append(lat + "%2C")

        End If

 

        ' build longitude part of query string

        If txtLong.Text <> String.Empty Then

            lon = txtLong.Text

            queryAddress.Append(lon)

        End If

 

        webBrowser1.Navigate(queryAddress.ToString())

 

    Catch ex As Exception

 

        MessageBox.Show(ex.Message.ToString(), "Error")

 

    End Try

 

End Sub 

That wraps up the class. The two button click event handlers do all of the work necessary to generate the query string and to pass that string to the navigate URL property for the web browser control. The approach shown works with latitude and longitude pairs as well as addresses; when dealing with addresses, the function will work even with incomplete addresses (e.g., no zip code, or no zip code and street address). 

打印 【打印该页】 【关闭窗口

此技术资料来自网络,仅供参考。未经许可,不得转载。
若有侵权,请及时与我们取得联系!
FAQS 常见问题
Consultation 即时咨询
Contact Us 联系我们