diff --git a/components/lazmapviewer/example/MapViewer_Demo.lpi b/components/lazmapviewer/example/MapViewer_Demo.lpi index 0fc000eca..cfca6c097 100644 --- a/components/lazmapviewer/example/MapViewer_Demo.lpi +++ b/components/lazmapviewer/example/MapViewer_Demo.lpi @@ -34,7 +34,7 @@ - + @@ -54,6 +54,10 @@ + + + + diff --git a/components/lazmapviewer/example/MapViewer_Demo.lpr b/components/lazmapviewer/example/MapViewer_Demo.lpr index b93b8b0a7..2aebd7ef3 100644 --- a/components/lazmapviewer/example/MapViewer_Demo.lpr +++ b/components/lazmapviewer/example/MapViewer_Demo.lpr @@ -5,7 +5,7 @@ program MapViewer_Demo; uses {$IFDEF UNIX}cthreads,{$ENDIF} Interfaces, // this includes the LCL widgetset - Forms, Main, gpslistform + Forms, Main, gpslistform, globals { you can add units after this }; {$R *.res} diff --git a/components/lazmapviewer/example/globals.pas b/components/lazmapviewer/example/globals.pas new file mode 100644 index 000000000..2e11a8882 --- /dev/null +++ b/components/lazmapviewer/example/globals.pas @@ -0,0 +1,19 @@ +unit globals; + +{$mode objfpc}{$H+} + +interface + +uses + Classes, SysUtils, mvEngine; + +const + DistanceUnit_Names: array[TDistanceUnits] of string = ('m', 'km', 'miles'); + +var + DistanceUnit: TDistanceUnits = duKilometers; + +implementation + +end. + diff --git a/components/lazmapviewer/example/gpslistform.lfm b/components/lazmapviewer/example/gpslistform.lfm index 05605fe29..5fcae742a 100644 --- a/components/lazmapviewer/example/gpslistform.lfm +++ b/components/lazmapviewer/example/gpslistform.lfm @@ -2,20 +2,21 @@ object GPSListViewer: TGPSListViewer Left = 282 Height = 352 Top = 135 - Width = 483 + Width = 557 Caption = 'GPS points' ClientHeight = 352 - ClientWidth = 483 + ClientWidth = 557 LCLVersion = '2.1.0.0' object ListView: TListView Left = 6 Height = 308 Top = 6 - Width = 471 + Width = 545 Align = alClient BorderSpacing.Left = 6 BorderSpacing.Top = 6 BorderSpacing.Right = 6 + Checkboxes = True Columns = < item Caption = 'ID' @@ -41,12 +42,12 @@ object GPSListViewer: TGPSListViewer Left = 0 Height = 38 Top = 314 - Width = 483 + Width = 557 Align = alBottom AutoSize = True BevelOuter = bvNone ClientHeight = 38 - ClientWidth = 483 + ClientWidth = 557 TabOrder = 1 object BtnDeletePoint: TBitBtn AnchorSideLeft.Control = BtnGoToPoint @@ -151,7 +152,7 @@ object GPSListViewer: TGPSListViewer AnchorSideTop.Control = Panel1 AnchorSideRight.Control = Panel1 AnchorSideRight.Side = asrBottom - Left = 402 + Left = 476 Height = 26 Top = 6 Width = 75 @@ -198,5 +199,19 @@ object GPSListViewer: TGPSListViewer OnClick = BtnCloseClick TabOrder = 2 end + object BtnCalcDistance: TButton + AnchorSideLeft.Control = BtnDeletePoint + AnchorSideLeft.Side = asrBottom + AnchorSideTop.Control = Panel1 + Left = 234 + Height = 25 + Top = 6 + Width = 203 + AutoSize = True + BorderSpacing.Around = 6 + Caption = 'Calc distance between two points' + OnClick = BtnCalcDistanceClick + TabOrder = 3 + end end end diff --git a/components/lazmapviewer/example/gpslistform.pas b/components/lazmapviewer/example/gpslistform.pas index 3e8c7d771..fb9a5217e 100644 --- a/components/lazmapviewer/example/gpslistform.pas +++ b/components/lazmapviewer/example/gpslistform.pas @@ -6,7 +6,7 @@ interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ButtonPanel, ComCtrls, - ExtCtrls, Buttons, mvGpsObj, mvMapViewer; + ExtCtrls, Buttons, StdCtrls, mvGpsObj, mvMapViewer; const // IDs of GPS items @@ -20,8 +20,10 @@ type BtnDeletePoint: TBitBtn; BtnGoToPoint: TBitBtn; BtnClose: TBitBtn; + BtnCalcDistance: TButton; ListView: TListView; Panel1: TPanel; + procedure BtnCalcDistanceClick(Sender: TObject); procedure BtnCloseClick(Sender: TObject); procedure BtnDeletePointClick(Sender: TObject); procedure BtnGoToPointClick(Sender: TObject); @@ -46,7 +48,8 @@ implementation {$R *.lfm} uses - mvTypes, mvEngine; + mvTypes, mvEngine, + globals; destructor TGPSListViewer.Destroy; begin @@ -92,6 +95,57 @@ begin Close; end; +procedure TGPSListViewer.BtnCalcDistanceClick(Sender: TObject); +type + TCoorRec = record + Lon: Double; + Lat: Double; + Name: String; + end; +var + i, iChecked: Integer; + gpsObj: TGpsObj; + gpsPt: TGpsPoint; + TCoorArr: array[0..1] of TCoorRec; +begin + // count checked items + iChecked := 0; + for i:=0 to ListView.Items.Count - 1 do begin + if ListView.Items.Item[i].Checked then Inc(iChecked); + end; + // + if iChecked <> 2 then begin + ShowMessage('Please select 2 items to calculate the distance.'); + end + else begin + iChecked := 0; + for i:=0 to ListView.Items.Count - 1 do begin + if ListView.Items.Item[i].Checked then begin + gpsObj := FList.Items[i]; + if gpsObj is TGpsPoint then begin + gpsPt := TGpsPoint(gpsObj); + TCoorArr[iChecked].Lat := gpsPt.Lat; + TCoorArr[iChecked].Lon := gpsPt.Lon; + TCoorArr[iChecked].Name:= gpsPt.Name; + Inc(iChecked); + end; + end; + end; + // show distance between selected items + ShowMessage('Distance between ' + TCoorArr[0].Name + ' and ' + TCoorArr[1].Name + ' is: ' + + Format('%.2n %s.', [ + CalcGeoDistance( + TCoorArr[0].Lat, + TCoorArr[0].Lon, + TCoorArr[1].Lat, + TCoorArr[1].Lon, + DistanceUnit + ), + DistanceUnit_Names[DistanceUnit] + ])); + end; +end; + procedure TGPSListViewer.BtnDeletePointClick(Sender: TObject); var gpsObj: TGpsObj; diff --git a/components/lazmapviewer/example/main.pas b/components/lazmapviewer/example/main.pas index 2f4f379a5..cc547b536 100644 --- a/components/lazmapviewer/example/main.pas +++ b/components/lazmapviewer/example/main.pas @@ -99,7 +99,7 @@ implementation uses LCLType, IniFiles, Math, FPCanvas, FPImage, IntfGraphics, mvEngine, mvExtraData, - gpslistform; + globals, gpslistform; type TLocationParam = class @@ -241,6 +241,7 @@ end; procedure TMainForm.CbDistanceUnitsChange(Sender: TObject); begin + DistanceUnit := TDistanceUnits(CbDistanceUnits.ItemIndex); UpdateViewPortSize; end; @@ -414,6 +415,7 @@ var i: Integer; s: String; pt: TRealPoint; + du: TDistanceUnits; begin ini := TMemIniFile.Create(CalcIniName); try @@ -435,6 +437,16 @@ begin pt.Lat := StrToFloatDef(ini.ReadString('MapView', 'Center.Latitude', ''), 0.0, PointFormatSettings); MapView.Center := pt; + s := ini.ReadString('MapView', 'DistanceUnits', ''); + if s <> '' then begin + for du in TDistanceUnits do + if DistanceUnit_Names[du] = s then begin + DistanceUnit := du; + CbDistanceUnits.ItemIndex := ord(du); + break; + end; + end; + List := TStringList.Create; try ini.ReadSection('Locations', List); @@ -518,9 +530,9 @@ begin MapView.GetVisibleArea.TopLeft.Lon, MapView.GetVisibleArea.TopLeft.Lat, MapView.GetVisibleArea.BottomRight.Lon, - TDistanceUnits(cbDistanceUnits.ItemIndex) + DistanceUnit ), - cbDistanceUnits.Items[cbDistanceUnits.ItemIndex] + DistanceUnit_Names[DistanceUnit] ]); InfoViewportHeight.Caption := Format('%.2n %s', [ CalcGeoDistance( @@ -528,9 +540,9 @@ begin MapView.GetVisibleArea.TopLeft.Lon, MapView.GetVisibleArea.BottomRight.Lat, MapView.GetVisibleArea.TopLeft.Lon, - TDistanceUnits(cbDistanceUnits.ItemIndex) + DistanceUnit ), - cbDistanceUnits.Items[cbDistanceUnits.ItemIndex] + DistanceUnit_Names[DistanceUnit] ]); end; @@ -552,6 +564,8 @@ begin ini.WriteString('MapView', 'Center.Longitude', FloatToStr(MapView.Center.Lon, PointFormatSettings)); ini.WriteString('MapView', 'Center.Latitude', FloatToStr(MapView.Center.Lat, PointFormatSettings)); + ini.WriteString('MapView', 'DistanceUnits', DistanceUnit_Names[DistanceUnit]); + ini.EraseSection('Locations'); for i := 0 to CbLocations.Items.Count-1 do ini.WriteString('Locations', 'Item'+IntToStr(i), CbLocations.Items[i]);