Lat/Long in MapPoint and Delphi
Hi,
i am using the MapPoint ActiveX control in Delphi 7 and i just cannot get the Lat/Long of the mouse position on map...
I tried (succesfully) to get the Lat/Long of a specific Location object, but i need something to convert the graphic XY coordinates of the mouse to Lat/Long coordinates. I also tried the function XYToLocation(X,Y: integer), but that doesn't seem to work because it never returns a valid location...
I want the lat/long coordinates so i can programmatically add a pushpin where the user clicks the map. Maybe there is an easier solution for this? Any idea would be helpful!
Marius.
Re: Lat/Long in MapPoint and Delphi
Global var
MouseX : Real ;
MouseY : Real ;
type
TLatLong = record
Lat : Double;
Long: Double;
End;
Function GetLatLong(M: TMapPointControl; Loc: Location): TLatLong;
var
LocNorthPole : Location;
LocSantaCruz : Location;
DblHalfEarth : Double;
DblQuarterEarth : Double;
DblLat : Double;
DblLon : Double;
L, D : Double;
Begin
LocNorthPole := M.ActiveMap.GetLocation(90, 0, 0);
LocSantaCruz := M.ActiveMap.GetLocation(0, -90 , 0);
DblHalfEarth := M.ActiveMap.Distance(locNorthPole, M.ActiveMap.GetLocation(-90, 0, 0));
DblQuarterEarth := dblHalfEarth / 2;
DblLat := 90 - 180 * M.ActiveMap.Distance(LocNorthPole, loc) / DblHalfEarth;
D := M.ActiveMap.Distance(M.ActiveMap.GetLocation(DblLa t, 0, 0), Loc);
L := (DblLat / 180) * Pi;
DblLon := 180 * Arccos((Cos((d * 2 * Pi) / (2 * DblHalfEarth)) - Sin(l) * Sin(l)) / (Cos(l) * Cos(l))) / Pi;
If M.ActiveMap.Distance(LocSantaCruz, Loc) < DblQuarterEarth Then DblLon := - DblLon;
Result.Long := DblLon ;
Result.Lat := DblLat ;
End;
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
Var
MyLococation: Location;
begin
MyLococation := Map.ActiveMap.XYToLocation(X, Y);
MouseX := GetLatLong(Map, Loc).Lat ;
MouseY := GetLatLong(Map, Loc).Long ;
This should fill MouseX and mouseY with the lat and long of the mouse pointer.
This could be used in form mousedown or form mousemove, This works ok for me, please let me know if you need more details
Nigel
Re: Lat/Long in MapPoint and Delphi
Yes, thanks very much... this works great. Actually, i used the same function, but it seems that i passed it on the simple Map component and not ActiveMap. By doing so, i never go a valid location on map (don't know why).
Nevertheless, thanks a lot Nigel!
Re: Lat/Long in MapPoint and Delphi
the activeX control in delphi 7 ? I was pretty sure it was not working ... unsupported interface or something... So I used the object interface..
Re: Lat/Long in MapPoint and Delphi
Well, that is an old problem that can be solved by just doing some modifications in the MapPoint_TLB file... After that you have working components!
Marius.