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.
Read the full article: http://www.mp2kmag.com/articles.asp?ArticleID=13
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 ...
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.
Read the full article: http://www.mp2kmag.com/articles.asp?ArticleID=13
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; } } }
~ Order MapPoint MapPoint 2013 Here and Get Free Access to the MapForums Downloads ~
~~
~ Upgrade to Get Access to the MapForums Downloads ~
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
Not thoroughly tested, and no error handling (ex: NULL pointers and bad hr's):
Pv@swooby.comCode:// // 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; }
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
www.comfortsoftware.be
www.expertsoftware.be
MapPoint coding demo
MapPoint 2011 available, buy now
Hi ,
I want lat long information for center of city how i could get it..?
please suggest me some ways to go ahead.
Thanks in advance
--Sidh
There are currently 1 users browsing this thread. (0 members and 1 guests)