Welcome to MapForums!

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

## Lat Lon problem URGENT HELP

This is a discussion on Lat Lon problem URGENT HELP within the MapPoint Desktop Discussion forums, part of the Map Forums category; Hi, I'm using in VB program the following function to get Lat and Lon, but if I execute it more ...

1. Junior Member White Belt
Join Date
Apr 2005
Posts
6

## Lat Lon problem URGENT HELP

Hi,

I'm using in VB program the following function to get Lat and Lon, but if I execute it more than once I retrieve the error
"Method Distance of object _Map failed.

I retrieve the error in this line
dblLat = 90 - 180 * objMap.Distance(locNorthPole, locX) / dblHalfEarth

Sub CalcPos(objMap As MapPoint.Map, locX As MapPoint.Location, dblLat As Double, 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
Set locNorthPole = objMap.GetLocation(90, 0)
Set 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)

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

Lat = dblLat
Lon = dblLon

End Sub

2. Senior Member Black Belt
Join Date
Nov 2004
Location
Belgium
Posts
2,433
Hi,

This code is ok if you have nothing changed on it. It is widely used in VB and other languages. You should check what is wrong with objMap. When you start to debug first thing to try is other method calls on it. they probably will fail also. Then you have to check out what is changed between the 2 function calls. Also check in debugger if all varialbles are still valid.