Welcome to MapForums!

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

## A More Compact Method for Obtaining Lat/Long

This is a discussion on A More Compact Method for Obtaining Lat/Long within the MP2K Magazine Articles forums, part of the Map Forums category; An alternate method for obtaining lat/lon was recently posted to the MapPoint newsgroup by Gilles Kohl that works with pushpins ...

1. Senior Member Black Belt
Join Date
Jul 2002
Posts
5,137

## A More Compact Method for Obtaining Lat/Long

An alternate method for obtaining lat/lon was recently posted to the MapPoint newsgroup by Gilles Kohl that works with pushpins and also works in the European version.

2. ## C# Solution - Geocoding in MapPoint

This was submitted by Peter Brooke (www.findware.com) today and is a translation of Gilles Kohl's original code (VB) to C#.

Code:
```using System;
using MapPoint;

namespace MapHelper
&#123;
/// <summary>
/// Summary description for MapPointUtilities.
/// </summary>
public class MapPointUtilities
&#123;
private static MapPoint.Location locNorthPole;
private static MapPoint.Location locSantaCruz;  // Center of western hemisphere
private static double dblHalfEarth; // Half circumference of the earth &#40;as a sphere&#41;
private static double dblQuarterEarth;  // Quarter circumference of the earth &#40;as a sphere&#41;

static MapPointUtilities&#40;&#41;
&#123;
//
// TODO&#58; Add constructor logic here
//
&#125;

// Compute latitude and longitude given a location object
// Author&#58; Gilles Kohl
// &#40;gilles@compuserve.com&#41;
//
// This code is copyrighted freeware - use freely, but please leave this
//
// Converted to C# by Peter Brooke &#40;peter@findware.com&#41;. The
// same rules as above apply.
//

static public void CalcPos&#40; MapPoint.Map objMap, MapPoint.Location locX, out double dblLat, out double dblLon &#41;
&#123;
double l;
double d;

// Check if initialization already done
if &#40; locNorthPole == null &#41;
&#123;
locNorthPole = objMap.GetLocation&#40; 90, 0, 1 &#41;;
locSantaCruz = objMap.GetLocation&#40; 0, -90, 1 &#41;;

// Compute distance between north and south poles == half earth circumference
dblHalfEarth = objMap.Distance&#40; locNorthPole, objMap.GetLocation&#40;-90, 0, 1&#41; &#41;;

//  Quarter of that is the max distance a point may be away from locSantaCruz and still be in western hemisphere
dblQuarterEarth = dblHalfEarth / 2;
&#125;

// Compute latitude from distance to north pole
dblLat = 90 - 180 * objMap.Distance&#40;locNorthPole, locX&#41; / dblHalfEarth;

d = objMap.Distance&#40;objMap.GetLocation&#40;dblLat, 0, 1&#41;, locX&#41;;

l = &#40;dblLat / 180&#41; * Math.PI;

// Compute Longitude from great circle distance
dblLon = 180 * Math.Acos&#40;&#40;Math.Cos&#40;&#40;d * 2 * Math.PI&#41; / &#40;2 * dblHalfEarth&#41;&#41; - Math.Sin&#40;l&#41; * Math.Sin&#40;l&#41;&#41; / &#40;Math.Cos&#40;l&#41; * Math.Cos&#40;l&#41;&#41;&#41; / Math.PI;

//Correct longitude sign if located in western hemisphere
if &#40; objMap.Distance&#40;locSantaCruz, locX&#41; < dblQuarterEarth &#41;
dblLon = -dblLon;
&#125;
&#125;
&#125;```

3. Senior Member Black Belt
Join Date
Jul 2002
Posts
5,137

## Other C# MapPoint Examples?

Hi -

I was wondering if you had any other C# mappoint COM examples. I have been having some trouble locating and making pins for only certain types of nearby features (such as all hospitals in a 2 mile radius of boston's city center.) Is anyone familiar with the PlaceCategory property?

Thanks!
Chris

4. Junior Member White Belt
Join Date
Jan 2004
Posts
4

## C++

Not thoroughly tested, and no error handling (ex: NULL pointers and bad hr's):

Code:
```//
// Compute latitude and longitude given a location object
// Author&#58; Gilles Kohl
// &#40;gilles@compuserve.com&#41;
//
// http&#58;//www.mp2kmag.com/a13--extract.lat.lon.mappoint.html
//
// This code is copyrighted freeware - use freely, but please leave this
//
HRESULT CalcPos&#40;MapPoint&#58;&#58;_Map* oMap, MapPoint&#58;&#58;Location* oLocation, double* dLatitude, double* dLongitude&#41;
&#123;
HRESULT hr = S_OK;

static CComPtr<MapPoint&#58;&#58;Location>	locNorthPole	= NULL;
static CComPtr<MapPoint&#58;&#58;Location>	locSouthPole	= NULL;
static CComPtr<MapPoint&#58;&#58;Location>	locSantaCruz	= NULL;		// Center of western hemisphere
static double						dHalfEarth;					// Half circumference of the earth &#40;as a sphere&#41;
static double						dQuarterEarth;				// Quarter circumference of the earth &#40;as a sphere&#41;
static double						dPi				= 3.14159265358979323846;	// TODO&#58; Where is M_PI?

if &#40;locNorthPole == NULL&#41;
&#123;
hr = oMap->GetLocation&#40;90,		0,		0,	&locNorthPole&#41;;
hr = oMap->GetLocation&#40;-90,		0,		0,	&locSouthPole&#41;;
hr = oMap->GetLocation&#40;0,		-90,	0,	&locSantaCruz&#41;;

// Compute distance between north and south poles == half earth circumference
hr = oMap->Distance&#40;locNorthPole, locSouthPole, &dHalfEarth&#41;;

// Quarter of that is the max distance a point may be away from locSantaCruz and still be in western hemisphere
dQuarterEarth = dHalfEarth / 2.0;
&#125;

double dDistanceToNorthPole;
hr = oMap->Distance&#40;locNorthPole, oLocation, &dDistanceToNorthPole&#41;;

// Compute latitude from distance to north pole
*dLatitude = 90.0 - &#40;180.0 * dDistanceToNorthPole / dHalfEarth&#41;;

// Compute great circle distance to locX from point on Greenwich meridian and computed Latitude
CComPtr<MapPoint&#58;&#58;Location>	locGreenwichAndLatitude;
hr = oMap->GetLocation&#40;*dLatitude, 0, 0, &locGreenwichAndLatitude&#41;;

double d;
hr = oMap->Distance&#40;locGreenwichAndLatitude, oLocation, &d&#41;;

double l = &#40;*dLatitude / 180.0&#41; * dPi;

// Compute Longitude from great circle distance
*dLongitude = 180 * acos&#40;&#40;cos&#40;&#40;d * 2 * dPi&#41; / &#40;2 * dHalfEarth&#41;&#41; - sin&#40;l&#41; * sin&#40;l&#41;&#41; / &#40;cos&#40;l&#41; * cos&#40;l&#41;&#41;&#41; / dPi;

// Correct longitude sign if located in western hemisphere
hr = oMap->Distance&#40;locSantaCruz, oLocation, &d&#41;;
if &#40;d < dQuarterEarth&#41;
*dLongitude = -&#40;*dLongitude&#41;;

return hr;
&#125;```
Pv@swooby.com

5. Senior Member Black Belt
Join Date
Jul 2002
Posts
5,137

## WHy bother with all that?

I simply created a subclass of pushpin and keep track of the lat long in two members. It is a PITA but far less expesive and bug prone.

Plus the spherical model of the earth is only good if you don't require lots of precision (don't take that as a flame on your work you did a fine job.)

6. Junior Member White Belt
Join Date
Sep 2004
Posts
2
Here's a correction for the Arccos routine -- recently the trig functions were moved and renamed -- be sure to import System.Math

Private 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

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

I made it with a singleton class. No need to create, it create itself and there is only one instance of it in the project:

Code:
```    public sealed class MPTools
&#123;
static readonly MPTools instance = new MPTools&#40;&#41;;
static Map map;
static Location locNorthPole;
static Location locSantaCruz;
static double halfEarth;
static double quarterEarth;

// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static MPTools&#40;&#41;
&#123;
&#125;

MPTools&#40;&#41;
&#123;
&#125;

public static MPTools Instance
&#123;
get
&#123;
return instance;
&#125;
&#125;

public static void CalcPos&#40;Map activeMap, Location loc, out double lat, out double lon&#41;
&#123;
if &#40;map != activeMap&#41; &#123; // user may have load another active map since last time
map = activeMap;
locNorthPole = map.GetLocation&#40;90, 0, 0&#41;;
locSantaCruz = map.GetLocation&#40;0, -90, 0&#41;;
halfEarth = map.Distance&#40;locNorthPole, map.GetLocation&#40;-90, 0, 0&#41;&#41;;
quarterEarth = halfEarth / 2;
&#125;
lat = 90 - 180 * map.Distance&#40;locNorthPole, loc&#41; / halfEarth;
double D = map.Distance&#40;map.GetLocation&#40;lat, 0, 0&#41;, loc&#41;;
lon = 180 * Math.Acos&#40;&#40;Math.Cos&#40;D * Math.PI / halfEarth&#41; - Math.Pow&#40;Math.Sin&#40;L&#41;, 2&#41;&#41; / Math.Pow&#40;Math.Cos&#40;L&#41;, 2&#41;&#41; / Math.PI;
if &#40;map.Distance&#40;locSantaCruz, loc&#41; < quarterEarth&#41;
lon = -lon;
&#125;

&#123;
return rad * 180 / Math.PI;
&#125;

&#123;
return degree * Math.PI / 180;
&#125;
&#125;```

8. Junior Member White Belt
Join Date
Aug 2008
Posts
1

## Re: A More Compact Method for Obtaining Lat/Long

Hi ,
I want lat long information for center of city how i could get it..?
--Sidh