Community of VE/MapPoint Users and Developers
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 ...
| |||||||
| Register | Blogs | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| |||
| A More Compact Method for Obtaining Lat/Long Read the full article: http://www.mp2kmag.com/articles.asp?ArticleID=13 |
| ||||
| 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
{
/// <summary>
/// Summary description for MapPointUtilities.
/// </summary>
public class MapPointUtilities
{
private static MapPoint.Location locNorthPole;
private static MapPoint.Location locSantaCruz; // Center of western hemisphere
private static double dblHalfEarth; // Half circumference of the earth (as a sphere)
private static double dblQuarterEarth; // Quarter circumference of the earth (as a sphere)
static MapPointUtilities()
{
//
// TODO: Add constructor logic here
//
}
// 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.
//
// Converted to C# by Peter Brooke (peter@findware.com). The
// same rules as above apply.
//
static public void CalcPos( MapPoint.Map objMap, MapPoint.Location locX, out double dblLat, out double dblLon )
{
double l;
double d;
// Check if initialization already done
if ( locNorthPole == null )
{
locNorthPole = objMap.GetLocation( 90, 0, 1 );
locSantaCruz = objMap.GetLocation( 0, -90, 1 );
// Compute distance between north and south poles == half earth circumference
dblHalfEarth = objMap.Distance( locNorthPole, objMap.GetLocation(-90, 0, 1) );
// Quarter of that is the max distance a point may be away from locSantaCruz and still be in western hemisphere
dblQuarterEarth = dblHalfEarth / 2;
}
// Compute latitude from distance to north pole
dblLat = 90 - 180 * objMap.Distance(locNorthPole, locX) / dblHalfEarth;
d = objMap.Distance(objMap.GetLocation(dblLat, 0, 1), locX);
// convert latitude to radian
l = (dblLat / 180) * Math.PI;
// Compute Longitude from great circle distance
dblLon = 180 * Math.Acos((Math.Cos((d * 2 * Math.PI) / (2 * dblHalfEarth)) - Math.Sin(l) * Math.Sin(l)) / (Math.Cos(l) * Math.Cos(l))) / Math.PI;
//Correct longitude sign if located in western hemisphere
if ( objMap.Distance(locSantaCruz, locX) < dblQuarterEarth )
dblLon = -dblLon;
}
}
}
__________________ |
| |||
| 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 |
| |||
| 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: Gilles Kohl
// (gilles@compuserve.com)
//
// http://www.mp2kmag.com/a13--extract.lat.lon.mappoint.html
//
// This code is copyrighted freeware - use freely, but please leave this
// header intact. Suggestions and comments welcome.
//
HRESULT CalcPos(MapPoint::_Map* oMap, MapPoint::Location* oLocation, double* dLatitude, double* dLongitude)
{
HRESULT hr = S_OK;
static CComPtr<MapPoint::Location> locNorthPole = NULL;
static CComPtr<MapPoint::Location> locSouthPole = NULL;
static CComPtr<MapPoint::Location> locSantaCruz = NULL; // Center of western hemisphere
static double dHalfEarth; // Half circumference of the earth (as a sphere)
static double dQuarterEarth; // Quarter circumference of the earth (as a sphere)
static double dPi = 3.14159265358979323846; // TODO: Where is M_PI?
if (locNorthPole == NULL)
{
hr = oMap->GetLocation(90, 0, 0, &locNorthPole);
hr = oMap->GetLocation(-90, 0, 0, &locSouthPole);
hr = oMap->GetLocation(0, -90, 0, &locSantaCruz);
// Compute distance between north and south poles == half earth circumference
hr = oMap->Distance(locNorthPole, locSouthPole, &dHalfEarth);
// Quarter of that is the max distance a point may be away from locSantaCruz and still be in western hemisphere
dQuarterEarth = dHalfEarth / 2.0;
}
double dDistanceToNorthPole;
hr = oMap->Distance(locNorthPole, oLocation, &dDistanceToNorthPole);
// Compute latitude from distance to north pole
*dLatitude = 90.0 - (180.0 * dDistanceToNorthPole / dHalfEarth);
// Compute great circle distance to locX from point on Greenwich meridian and computed Latitude
CComPtr<MapPoint::Location> locGreenwichAndLatitude;
hr = oMap->GetLocation(*dLatitude, 0, 0, &locGreenwichAndLatitude);
double d;
hr = oMap->Distance(locGreenwichAndLatitude, oLocation, &d);
// convert latitude to radian
double l = (*dLatitude / 180.0) * dPi;
// Compute Longitude from great circle distance
*dLongitude = 180 * acos((cos((d * 2 * dPi) / (2 * dHalfEarth)) - sin(l) * sin(l)) / (cos(l) * cos(l))) / dPi;
// Correct longitude sign if located in western hemisphere
hr = oMap->Distance(locSantaCruz, oLocation, &d);
if (d < dQuarterEarth)
*dLongitude = -(*dLongitude);
return hr;
}
|
| |||
| 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.) |
| |||
|
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 |
| |||
|
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
{
static readonly MPTools instance = new MPTools();
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()
{
}
MPTools()
{
}
public static MPTools Instance
{
get
{
return instance;
}
}
public static void CalcPos(Map activeMap, Location loc, out double lat, out double lon)
{
if (map != activeMap) { // user may have load another active map since last time
map = activeMap;
locNorthPole = map.GetLocation(90, 0, 0);
locSantaCruz = map.GetLocation(0, -90, 0);
halfEarth = map.Distance(locNorthPole, map.GetLocation(-90, 0, 0));
quarterEarth = halfEarth / 2;
}
lat = 90 - 180 * map.Distance(locNorthPole, loc) / halfEarth;
double D = map.Distance(map.GetLocation(lat, 0, 0), loc);
double L = Deg2Rad(lat);
lon = 180 * Math.Acos((Math.Cos(D * Math.PI / halfEarth) - Math.Pow(Math.Sin(L), 2)) / Math.Pow(Math.Cos(L), 2)) / Math.PI;
if (map.Distance(locSantaCruz, loc) < quarterEarth)
lon = -lon;
}
private static double Rad2Deg(double rad)
{
return rad * 180 / Math.PI;
}
private static double Deg2Rad(double degree)
{
return degree * Math.PI / 180;
}
}
__________________ rgds, Wilfried Mestdagh www.mestdagh.biz MapPoint coding demo Order MapPoint 2009 with Routing and User Tools Spreadsheet |
![]() |
| Tags |
| compact, lat or long, method, obtaining |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| MapPoint + .NET Compact Framework | anirban | MapPoint Web Service and Virtual Earth | 1 | 05-08-2006 05:43 AM |
| Obtaining pushpin's territory information | paf33 | MapPoint 2006/2009 Discussion | 0 | 07-30-2005 08:53 PM |
| mappoint control 9 and .net compact framework | rushang | MapPoint 2006/2009 Discussion | 1 | 01-21-2005 09:54 AM |
| Obtaining corrected matched data | jamesvdale | MapPoint 2006/2009 Discussion | 0 | 12-03-2004 04:02 PM |
| Obtaining/Exporting GPS Coordinates | cmolloy69 | MapPoint 2006/2009 Discussion | 3 | 04-22-2003 03:13 PM |