ZIP Code and City Finder Sample

 

Microsoft Corporation

July 2004

Applies to:
   Microsoft MapPoint 2002
   Microsoft MapPoint 2004

Summary: Learn how to create a simple Microsoft Visual Basic 6.0 application that takes a ZIP Code as input and returns a city. (3 printed pages)

Contents

Introduction
Creating the Application

Introduction

This article describes how to build a simple Microsoft Visual Basic 6.0 application that takes a United States ZIP Code as input, returns the city that the ZIP Code is in, and centers the Microsoft MapPoint map on that ZIP Code. This sample was written for MapPoint North America and will not work with MapPoint Europe without modifications.

When you run the application, be aware of the following limitations:

  • Because ZIP Code boundaries do not always follow city boundaries, you might get mixed results in some cases.
  • Some ZIP Codes are not located in areas that MapPoint defines as a city; for these ZIP Codes, you can instead return the county that the ZIP Code is located in by using the geoShowByRegion2 constant (county) instead of geoShowByCity (city).
  • MapPoint might not contain ZIP Codes that have been recently added by the U.S. Postal service.

Creating the Application

To create the application

  1. In Visual Basic, open a new Standard EXE project.
  2. On the Project menu, click References, and then select Microsoft MapPoint 9.0 Object Library (North America).
  3. Add a TextBox control to Form1, and in the Properties window, set the Text property to nothing (empty).
  4. Add a CommandButton control to Form1, and in the Properties Window, change the Caption property of the Command1 control to Find city.
  5. Double-click the CommandButton control and add the following code to the Command1_Click() subroutine:
    Private Sub Command1_Click()
            Dim oCityLoc As MapPoint.Location
            Dim oMap As MapPoint.Map
            Set oMap = CreateObject("MapPoint.Application").ActiveMap
            oMap.Application.Visible=True
            oMap.Application.UserControl=True
        
        
            Dim zipCode As String
            zipCode = Text1.Text
        
            Set oCityLoc = FindCityForZipCode(zipCode, oMap)
            If Not (Nothing Is oCityLoc) Then
                MsgBox "ZIP Code " & zipCode & " is in " & oCityLoc.Name & "."
            Else
                MsgBox "Couldn't find city for this ZIP Code."
            End If
    
    End Sub
    
    ' Returns the city at the center of the ZIP Code
    Function FindCityForZipCode(strZip As String, oMap As MapPoint.Map) As MapPoint.Location
        Dim oZipLoc As MapPoint.Location
    
        Dim oZipResults As MapPoint.FindResults
        
        Set oZipResults = oMap.FindAddressResults(, , , , strZip, "USA")
        
        ' If it's not a good match, then don't accept it
        If geoFirstResultGood = oZipResults.ResultsQuality Then
            Set oZipLoc = oZipResults(1)
        Else
            Exit Function
        End If
        
        '  Must be a match to a Post Code (US ZIP Code)
        If Not (geoShowByPostal1 = oZipLoc.Type) Then
            Exit Function
        End If
    
        ' Go to the ZIP Code location on the map to hit test    
        oZipLoc.GoTo    
        Dim x As Integer
        Dim y As Integer
        x = oMap.LocationToX(oZipLoc)
        y = oMap.LocationToY(oZipLoc)
        
        ' Find all geographic entities at that point
        Dim oContext As MapPoint.FindResults
        Set oContext = oMap.ObjectsFromPoint(x, y)
        
        ' Return the city at that point (if any)    
        For Each obj In oContext
            If geoShowByCity = obj.Type Then
                Set FindCityForZipCode = obj
                Exit Function
            End If
        Next obj
    
    End Function
    
    

Run the Visual Basic project. Type any U.S. ZIP Code in the text box. A pop-up message appears indicating the city to which the ZIP Code belongs. The MapPoint map also centers itself on that ZIP Code.

Show: