VB.NET•Display Microsoft MapPoint Topo Maps

Listing 4. Invoke MapPoint's Web Services RenderServiceSoap.GetMap method to return a bitmap image defined by the latitude and longitude of the map's centerpoint and a Scale integer calculated from monitor resolution (usually 96 pixels per inch), image size (640-by-480 pixels), and the value the user selects from the Scale menu. The GetMapPointImage function is a MapPoint clone of the GetTileImage function for USGS images.

Private Function GetMapPointImage( _
   ByVal dblLatUD As Double, _
   ByVal dblLonUD As Double, _
   ByVal intWidth As Integer, _
   ByVal intHeight As Integer) As Bitmap
   'Retrieve the MapPoint image
   'Get the lat/lon, if necessary, and display the map
   'This code is based on examples of ASP.NET 
   'code included with the MapPoint Web Services 
   '3.5 SDK
   If strLastTitle <> Me.Text And dblLatNav = _
      0 And Not (blnIsFromLandmarks Or _
      blnIsFromAddress) Then
      'If a different view and not from landmarks 
      'or navigation, get the centerpoints from 
      'TerraService
      Dim TS As New TS.TerraService
      TS.Timeout = 30000

      ' Get the latitude and longitude of the 
      ' requested place
      Dim plCity As New TS.Place
      With plCity
         .City = strCity
         .State = strState
         .Country = "USA"
      End With
      'Return the centerpoint by invoking the 
      'ConvertPlaceToLonLatPt Web method
      Dim ptCity As TS.LonLatPt = _
         TS.ConvertPlaceToLonLatPt(plCity)
      dblLatPoint = ptCity.Lat
      dblLonPoint = ptCity.Lon
   End If
   strLastTitle = Me.Text

   'MapPoint definitions
   Dim bmpMapPoint As New Bitmap(intWidth, _
      intHeight, pixFormat)
   Dim grfImage As Graphics = _
      Graphics.FromImage(bmpMapPoint)
   Dim mwsRender As MP.RenderServiceSoap = _
      New MP.RenderServiceSoap
   Dim crdMapPoint As New _
      NetworkCredential(strMPUserID, strMPPassword)
   mwsRender.Credentials = crdMapPoint
   mwsRender.PreAuthenticate = True

   'Define the view
   Dim amvViews(0) As MP.ViewByScale
   amvViews(0) = New MP.ViewByScale
   With amvViews(0)
      .CenterPoint = New MP.LatLong
      If dblLatUD = 0 Then
         .CenterPoint.Latitude = dblLatPoint
         .CenterPoint.Longitude = dblLonPoint
      Else
         .CenterPoint.Latitude = dblLatUD
         .CenterPoint.Longitude = dblLonUD
      End If
      .MapScale = 60000 * Val(strScale) / 16
   End With

   'Set map options
   Dim moImage As New MP.MapOptions
   With moImage
      .ReturnType = MP.MapReturnType.ReturnImage
      .Format = New MP.ImageFormat
      .Format.Height = intHeight
      .Format.Width = intWidth
   End With

   'Add a centerpoint Pushpin
   Dim ppCenter(0) As MP.Pushpin
   ppCenter(0) = New MP.Pushpin
   With ppCenter(0)
      .PinID = "pin0"
      'Labels are a bit intrusive
      '.Label = "CenterPoint"
      .IconName = "0"
      .IconDataSource = "MapPoint.Icons"
      .LatLong = amvViews(0).CenterPoint
   End With

   'Set up the specification object
   Dim mapSpec As New MP.MapSpecification
   With mapSpec
      .Options = moImage
      .Pushpins = ppCenter
      .Views = amvViews
      .DataSourceName = "MapPoint.NA"
   End With
   Dim imgMap As Image
   Try
      'Get the MapPoint image from the staging server
      Dim mpImage As MP.MapImage()
      mpImage = mwsRender.GetMap(mapSpec)
      imgMap = Image.FromStream(New _
         MemoryStream(mpImage(0).MimeData.Bits))

      'Draw the image
      Dim ptfStart As New PointF(0, 0)
      grfImage.DrawImage(imgMap, ptfStart)
      mnuFilePrintImage.Enabled = True
      Return bmpMapPoint

   Catch excImage As Exception
      Dim strMsg As String = "Error obtaining image " + _
         "from MapPoint Web Service. "
      MsgBox(strMsg + excImage.StackTrace, _ 
         MsgBoxStyle.Exclamation, _
         "Error Obtaining MapPoint Image")
   Finally
      If Not imgMap Is Nothing Then
         imgMap.Dispose()
      End If
   End Try
End Function