From eb727001e80e0389f598497eb2566756a8ef0a4f Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Mon, 16 Apr 2018 16:35:14 +0000 Subject: [PATCH] lazMapViewer: some additions to demo program git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@6310 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../lazmapviewer/example/MapViewer_Demo.lpi | 2 +- .../lazmapviewer/example/MapViewer_Demo.lpr | 2 +- components/lazmapviewer/example/main.lfm | 294 +++++++++++++++++- components/lazmapviewer/example/main.pas | 238 +++++++++++++- .../lazmapviewer/source/mvmapviewer.pas | 2 +- 5 files changed, 514 insertions(+), 24 deletions(-) diff --git a/components/lazmapviewer/example/MapViewer_Demo.lpi b/components/lazmapviewer/example/MapViewer_Demo.lpi index 323dc923f..fe42d1725 100644 --- a/components/lazmapviewer/example/MapViewer_Demo.lpi +++ b/components/lazmapviewer/example/MapViewer_Demo.lpi @@ -41,7 +41,7 @@ - + diff --git a/components/lazmapviewer/example/MapViewer_Demo.lpr b/components/lazmapviewer/example/MapViewer_Demo.lpr index f4b4e5afc..2e3a981d3 100644 --- a/components/lazmapviewer/example/MapViewer_Demo.lpr +++ b/components/lazmapviewer/example/MapViewer_Demo.lpr @@ -16,7 +16,7 @@ begin RequireDerivedFormResource:=True; Application.Scaled:=True; Application.Initialize; - Application.CreateForm(TForm1, Form1); + Application.CreateForm(TMainForm, MainForm); Application.Run; end. diff --git a/components/lazmapviewer/example/main.lfm b/components/lazmapviewer/example/main.lfm index 92837d1cb..3fdc92cfa 100644 --- a/components/lazmapviewer/example/main.lfm +++ b/components/lazmapviewer/example/main.lfm @@ -1,12 +1,13 @@ -object Form1: TForm1 +object MainForm: TMainForm Left = 258 Height = 545 Top = 127 Width = 869 - Caption = 'Form1' + Caption = 'MainForm' ClientHeight = 545 ClientWidth = 869 OnCreate = FormCreate + OnDestroy = FormDestroy OnShow = FormShow LCLVersion = '1.9.0.0' object Panel1: TPanel @@ -15,10 +16,289 @@ object Form1: TForm1 Top = 0 Width = 237 Align = alRight - Caption = 'Panel1' + ClientHeight = 545 + ClientWidth = 237 TabOrder = 0 + object CbProviders: TComboBox + AnchorSideLeft.Control = LblProviders + AnchorSideLeft.Side = asrBottom + AnchorSideTop.Control = Panel1 + AnchorSideRight.Control = Panel1 + AnchorSideRight.Side = asrBottom + Left = 69 + Height = 23 + Top = 9 + Width = 159 + Anchors = [akTop, akLeft, akRight] + BorderSpacing.Left = 8 + BorderSpacing.Top = 8 + BorderSpacing.Right = 8 + ItemHeight = 15 + OnChange = CbProvidersChange + Style = csDropDownList + TabOrder = 0 + end + object LblProviders: TLabel + AnchorSideLeft.Control = Panel1 + AnchorSideTop.Control = CbProviders + AnchorSideTop.Side = asrCenter + Left = 9 + Height = 15 + Top = 13 + Width = 52 + BorderSpacing.Left = 8 + Caption = 'Providers:' + FocusControl = CbProviders + ParentColor = False + end + object CbUseThreads: TCheckBox + AnchorSideLeft.Control = LblProviders + Left = 9 + Height = 19 + Top = 40 + Width = 62 + Caption = 'Threads' + Checked = True + OnChange = CbUseThreadsChange + State = cbChecked + TabOrder = 1 + end + object CbDoubleBuffer: TCheckBox + AnchorSideLeft.Control = CbUseThreads + AnchorSideLeft.Side = asrBottom + Left = 95 + Height = 19 + Top = 40 + Width = 87 + BorderSpacing.Left = 24 + BorderSpacing.Right = 9 + Caption = 'DblBuffering' + Checked = True + OnChange = CbDoubleBufferChange + State = cbChecked + TabOrder = 2 + end + object ZoomTrackBar: TTrackBar + AnchorSideLeft.Control = CbProviders + AnchorSideTop.Control = CbDoubleBuffer + AnchorSideTop.Side = asrBottom + AnchorSideRight.Control = Panel1 + AnchorSideRight.Side = asrBottom + Left = 69 + Height = 40 + Top = 59 + Width = 167 + Max = 17 + Min = 1 + OnChange = ZoomTrackBarChange + Position = 1 + TickMarks = tmBoth + Anchors = [akTop, akLeft, akRight] + TabOrder = 3 + end + object LblZoom: TLabel + AnchorSideLeft.Control = Panel1 + AnchorSideTop.Control = ZoomTrackBar + AnchorSideTop.Side = asrCenter + Left = 9 + Height = 15 + Top = 72 + Width = 35 + BorderSpacing.Left = 8 + Caption = 'Zoom:' + ParentColor = False + end + object CbMouseCoords: TGroupBox + AnchorSideLeft.Control = LblProviders + AnchorSideTop.Control = ZoomTrackBar + AnchorSideTop.Side = asrBottom + AnchorSideRight.Control = CbProviders + AnchorSideRight.Side = asrBottom + Left = 9 + Height = 66 + Top = 107 + Width = 219 + Anchors = [akTop, akLeft, akRight] + AutoSize = True + BorderSpacing.Top = 8 + Caption = 'Mouse position' + ClientHeight = 46 + ClientWidth = 215 + TabOrder = 4 + object LblPositionLongitude: TLabel + AnchorSideLeft.Control = CbMouseCoords + AnchorSideTop.Control = CbMouseCoords + Left = 16 + Height = 15 + Top = 4 + Width = 54 + BorderSpacing.Left = 16 + BorderSpacing.Top = 4 + Caption = 'Longitude' + ParentColor = False + end + object LblPositionLatitude: TLabel + AnchorSideLeft.Control = CbMouseCoords + AnchorSideTop.Control = LblPositionLongitude + AnchorSideTop.Side = asrBottom + Left = 16 + Height = 15 + Top = 23 + Width = 46 + BorderSpacing.Left = 16 + BorderSpacing.Top = 4 + BorderSpacing.Bottom = 8 + Caption = 'Latitude:' + ParentColor = False + end + object InfoPositionLongitude: TLabel + AnchorSideLeft.Control = CbMouseCoords + AnchorSideTop.Control = CbMouseCoords + AnchorSideRight.Control = CbMouseCoords + AnchorSideRight.Side = asrBottom + Left = 165 + Height = 15 + Top = 4 + Width = 34 + Alignment = taRightJustify + Anchors = [akTop, akRight] + BorderSpacing.Top = 4 + BorderSpacing.Right = 16 + Caption = 'Label2' + ParentColor = False + end + object InfoPositionLatitude: TLabel + AnchorSideLeft.Control = CbMouseCoords + AnchorSideTop.Control = InfoPositionLongitude + AnchorSideTop.Side = asrBottom + AnchorSideRight.Control = CbMouseCoords + AnchorSideRight.Side = asrBottom + Left = 165 + Height = 15 + Top = 23 + Width = 34 + Alignment = taRightJustify + Anchors = [akTop, akRight] + BorderSpacing.Top = 4 + BorderSpacing.Right = 16 + BorderSpacing.Bottom = 8 + Caption = 'Label2' + ParentColor = False + end + end + object GbCenterCoords: TGroupBox + AnchorSideLeft.Control = LblProviders + AnchorSideTop.Control = CbMouseCoords + AnchorSideTop.Side = asrBottom + AnchorSideRight.Control = CbProviders + AnchorSideRight.Side = asrBottom + Left = 9 + Height = 66 + Top = 185 + Width = 219 + Anchors = [akTop, akLeft, akRight] + AutoSize = True + BorderSpacing.Top = 12 + Caption = 'Center' + ClientHeight = 46 + ClientWidth = 215 + TabOrder = 5 + object LblCenterLongitude: TLabel + AnchorSideLeft.Control = GbCenterCoords + AnchorSideTop.Control = GbCenterCoords + Left = 16 + Height = 15 + Top = 4 + Width = 54 + BorderSpacing.Left = 16 + BorderSpacing.Top = 4 + Caption = 'Longitude' + ParentColor = False + end + object LblCenterLatitude: TLabel + AnchorSideLeft.Control = GbCenterCoords + AnchorSideTop.Control = LblCenterLongitude + AnchorSideTop.Side = asrBottom + Left = 16 + Height = 15 + Top = 23 + Width = 46 + BorderSpacing.Left = 16 + BorderSpacing.Top = 4 + BorderSpacing.Bottom = 8 + Caption = 'Latitude:' + ParentColor = False + end + object InfoCenterLongitude: TLabel + AnchorSideLeft.Control = GbCenterCoords + AnchorSideTop.Control = GbCenterCoords + AnchorSideRight.Control = GbCenterCoords + AnchorSideRight.Side = asrBottom + Left = 165 + Height = 15 + Top = 4 + Width = 34 + Alignment = taRightJustify + Anchors = [akTop, akRight] + BorderSpacing.Top = 4 + BorderSpacing.Right = 16 + Caption = 'Label2' + ParentColor = False + end + object InfoCenterLatitude: TLabel + AnchorSideLeft.Control = GbCenterCoords + AnchorSideTop.Control = InfoCenterLongitude + AnchorSideTop.Side = asrBottom + AnchorSideRight.Control = GbCenterCoords + AnchorSideRight.Side = asrBottom + Left = 165 + Height = 15 + Top = 23 + Width = 34 + Alignment = taRightJustify + Anchors = [akTop, akRight] + BorderSpacing.Top = 4 + BorderSpacing.Right = 16 + BorderSpacing.Bottom = 8 + Caption = 'Label2' + ParentColor = False + end + end + object CbLocations: TComboBox + AnchorSideLeft.Control = LblProviders + AnchorSideTop.Control = BtnSearch + AnchorSideTop.Side = asrCenter + AnchorSideRight.Control = BtnSearch + Left = 9 + Height = 23 + Top = 264 + Width = 150 + Anchors = [akTop, akLeft, akRight] + BorderSpacing.Right = 8 + BorderSpacing.Bottom = 4 + DropDownCount = 24 + ItemHeight = 15 + TabOrder = 6 + Text = 'New York' + end + object BtnSearch: TButton + AnchorSideTop.Control = GbCenterCoords + AnchorSideTop.Side = asrBottom + AnchorSideRight.Control = CbProviders + AnchorSideRight.Side = asrBottom + Left = 167 + Height = 25 + Top = 263 + Width = 61 + Anchors = [akTop, akRight] + AutoSize = True + BorderSpacing.Top = 12 + Caption = 'Search' + OnClick = BtnSearchClick + TabOrder = 7 + end end - object MapView1: TMapView + object MapView: TMapView Left = 0 Height = 545 Top = 0 @@ -31,9 +311,11 @@ object Form1: TForm1 MapProvider = 'OpenStreetMap Mapnik' UseThreads = True Zoom = 0 + OnZoomChange = MapViewZoomChange + OnMouseMove = MapViewMouseMove end object GeoNames: TMVGeoNames - left = 481 - top = 256 + left = 520 + top = 288 end end diff --git a/components/lazmapviewer/example/main.pas b/components/lazmapviewer/example/main.pas index ffda91280..9fd1b5472 100644 --- a/components/lazmapviewer/example/main.pas +++ b/components/lazmapviewer/example/main.pas @@ -5,49 +5,257 @@ unit Main; interface uses - Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, mvgeonames, - mvMapViewer; + Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, + ComCtrls, mvgeonames, mvMapViewer; type - { TForm1 } + { TMainForm } - TForm1 = class(TForm) - MapView1: TMapView; + TMainForm = class(TForm) + BtnSearch: TButton; + CbDoubleBuffer: TCheckBox; + CbLocations: TComboBox; + CbProviders: TComboBox; + CbUseThreads: TCheckBox; + CbMouseCoords: TGroupBox; + GbCenterCoords: TGroupBox; + InfoCenterLatitude: TLabel; + InfoCenterLongitude: TLabel; + LblCenterLatitude: TLabel; + LblPositionLongitude: TLabel; + LblPositionLatitude: TLabel; + InfoPositionLongitude: TLabel; + InfoPositionLatitude: TLabel; + LblCenterLongitude: TLabel; + LblProviders: TLabel; + LblZoom: TLabel; + MapView: TMapView; GeoNames: TMVGeoNames; Panel1: TPanel; + ZoomTrackBar: TTrackBar; + procedure BtnSearchClick(Sender: TObject); + procedure CbDoubleBufferChange(Sender: TObject); + procedure CbProvidersChange(Sender: TObject); + procedure CbUseThreadsChange(Sender: TObject); procedure FormCreate(Sender: TObject); + procedure FormDestroy(Sender: TObject); procedure FormShow(Sender: TObject); + procedure MapViewMouseMove(Sender: TObject; Shift: TShiftState; X, + Y: Integer); + procedure MapViewZoomChange(Sender: TObject); + procedure ZoomTrackBarChange(Sender: TObject); + private + procedure UpdateLocationHistory(ALocation: String); public + procedure ReadFromIni; + procedure WriteToIni; end; var - Form1: TForm1; + MainForm: TMainForm; implementation {$R *.lfm} uses - mvTypes; + IniFiles, mvTypes; -{ TForm1 } +const + MAX_LOCATIONS_HISTORY = 50; + HOMEDIR = ''; -procedure TForm1.FormCreate(Sender: TObject); +var + PointFormatSettings: TFormatsettings; + +function CalcIniName: String; begin - MapView1.DoubleBuffered := true; - MapView1.Zoom := 7; - GeoNames.LocationName := 'New York'; - MapView1.Center := GeoNames.Search(MapView1.DownloadEngine); + Result := ChangeFileExt(Application.ExeName, '.ini'); end; -procedure TForm1.FormShow(Sender: TObject); + +{ TMainForm } + +procedure TMainForm.BtnSearchClick(Sender: TObject); begin - MapView1.Active := true; + MapView.Center := GeoNames.Search(CbLocations.Text, MapView.DownloadEngine); + { + ClearFoundLocations; + GeoNames.LocationName := CbLocations.Text; + GeoNames.ListLocations(MapView.DownloadEngine); + //CbFoundLocations.Text := CbFoundLocations.Items[0]; + UpdateDropdownWidth(CbFoundLocations); + } + UpdateLocationHistory(CbLocations.Text); end; +procedure TMainForm.CbDoubleBufferChange(Sender: TObject); +begin + MapView.DoubleBuffered := CbDoubleBuffer.Checked; +end; + +procedure TMainForm.CbProvidersChange(Sender: TObject); +begin + MapView.MapProvider := CbProviders.Text; +end; + +procedure TMainForm.CbUseThreadsChange(Sender: TObject); +begin + MapView.UseThreads := CbUseThreads.Checked; +end; + +procedure TMainForm.FormCreate(Sender: TObject); +begin + ForceDirectories(HOMEDIR + 'cache/'); + MapView.CachePath := HOMEDIR + 'cache/'; + MapView.GetMapProviders(CbProviders.Items); + CbProviders.ItemIndex := CbProviders.Items.Indexof(MapView.MapProvider); + MapView.DoubleBuffered := true; + MapView.Zoom := 1; + CbUseThreads.Checked := MapView.UseThreads; + CbDoubleBuffer.Checked := MapView.DoubleBuffered; +// GeoNames.LocationName := 'New York'; +// MapView.Center := GeoNames.Search('New York', MapView.DownloadEngine); + + ReadFromIni; +end; + +procedure TMainForm.FormDestroy(Sender: TObject); +begin + WriteToIni; +end; + +procedure TMainForm.FormShow(Sender: TObject); +begin + MapView.Active := true; +end; + +procedure TMainForm.MapViewMouseMove(Sender: TObject; Shift: TShiftState; + X, Y: Integer); +var + rPt: TRealPoint; +begin + rPt := MapView.ScreenToLonLat(Point(X, Y)); + (* + p := MapView.GetMouseMapPixel(X, Y); + LblZoom.Caption := Format('Pixel: %d:%d', [p.X, p.Y]); + p := mv.GetMouseMapTile(X, Y); + Label3.Caption := Format('Tile: %d:%d', [p.X, p.Y]); + r := mv.GetMouseMapLongLat(X, Y); + *) + InfoPositionLongitude.Caption := Format('%.6f°', [rPt.Lon]); + InfoPositionLatitude.Caption := Format('%.6f°', [rPt.Lat]); + + rPt := MapView.Center; + InfoCenterLongitude.Caption := Format('%.6f°', [rPt.Lon]); + InfoCenterLatitude.Caption := Format('%.6f°', [rPt.Lat]); +end; + +procedure TMainForm.MapViewZoomChange(Sender: TObject); +begin + ZoomTrackbar.Position := MapView.Zoom; +end; + +procedure TMainForm.ReadFromIni; +var + ini: TCustomIniFile; + List: TStringList; + L, T, W, H: Integer; + R: TRect; + i: Integer; + s: String; + pt: TRealPoint; +begin + ini := TMemIniFile.Create(CalcIniName); + try + R := Screen.DesktopRect; + L := ini.ReadInteger('MainForm', 'Left', Left); + T := ini.ReadInteger('MainForm', 'Top', Top); + W := ini.ReadInteger('MainForm', 'Width', Width); + H := ini.ReadInteger('MainForm', 'Height', Height); + if L + W > R.Right then L := R.Right - W; + if L < R.Left then L := R.Left; + if T + H > R.Bottom then T := R.Bottom - H; + if T < R.Top then T := R.Top; + SetBounds(L, T, W, H); + + MapView.MapProvider := ini.ReadString('MapView', 'Provider', MapView.MapProvider); + CbProviders.Text := MapView.MapProvider; + MapView.Zoom := ini.ReadInteger('MapView', 'Zoom', MapView.Zoom); + pt.Lon := StrToFloatDef(ini.ReadString('MapView', 'Center.Longitude', ''), 0.0, PointFormatSettings); + pt.Lat := StrToFloatDef(ini.ReadString('MapView', 'Center.Latitude', ''), 0.0, PointFormatSettings); + MapView.Center := pt; + + List := TStringList.Create; + try + ini.ReadSection('Locations', List); + for i:=0 to List.Count-1 do begin + s := ini.ReadString('Locations', List[i], ''); + if s <> '' then + CbLocations.Items.Add(s); + end; + finally + List.Free; + end; + + finally + ini.Free; + end; +end; + +procedure TMainForm.UpdateLocationHistory(ALocation: String); +var + idx: Integer; +begin + idx := CbLocations.Items.IndexOf(ALocation); + if idx <> -1 then + CbLocations.Items.Delete(idx); + CbLocations.Items.Insert(0, ALocation); + while CbLocations.Items.Count > MAX_LOCATIONS_HISTORY do + CbLocations.Items.Delete(Cblocations.items.Count-1); + CbLocations.Text := ALocation; +end; + +procedure TMainForm.WriteToIni; +var + ini: TCustomIniFile; + L: TStringList; + i: Integer; +begin + ini := TMemIniFile.Create(CalcIniName); + try + ini.WriteInteger('MainForm', 'Left', Left); + ini.WriteInteger('MainForm', 'Top', Top); + ini.WriteInteger('MainForm', 'Width', Width); + ini.WriteInteger('MainForm', 'Height', Height); + + ini.WriteString('MapView', 'Provider', MapView.MapProvider); + ini.WriteInteger('MapView', 'Zoom', MapView.Zoom); + ini.WriteString('MapView', 'Center.Longitude', FloatToStr(MapView.Center.Lon, PointFormatSettings)); + ini.WriteString('MapView', 'Center.Latitude', FloatToStr(MapView.Center.Lat, PointFormatSettings)); + + ini.EraseSection('Locations'); + for i := 0 to CbLocations.Items.Count-1 do + ini.WriteString('Locations', 'Item'+IntToStr(i), CbLocations.Items[i]); + + finally + ini.Free; + end; +end; + +procedure TMainForm.ZoomTrackBarChange(Sender: TObject); +begin + MapView.Zoom := ZoomTrackBar.Position; + LblZoom.Caption := Format('Zoom (%d):', [ZoomTrackbar.Position]); +end; + + +initialization + PointFormatSettings.DecimalSeparator := '.'; + end. diff --git a/components/lazmapviewer/source/mvmapviewer.pas b/components/lazmapviewer/source/mvmapviewer.pas index 50fc85907..8a24710a4 100644 --- a/components/lazmapviewer/source/mvmapviewer.pas +++ b/components/lazmapviewer/source/mvmapviewer.pas @@ -17,7 +17,7 @@ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. } -unit mvmapviewer; +unit MvMapViewer; {$MODE objfpc}{$H+}