Welcome to MapForums!

Register, sign in, or use Facebook Connect above to join in and participate in the forum.

When you are signed in, this message, the ads in this row, and the red-underlined link ads all go away.

Subscribe to receive our newsletter.
Subscribe Unsubscribe
Results 1 to 5 of 5

VB.net Calcpos

This is a discussion on VB.net Calcpos within the MP2K Magazine Articles forums, part of the Map Forums category; Hello Experts I have attempted to use Gile Coles code to obtain the Latitude and longitude for a map position ...

  1. #1
    JoeBo is offline Member Yellow Belt
    Join Date
    May 2007
    Posts
    31

    VB.net Calcpos

    Hello Experts

    I have attempted to use Gile Coles code to obtain the Latitude and longitude for a map position resulting from a mouse event. I am using visual studio 2005, latitude and longitude are returned as 0 . Visual studio has altered the formula names in Accros Function from:
    Arccos = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)
    To:
    Arccos = Atan(-x / Sqrt(-x * x + 1)) + 2 * Atan(1)

    I believe these are only name changes in system.math,. The full codes used is reproduced below can you see the problem.

    Private Sub AxMappointControl1_BeforeDblClick(ByVal sender As System.Object, ByVal e As AxMapPoint._IMappointCtrlEvents_BeforeDblClickEven t) Handles AxMappointControl1.BeforeDblClick
    Try
    Dim objMap As MapPoint.Map
    objMap = Me.AxMappointControl1.ActiveMap
    Dim objDataSet As MapPoint.DataSet
    Dim txt1, txt2, txt0 As String
    Dim Ilat, Ilong As Double
    For Each objDataSet In objMap.DataSets 'clear pushpins
    If objDataSet.Name = "My Pushpins" Then
    objDataSet.Delete()
    End If
    Next
    Dim objResult As Object
    Dim objResults As MapPoint.FindResults
    'Display the name of each object where user double-clicks on map
    objResults = objMap.ObjectsFromPoint(e.x, e.y)
    Dim i As Int16 = 0
    For Each objResult In objResults
    If (i) = 0 Then txt0 = objResult.name
    If (i) = 1 Then txt1 = objResult.name
    If (i) = 2 Then txt2 = objResult.name
    i = i + 1
    Next

    Dim objLoc As MapPoint.Location

    objLoc = objMap.XYToLocation(e.x, e.y)
    objMap.AddPushpin(objLoc, "Current location")

    objMap.AddPushpin(objLoc, "Current location").Highlight = True
    CalcPos(objMap, objLoc, Ilat, Ilong)
    txt0 = txt0 & ", " & txt1 & " ," & txt2
    MsgBox("Current Location" & vbCrLf & txt0 & vbCrLf & Ilat & " Latitude" & vbCrLf & Ilong & " Longitude")
    Catch ex As Exception
    MsgBox(Err.Description)

    End Try
    End Sub
    I would value any help with this.

    Regards,
    Joe

  2. #2
    Winwaed's Avatar
    Winwaed is offline Mapping-Tools.com Black Belt
    Join Date
    Feb 2004
    Location
    Irving,Texas
    Posts
    1,859
    Blog Entries
    60

    Re: VB.net Calcpos

    It might help if we knew what your symptom was?
    (and if you think you have a CalcPos problem, seeing your CalcPos port might be useful...)

    Richard
    Winwaed Software Technology LLC
    http://www.winwaed.com
    See http://www.mapping-tools.com for MapPoint Tools

  3. #3
    JoeBo is offline Member Yellow Belt
    Join Date
    May 2007
    Posts
    31

    Re: VB.net Calcpos

    Hi Richard,

    Thanks for the response, I have reproduced the rest of the code below, the symptom is that I am not receiving a return from calcpos for the given location (“latitude and longitude are returned as 0”)

    Regards,
    Joe

    ' Compute latitude and longitude given a location object
    ' Author: Gilles Kohl
    ' (gilles@compuserve.com)
    '
    ' This code is copyrighted freeware - use freely, but please leave this
    ' header intact. Suggestions and comments welcome.

    Function Arccos(ByVal x As Double) As Double
    If x = 1 Then
    Arccos = 0
    Exit Function
    End If
    Arccos = Atan(-x / Sqrt(-x * x + 1)) + 2 * Atan(1)
    End Function

    Sub CalcPos(ByVal objMap As MapPoint.Map, ByVal locX As MapPoint.Location, ByVal dblLat As Double, ByVal dblLon As Double)
    Static locNorthPole As MapPoint.Location
    Static locSantaCruz As MapPoint.Location ' Center of western hemisphere
    Static dblHalfEarth As Double ' Half circumference of the earth (as a sphere)
    Static dblQuarterEarth As Double ' Quarter circumference of the earth (as a sphere)
    Static Pi As Double

    ' Check if initialization already done
    If locNorthPole Is Nothing Then
    locNorthPole = objMap.GetLocation(90, 0)
    locSantaCruz = objMap.GetLocation(0, -90)

    ' Compute distance between north and south poles == half earth circumference
    dblHalfEarth = objMap.Distance(locNorthPole, objMap.GetLocation(-90, 0))

    ' Quarter of that is the max distance a point may be away from locSantaCruz and still be in western hemisphere
    dblQuarterEarth = dblHalfEarth / 2
    Pi = 3.14159265358979
    End If

    ' Compute latitude from distance to north pole
    dblLat = 90 - 180 * objMap.Distance(locNorthPole, locX) / dblHalfEarth

    Dim l As Double
    Dim d As Double

    ' Compute great circle distance to locX from point on Greenwich meridian and computed Latitude
    d = objMap.Distance(objMap.GetLocation(dblLat, 0), locX)

    ' convert latitude to radian
    l = (dblLat / 180) * Pi

    ' Compute Longitude from great circle distance
    dblLon = 180 * Arccos((Cos((d * 2 * Pi) / (2 * dblHalfEarth)) - Sin(l) * Sin(l)) / (Cos(l) * Cos(l))) / Pi

    ' Correct longitude sign if located in western hemisphere
    If objMap.Distance(locSantaCruz, locX) < dblQuarterEarth Then dblLon = -dblLon
    End Sub

  4. #4
    Winwaed's Avatar
    Winwaed is offline Mapping-Tools.com Black Belt
    Join Date
    Feb 2004
    Location
    Irving,Texas
    Posts
    1,859
    Blog Entries
    60

    Re: VB.net Calcpos

    Your longitude and latitude ordinates are defined as "byval". You must use ByRef. I assume that is what it is called in VB.Net - it is in VB6 (C# uses 'ref' and 'out').

    byval is one way - data is passed into the function, but nothing is passed back.

    It is also good practice to use 'byref' for object parameters (eg. the location and map objects).
    This removes unnecessary copying/etc. (I'm not sure if I would trust a MapPoint.Map object that has been deep-copied)

    Richard
    Winwaed Software Technology LLC
    http://www.winwaed.com
    See http://www.mapping-tools.com for MapPoint Tools

  5. #5
    JoeBo is offline Member Yellow Belt
    Join Date
    May 2007
    Posts
    31

    Re: VB.net Calcpos

    Hi Richard

    I am fairly new to vb.net, as you will see from my code I still have a lot to learn.

    Your suggested alterations sorted the problem out.

    Thanks,
    Joe

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. CalcPos in python
    By Loggy in forum MapPoint Desktop Discussion
    Replies: 1
    Last Post: 08-31-2006, 10:40 PM
  2. Please help me with calcpos
    By chris701 in forum MapPoint Desktop Discussion
    Replies: 1
    Last Post: 08-11-2006, 12:55 PM
  3. Some concerns about calcpos sub
    By lucci56 in forum MapPoint Desktop Discussion
    Replies: 5
    Last Post: 07-18-2006, 11:44 AM
  4. Problems using CalcPos
    By Pete_ADL in forum MapPoint Desktop Discussion
    Replies: 4
    Last Post: 06-21-2006, 04:12 PM
  5. Confused with calcpos :s
    By Matrices in forum MapPoint Desktop Discussion
    Replies: 4
    Last Post: 09-25-2003, 10:30 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96