MapPoint Forums

MapForums

Community of VE/MapPoint Users and Developers




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 ...


Go Back   MapPoint Forums > Map Forums > MP2K Magazine Articles

Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read



Click here to register

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-20-2001
Senior Member
Black Belt
 
Join Date: Jul 2002
Posts: 5,138
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 10-16-2002
Eric Frost's Avatar
Senior Member
Black Belt
 
Join Date: Jul 1992
Posts: 2,470
Blog Entries: 1
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;
		}
	}
}
__________________
~ Now taking orders for MapPoint 2009 ~
~
~ Upgrade to MapForums Plus membership ~
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3 (permalink)  
Old 01-08-2003
Senior Member
Black Belt
 
Join Date: Jul 2002
Posts: 5,138
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4 (permalink)  
Old 01-28-2004
Junior Member
White Belt
 
Join Date: Jan 2004
Posts: 4
Send a message via ICQ to paulpv Send a message via MSN to paulpv Send a message via Yahoo to paulpv
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;
}
Pv@swooby.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5 (permalink)  
Old 03-16-2004
Senior Member
Black Belt
 
Join Date: Jul 2002
Posts: 5,138
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.)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6 (permalink)  
Old 10-03-2004
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7 (permalink)  
Old 12-03-2005
Senior Member
Black Belt
 
Join Date: Nov 2004
Posts: 2,112
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;
        }
    }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8 (permalink)  
Old 08-26-2008
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
compact, lat or long, method, obtaining


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

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


All times are GMT -5. The time now is 06:21 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
MP2K Magazine
Visitor Map


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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54