Welcome to MapForums!

Register, sign in, or use Facebook Connect above to join in and participate in the forum.

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

Subscribe to receive our newsletter.
Subscribe Unsubscribe
Results 1 to 8 of 8

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. #1
    Anonymous is offline 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.

    Read the full article: http://www.mp2kmag.com/articles.asp?ArticleID=13

  2. #2
    Eric Frost's Avatar
    Eric Frost is offline Administrator Black Belt
    Join Date
    Jul 1992
    Posts
    5,094
    Blog Entries
    4

    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
    	&#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
    		// header intact. Suggestions and comments welcome.
    		//
    		// 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;;
    
    				// convert latitude to radian
    			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;
    ~ Order MapPoint MapPoint 2013 Here and Get Free Access to the MapForums Downloads ~
    ~~
    ~ Upgrade to Get Access to the MapForums Downloads ~

  3. #3
    Anonymous is offline 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. #4
    paulpv is offline 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
    // header intact. Suggestions and comments welcome.
    //
    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;;
    
    	// convert latitude to radian
    	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. #5
    Anonymous is offline 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. #6
    oz
    oz is offline 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. #7
    Wilfried is offline 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;;
                double L = Deg2Rad&#40;lat&#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;
    
            private static double Rad2Deg&#40;double rad&#41;
            &#123;
                return rad * 180 / Math.PI;
            &#125;
    
            private static double Deg2Rad&#40;double degree&#41;
            &#123;
                return degree * Math.PI / 180;
            &#125;
        &#125;

  8. #8
    sidh is offline 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..?
    please suggest me some ways to go ahead.
    Thanks in advance
    --Sidh

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. MapPoint + .NET Compact Framework
    By anirban in forum Bing Maps and MapPoint Web Service
    Replies: 1
    Last Post: 05-08-2006, 05:43 AM
  2. Obtaining pushpin's territory information
    By paf33 in forum MapPoint Desktop Discussion
    Replies: 0
    Last Post: 07-30-2005, 08:53 PM
  3. mappoint control 9 and .net compact framework
    By rushang in forum MapPoint Desktop Discussion
    Replies: 1
    Last Post: 01-21-2005, 09:54 AM
  4. Obtaining corrected matched data
    By jamesvdale in forum MapPoint Desktop Discussion
    Replies: 0
    Last Post: 12-03-2004, 04:02 PM
  5. Obtaining/Exporting GPS Coordinates
    By cmolloy69 in forum MapPoint Desktop Discussion
    Replies: 3
    Last Post: 04-22-2003, 03:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27