VB.net Calcpos

JoeBo
10-17-2007, 02:57 AM
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

Winwaed
10-17-2007, 09:23 AM
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

JoeBo
10-17-2007, 09:36 AM
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

Winwaed
10-17-2007, 11:10 AM
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

JoeBo
10-17-2007, 11:49 AM
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

 
Web mp2kmag.com
mapforums.com