-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDPF.iOS.MKMapView.pas
1726 lines (1519 loc) · 64 KB
/
DPF.iOS.MKMapView.pas
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ------------------------------------------------------------------------------
// DPF.iOS.MKMapView Component
//
// Dadeh Pardazane Faragir ( DPF ) Co.
//
// Web: http://www.dpfaragir.com
//
// Developed By: Babak Yaghoobi
//
// Email #1: [email protected]
// Email #2: [email protected]
// Email #3: [email protected]
//
// ------------------------------------------------------------------------------
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ------------------------------------------------------------------------------
unit DPF.iOS.MKMapView;
interface
{$I DPF.iOS.Defs.inc}
uses
System.SysUtils,
System.Classes,
System.Types,
System.UITypes,
System.Variants,
System.Generics.Collections,
System.TypInfo,
System.Math,
Data.DBXJSON,
{$IFDEF DELPHIXE6}
System.JSON,
{$ENDIF}
Data.DB,
XML.xmldom,
XML.XMLDoc,
XML.XMLIntf,
DPF.iOS.BaseControl,
{$IFDEF IOS}
Macapi.CoreFoundation,
Macapi.ObjectiveC,
Macapi.ObjCRuntime,
IOSapi.CocoaTypes,
IOSapi.Foundation,
IOSapi.Uikit,
IOSapi.CoreGraphics,
IOSapi.CoreImage,
IOSapi.CoreLocation,
FMX.Platform.iOS,
DPF.iOS.MapKit,
DPF.iOS.Common,
{$ELSE}
// Added by Fenistil
DPF.iOS.DesignTime,
{$ENDIF}
{$IFDEF DELPHIXE5} FMX.Graphics, {$ENDIF}
FMX.Types,
FMX.Controls,
FMX.Forms;
type
TDPFMapKitMapType = ( mkmtStandard, mkmtSatellite, mkmtTypeHybrid );
TDPFMapKitUserTrackMode = ( utmNone, utmFollow, utmFollowWithHeading );
{$IFNDEF IOS}
CLLocationDegrees = Double;
CLLocationCoordinate2D = record
latitude: CLLocationDegrees;
longitude: CLLocationDegrees;
end;
{$ENDIF}
{$IFDEF IOS}
TGeoAddressInfo = record
Location: CLLocationCoordinate2D;
end;
TDPFMapView = class;
// ------------------------------------------------------------------------------
DPFMKMapView = interface( MKMapView )
['{9D39C617-8A12-4B35-A506-97C9D7B7C933}']
{$IFDEF IOS7}
// function viewForOverlay( overlay: MKOverlay ): MKOverlayView; cdecl;
// function rendererForOverlay( overlay: MKOverlay ): MKOverlayRenderer; cdecl; // iOS 7.0 and later
{$ENDIF}
procedure singleTap( Sender: pointer ); cdecl;
procedure doubleTap( Sender: pointer ); cdecl;
procedure touchesBegan( touches: NSSet; withEvent: UIEvent ); cdecl;
procedure touchesMoved( touches: NSSet; withEvent: UIEvent ); cdecl;
procedure touchesEnded( touches: NSSet; withEvent: UIEvent ); cdecl;
end;
TDPFMKMapView = class( TOCLocal )
private
LastTouchPoint : NSPoint;
LastMapPointTouch: MKMapPoint;
protected
FDPFMapView: TDPFMapView;
public
constructor Create( ADPFMapView: TDPFMapView );
function GetObjectiveCClass: PTypeInfo; override;
{$IFDEF IOS7}
// function viewForOverlay( overlay: MKOverlay ): MKOverlayView; cdecl;
// function rendererForOverlay( overlay: MKOverlay ): MKOverlayRenderer; cdecl; // iOS 7.0 and later
{$ENDIF}
procedure singleTap( Sender: pointer ); cdecl;
procedure doubleTap( Sender: pointer ); cdecl;
procedure touchesBegan( touches: NSSet; withEvent: UIEvent ); cdecl;
procedure touchesMoved( touches: NSSet; withEvent: UIEvent ); cdecl;
procedure touchesEnded( touches: NSSet; withEvent: UIEvent ); cdecl;
end;
// ----------------------------------------------------------------------------
DPFMKTileOverlayRenderer = interface( MKTileOverlayRenderer )
['{618391D5-11A7-452A-9FA3-D5892061FDE8}']
procedure drawMapRect( mapRect: MKMapRect; zoomScale: MKZoomScale; inContext: CGContextRef ); cdecl;
end;
TDPFMKTileOverlayRenderer = class( TOCLocal )
protected
FDPFMapView: TDPFMapView;
public
constructor Create( ADPFMapView: TDPFMapView; AMKTileOverlay: MKTileOverlay );
function GetObjectiveCClass: PTypeInfo; override;
procedure drawMapRect( mapRect: MKMapRect; zoomScale: MKZoomScale; inContext: CGContextRef ); cdecl;
end;
POverlayInfo = ^TOverlayInfo;
TOverlayInfo = record
Title: string;
SubTitle: string;
FillColor: TAlphaColor;
StrokeColor: TAlphaColor;
LineWidth: Single;
Alpha: Single;
end;
TKPinAnnotationColor = ( pcRed, pcGreen, pcPurple );
PAnnotationInfo = ^TAnnotationInfo;
TAnnotationInfo = record
PinImage: string;
LeftCalloutImage: string;
PinColor: TKPinAnnotationColor;
Draggable: boolean;
Alpha: Single;
ShowCallout: Boolean;
ShowCalloutButton: Boolean;
AnimateDrop: Boolean;
TagStr: string;
end;
// ------------------------------------------------------------------------------
TMKMapViewDelegate = class( TOCLocal, IMKMapViewDelegate )
private
FMapView: TDPFMapView;
//M : TDPFMKTileOverlayRenderer;
public
constructor Create( aMapView: TDPFMapView );
function mapView( mapView: MKMapView; viewForAnnotation: MKAnnotation ): MKAnnotationView; overload; cdecl;
function mapView( mapView: MKMapView; viewForOverlay: MKOverlay ): MKOverlayView; overload; cdecl;
procedure mapView( mapView: MKMapView; regionDidChangeAnimated: Boolean ); overload; cdecl;
procedure mapView( mapView: MKMapView; didSelectAnnotationView: MKAnnotationView ); overload; cdecl;
procedure mapView( mapView: MKMapView; annotationView: MKAnnotationView; calloutAccessoryControlTapped: UIControl ); overload; cdecl;
// function mapView( mapView: MKMapView; rendererForOverlay: Pointer ): MKOverlayRenderer; overload; cdecl;
procedure mapViewWillStartLocatingUser( mapView: MKMapView ); cdecl;
procedure mapViewDidStopLocatingUser( mapView: MKMapView ); cdecl;
procedure mapView( mapView: MKMapView; didUpdateUserLocation: MKUserLocation ); overload; cdecl;
procedure mapViewWillStartLoadingMap( mapView: MKMapView ); cdecl;
procedure mapViewDidFinishLoadingMap( mapView: MKMapView ); cdecl;
procedure mapViewDidFailLoadingMap( mapView: MKMapView; withError: NSError ); cdecl;
end;
{$ENDIF}
// ------------------------------------------------------------------------------
TDPFLocation = record
altitude: Double;
latitude: Double;
longitude: Double;
course: Double;
horizontalAccuracy: Double;
speed: Double;
timestamp: TDateTime;
verticalAccuracy: Double;
end;
// ------------------------------------------------------------------------------
{$IFDEF IOS}
TDPFMapViewOnPolygonClick = procedure( sender: TObject; PolygonView: MKPolygonView; Polygon: MKPolygon ) of object;
TDPFMapViewOnPolylineClick = procedure( sender: TObject; PolylineView: MKPolylineView; Polyline: MKPolyline ) of object;
TDPFMapViewOnCircleClick = procedure( sender: TObject; CircleView: MKCircleView; Circle: MKCircle ) of object;
TDPFMapViewOnAnnotationClick = procedure( sender: TObject; AnnotationView: MKAnnotationView; Annotation: MKAnnotation; AnnotationInfo: PAnnotationInfo ) of object;
TDPFMapViewOnAnnotationCalloutClick = procedure( sender: TObject; AnnotationView: MKAnnotationView; Annotation: MKAnnotation; AnnotationInfo: PAnnotationInfo ) of object;
{$ENDIF}
TDPFMapViewOnZoomChanged = procedure( sender: TObject ) of object;
TDPFMapViewOnStartLoadingMap = procedure( sender: TObject ) of object;
TDPFMapViewOnDidFinishLoadingMap = procedure( sender: TObject ) of object;
TDPFMapViewOnDidFailLoadingMap = procedure( sender: TObject ) of object;
TDPFMapViewOnWillStartLocatingUser = procedure( sender: TObject ) of object;
TDPFMapViewOnDidStopLocatingUser = procedure( sender: TObject ) of object;
TDPFMapViewOnDidUpdateUserLocation = procedure( sender: TObject; Location: TDPFLocation ) of object;
TDPFMapViewOnDrawCustomImage = procedure( sender: TObject; ZoomLevel: byte; var Image: string; var Alpha: Single ) of object;
TDPFMapViewTileOverlayType = ( otApple, otGoogle, otOpenStreet );
[ComponentPlatformsAttribute( PidWin32 or pidiOSDevice32 or pidiOSDevice64 or PidiOSSimulator )]
TDPFMapView = class( TDPFiOSBaseControl )
private
{$IFDEF IOS}
FDPFMKMapView : TDPFMKMapView;
FMKMapView : MKMapView;
FMKMapViewDelegate: TMKMapViewDelegate;
FAnnotationsList : TDictionary<Pointer, PAnnotationInfo>;
FOverlaysList : TDictionary<Pointer, POverlayInfo>;
FFromRouteLoc : CLLocationCoordinate2D;
FToRouteLoc : CLLocationCoordinate2D;
FCustomOverlay: MKTileOverlay;
{$ENDIF}
FBackgroundColor : TAlphaColor;
FShowUserLocation : Boolean;
FMapType : TDPFMapKitMapType;
FZoomEnabled : Boolean;
FScrollEnabled : Boolean;
FUserTrackingMode : TDPFMapKitUserTrackMode;
FZoomLevel : byte;
FLastZoomLevel : byte;
FOnZoomChanged : TDPFMapViewOnZoomChanged;
FOnStartLoadingMap : TDPFMapViewOnStartLoadingMap;
FOnDidFinishLoadingMap : TDPFMapViewOnDidFinishLoadingMap;
FOnDidFailLoadingMap : TDPFMapViewOnDidFailLoadingMap;
FOnWillStartLocatingUser: TDPFMapViewOnWillStartLocatingUser;
FOnDidStopLocatingUser : TDPFMapViewOnDidStopLocatingUser;
FOnDidUpdateUserLocation: TDPFMapViewOnDidUpdateUserLocation;
FTileOverlayType : TDPFMapViewTileOverlayType;
FOnDrawCustomImage : TDPFMapViewOnDrawCustomImage;
{$IFDEF IOS}
FOnPolygonClick : TDPFMapViewOnPolygonClick;
FOnPolylineClick : TDPFMapViewOnPolylineClick;
FOnCircleClick : TDPFMapViewOnCircleClick;
FOnAnnotationClick : TDPFMapViewOnAnnotationClick;
FOnAnnotationCalloutClick: TDPFMapViewOnAnnotationCalloutClick;
{$ENDIF}
procedure SetBackgroundColor( const Value: TAlphaColor );
procedure SetShowUserLocation( const Value: Boolean );
procedure SetMapType( const Value: TDPFMapKitMapType );
procedure SetZoomEnabled( const Value: Boolean );
procedure SetScrollEnabled( const Value: Boolean );
procedure SetUserTrackingMode( const Value: TDPFMapKitUserTrackMode );
function GetUserLocationVisible: Boolean;
procedure SetZoomLevel( const Value: byte );
procedure SetTileOverlayType( const Value: TDPFMapViewTileOverlayType );
protected
procedure Resize; override;
procedure Move; override;
{$IFDEF IOS}
procedure ParseJSONPairs( jo: TJSONObject );
procedure ParseJSONArray( ja: TJSONArray );
function XMLPars( XMLNode: IXMLNode ): TGeoAddressInfo;
procedure DrawRoute( PolyStr: string; FillColor: TAlphaColor; StrokeColor: TAlphaColor; LineWidth: Single; Alpha: Single; Title: string; SubTilte: string );
{$ELSE}
procedure Paint; override;
{$ENDIF}
public
{$IFDEF IOS}
procedure Loaded; override;
procedure ReloadTileOverlay;
{$ENDIF}
constructor Create( AOwner: TComponent ); override;
destructor Destroy; override;
{$IFDEF IOS}
// Return Annotation ID
function AddAnnotation( Title: string; SubTitle: string; LocationAddress: string; Zoom: byte = 0; TagStr: string = '' ): MKPointAnnotation; overload;
function AddAnnotation( Title: string; SubTitle: string; Latitude: CLLocationDegrees; longitude: CLLocationDegrees; Zoom: byte = 0; TagStr: string = ''; Draggable: Boolean = false; Alpha: Single = 1.0; PinColor: TKPinAnnotationColor = pcRed; PinImage: string = ''; LeftCalloutImage: string = ''; ShowCallout: Boolean = true;
ShowCalloutButton: Boolean = false; Animated: Boolean = true; CentreMapToAnnotation: Boolean = true ): MKPointAnnotation; overload;
procedure RemoveAnnotation( Annotation: MKPointAnnotation );
procedure RemoveAllAnnotation;
function AddCircle( Latitude, longitude: CLLocationDegrees; radius: Double; FillColor: TAlphaColor; StrokeColor: TAlphaColor; LineWidth: Single; Alpha: Single; Title: string; SubTitle: string ): MKCircle;
procedure RemoveOverlay( overlay: MKOverlay );
procedure RemoveAllOverlays;
function AddPolyLine( const Poly: TArray<CLLocationCoordinate2D>; FillColor: TAlphaColor; StrokeColor: TAlphaColor; LineWidth: Single; Alpha: Single; Title: string; SubTitle: string ): MKPolyline;
function AddPolygon( const Poly: TArray<CLLocationCoordinate2D>; FillColor: TAlphaColor; StrokeColor: TAlphaColor; LineWidth: Single; Alpha: Single; Title: string; SubTitle: string ): MKPolygon;
function GetRoutePointFrom( FromPoint: CLLocationCoordinate2D; ToPoint: CLLocationCoordinate2D ): NSArray;
function GetLatLngFromAddress( Addr: string ): CLLocationCoordinate2D;
procedure ShowRoute( FromDir: string; ToDir: string );
procedure setCenter( Latitude: CLLocationDegrees; longitude: CLLocationDegrees; Animate: Boolean );
function GetCurrentLocation: TDPFLocation;
{$ENDIF}
procedure ZoomIn;
procedure ZoomOut;
procedure ZoomBy( ZoomChange: Double );
function PointToCoordinate( APoint: TPointF ): CLLocationCoordinate2D;
function CoordinateToPoint( ACoordinate: CLLocationCoordinate2D ): TPointF;
published
property BackgroundColor : TAlphaColor read FBackgroundColor write SetBackgroundColor default TAlphaColors.Null;
property ShowUserLocation : Boolean read FShowUserLocation write SetShowUserLocation default True;
property UserLocationVisible: Boolean read GetUserLocationVisible;
property MapType : TDPFMapKitMapType read FMapType write SetMapType default mkmtStandard;
property ZoomEnabled : Boolean read FZoomEnabled write SetZoomEnabled default True;
property ScrollEnabled : Boolean read FScrollEnabled write SetScrollEnabled default True;
property UserTrackingMode : TDPFMapKitUserTrackMode read FUserTrackingMode write SetUserTrackingMode default utmNone;
property ZoomLevel : byte read FZoomLevel write SetZoomLevel default 14;
property TileOverlayType : TDPFMapViewTileOverlayType read FTileOverlayType write SetTileOverlayType default TDPFMapViewTileOverlayType.otApple;
property OnZoomChanged : TDPFMapViewOnZoomChanged read FOnZoomChanged write FOnZoomChanged;
property OnStartLoadingMap : TDPFMapViewOnStartLoadingMap read FOnStartLoadingMap write FOnStartLoadingMap;
property OnDidFinishLoadingMap : TDPFMapViewOnDidFinishLoadingMap read FOnDidFinishLoadingMap write FOnDidFinishLoadingMap;
property OnDidFailLoadingMap : TDPFMapViewOnDidFailLoadingMap read FOnDidFailLoadingMap write FOnDidFailLoadingMap;
property OnWillStartLocatingUser: TDPFMapViewOnWillStartLocatingUser read FOnWillStartLocatingUser write FOnWillStartLocatingUser;
property OnDidStopLocatingUser : TDPFMapViewOnDidStopLocatingUser read FOnDidStopLocatingUser write FOnDidStopLocatingUser;
property OnDidUpdateUserLocation: TDPFMapViewOnDidUpdateUserLocation read FOnDidUpdateUserLocation write FOnDidUpdateUserLocation;
property OnDrawCustomImage : TDPFMapViewOnDrawCustomImage read FOnDrawCustomImage write FOnDrawCustomImage;
{$IFDEF IOS}
property OnPolygonClick : TDPFMapViewOnPolygonClick read FOnPolygonClick write FOnPolygonClick;
property OnPolylineClick : TDPFMapViewOnPolylineClick read FOnPolylineClick write FOnPolylineClick;
property OnCircleClick : TDPFMapViewOnCircleClick read FOnCircleClick write FOnCircleClick;
property OnAnnotationClick : TDPFMapViewOnAnnotationClick read FOnAnnotationClick write FOnAnnotationClick;
property OnAnnotationCalloutClick: TDPFMapViewOnAnnotationCalloutClick read FOnAnnotationCalloutClick write FOnAnnotationCalloutClick;
{$ENDIF}
property UserInteraction;
property ContentMode;
property Alpha;
property Align;
property Position;
property Width;
property Height;
property Visible;
end;
// ------------------------------------------------------------------------------
implementation
{$IFDEF IOS}
// ------------------------------------------------------------------------------
function MKMapPointMake( x, y: double ): MKMapPoint;
begin
result.x := x;
result.y := y;
end;
// ------------------------------------------------------------------------------
function distanceOfPoint( pt: MKMapPoint; Poly: MKPolyline ): Double;
var
u, distance : double;
n : Integer;
ptA, ptB : MKMapPoint;
xDelta, yDelta: double;
ptClosest : MKMapPoint;
P : PMKMapPoint;
pnt : MKMapPoint;
begin
distance := MaxDouble;
P := poly.points;
for n := 0 to Poly.pointCount - 1 do
begin
Move( P^, pnt, sizeof( MKMapPoint ) );
ptA := pnt;
inc( p );
Move( P^, pnt, sizeof( MKMapPoint ) );
ptB := pnt;
xDelta := ptB.x - ptA.x;
yDelta := ptB.y - ptA.y;
if ( xDelta = 0.0 ) and ( yDelta = 0.0 ) then
begin
// Points must not be equal
continue;
end;
u := ( ( pt.x - ptA.x ) * xDelta + ( pt.y - ptA.y ) * yDelta ) / ( xDelta * xDelta + yDelta * yDelta );
if ( u < 0.0 ) then
begin
ptClosest := ptA;
end
else if ( u > 1.0 ) then
begin
ptClosest := ptB;
end
else
begin
ptClosest := MKMapPointMake( ptA.x + u * xDelta, ptA.y + u * yDelta );
end;
distance := MIN( distance, MKMetersBetweenMapPoints( ptClosest, pt ) );
end;
result := distance;
end;
// ------------------------------------------------------------------------------
function metersFromPixel( mapView: MKMapView; px: NSUInteger; pt: CGPoint ): double;
var
ptB : CGPoint;
coordA, coordB: CLLocationCoordinate2D;
begin
ptB := CGPointMake( pt.x + px, pt.y );
coordA := mapView.convertPoint( pt, mapView );
coordB := mapView.convertPoint( ptB, mapView );
result := MKMetersBetweenMapPoints( MKMapPointForCoordinate( coordA ), MKMapPointForCoordinate( coordB ) );
end;
{$ENDIF}
// ------------------------------------------------------------------------------
{ TDPFMapView }
// ------------------------------------------------------------------------------
constructor TDPFMapView.Create( AOwner: TComponent );
begin
inherited Create( AOwner );
ControlCaption := 'MapView';
FZoomLevel := 14;
FLastZoomLevel := 0;
FBackgroundColor := TAlphaColors.Null;
FShowUserLocation := True;
FMapType := mkmtStandard;
FZoomEnabled := True;
FScrollEnabled := True;
FUserTrackingMode := utmNone;
FTileOverlayType := TDPFMapViewTileOverlayType.otApple;
{$IFDEF IOS}
FCustomOverlay := nil;
FAnnotationsList := TDictionary<Pointer, PAnnotationInfo>.Create;
FOverlaysList := TDictionary<Pointer, POverlayInfo>.Create;
FMKMapViewDelegate := TMKMapViewDelegate.Create( Self );
// FMKMapView := TMKMapView.Wrap( TMKMapView.alloc.initWithFrame( CGRectMake( 0, 0, 100, 100 ) ) );
FDPFMKMapView := TDPFMKMapView.Create( self );
// FMKMapView := TMKMapView.Wrap( FDPFMKMapView.Super.init );
FMKMapView := TMKMapView.Wrap( ( FDPFMKMapView as ILocalObject ).GetObjectID ); // SZ
FMKMapView.setFrame( CGRectMake( Position.X, Position.Y, Width, Height ) );
FUIControl := FMKMapView;
{$ENDIF}
end;
// ------------------------------------------------------------------------------
destructor TDPFMapView.Destroy;
{$IFDEF IOS}
var
ArrOverlays: TArray<Pointer>;
I : Integer;
{$ENDIF}
begin
{$IFDEF IOS}
FMKMapView.setDelegate( nil );
TNSObject.Wrap( ( FMKMapViewDelegate as ILocalObject ).GetObjectID ).release; // SZ: Fix memory leak
if FMKMapViewDelegate <> nil then
FMKMapViewDelegate.DisposeOf;
if FAnnotationsList <> nil then
begin
ArrOverlays := FAnnotationsList.Keys.ToArray;
for I := 0 to high( ArrOverlays ) do
RemoveAnnotation( TMKPointAnnotation.Wrap( ArrOverlays[I] ) );
FAnnotationsList.Clear;
FAnnotationsList.DisposeOf;
end;
if FOverlaysList <> nil then
begin
ArrOverlays := FOverlaysList.Keys.ToArray;
for I := 0 to high( ArrOverlays ) do
RemoveOverlay( TMKOverlay.Wrap( ArrOverlays[I] ) );
FOverlaysList.Clear;
FOverlaysList.DisposeOf;
end;
{$ENDIF}
inherited;
end;
{$IFDEF IOS}
// ------------------------------------------------------------------------------
function TDPFMapView.AddCircle( Latitude, longitude: CLLocationDegrees; radius: Double; FillColor: TAlphaColor; StrokeColor: TAlphaColor; LineWidth: Single; Alpha: Single; Title: string; SubTitle: string ): MKCircle;
var
Coordinate: CLLocationCoordinate2D;
PCinfo : POverlayInfo;
begin
{$IFDEF IOS}
if FMKMapView <> nil then
begin
Coordinate.Latitude := Latitude;
Coordinate.longitude := longitude;
result := TMKCircle.Wrap( TMKCircle.OCClass.circleWithCenterCoordinate( Coordinate, radius ) );
result.setTitle( NSStr( Title ) );
result.setSubtitle( NSStr( subTitle ) );
result.retain;
FMKMapView.addOverlay( Result );
new( PCinfo );
PCinfo^.Title := Title;
PCinfo^.SubTitle := SubTitle;
PCinfo^.FillColor := FillColor;
PCinfo^.StrokeColor := StrokeColor;
PCinfo^.LineWidth := LineWidth;
PCinfo^.Alpha := Alpha;
FOverlaysList.AddOrSetValue( ( result as ILocalObject ).GetObjectID, PCinfo );
end;
{$ENDIF}
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.setCenter( Latitude: CLLocationDegrees; longitude: CLLocationDegrees; Animate: Boolean );
var
centerCoordinate: CLLocationCoordinate2D;
begin
centerCoordinate.latitude := Latitude;
centerCoordinate.longitude := longitude; // Fixed by Paul
FMKMapView.setCenterCoordinate( centerCoordinate, Animate );
end;
// ------------------------------------------------------------------------------
function TDPFMapView.GetCurrentLocation: TDPFLocation;
begin
if FMKMapView.userLocation.location <> nil then // Fixed by Paul
begin
Result.latitude := FMKMapView.userLocation.location.coordinate.latitude;
Result.longitude := FMKMapView.userLocation.location.coordinate.longitude;
Result.altitude := FMKMapView.userLocation.location.altitude;
Result.speed := FMKMapView.userLocation.location.speed;
Result.course := FMKMapView.userLocation.location.course;
Result.timestamp := NSDateToDateTime( FMKMapView.userLocation.location.timestamp );
Result.horizontalAccuracy := FMKMapView.userLocation.location.horizontalAccuracy;
Result.verticalAccuracy := FMKMapView.userLocation.location.verticalAccuracy;
end;
end;
// ------------------------------------------------------------------------------
function TDPFMapView.AddAnnotation( Title: string; SubTitle: string; LocationAddress: string; Zoom: Byte = 0; TagStr: string = '' ): MKPointAnnotation;
var
GeoAddr: TGeoAddressInfo;
begin
GeoAddr.Location := GetLatLngFromAddress( LocationAddress );
result := AddAnnotation( Title, SubTitle, GeoAddr.Location.latitude, GeoAddr.Location.longitude, Zoom, TagStr );
end;
// ------------------------------------------------------------------------------
function TDPFMapView.AddAnnotation( Title: string; SubTitle: string; Latitude: CLLocationDegrees; longitude: CLLocationDegrees; Zoom: byte = 0; TagStr: string = ''; Draggable: Boolean = false; Alpha: Single = 1.0; PinColor: TKPinAnnotationColor = pcRed; PinImage: string = ''; LeftCalloutImage: string = ''; ShowCallout: Boolean = true;
ShowCalloutButton: Boolean = false; Animated: Boolean = true; CentreMapToAnnotation: Boolean = true ): MKPointAnnotation;
var
Coordinate: CLLocationCoordinate2D;
value : PAnnotationInfo;
begin
if FMKMapView <> nil then
begin
Result := TMKPointAnnotation.Wrap( TMKPointAnnotation.Alloc.init );
Coordinate.Latitude := Latitude;
Coordinate.longitude := longitude;
Result.setTitle( NSStr( Title ) );
Result.setSubTitle( NSStr( SubTitle ) );
Result.setCoordinate( Coordinate );
FMKMapView.setCenterCoordinate( Result.Coordinate, True );
if Zoom <> 0 then
setCenterCoordinate( FMKMapView, Result.Coordinate, Zoom, True );
new( value );
value^.PinImage := PinImage;
value^.LeftCalloutImage := LeftCalloutImage;
value^.PinColor := PinColor;
value^.Draggable := Draggable;
value^.Alpha := Alpha;
value^.ShowCallout := ShowCallout;
value^.ShowCalloutButton := ShowCalloutButton;
value^.TagStr := TagStr;
value^.AnimateDrop := ( Animated ) and ( PinImage = '' ); // By Paul
FAnnotationsList.AddOrSetValue( ( Result as ILocalObject ).GetObjectID, Value );
// By Paul
FMKMapView.addAnnotation( Result );
if ( CentreMapToAnnotation ) or ( Zoom <> 0 ) then
begin
if Zoom <> 0 then
setCenterCoordinate( FMKMapView, Result.Coordinate, Zoom, Animated )
else
FMKMapView.setCenterCoordinate( Result.Coordinate, Animated );
end;
end;
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.RemoveAnnotation( Annotation: MKPointAnnotation );
var
value: PAnnotationInfo;
p : Pointer;
begin
if assigned( FMKMapView ) and assigned( Annotation ) and ( FAnnotationsList.Count > 0 ) then
begin
P := ( Annotation as ILocalObject ).GetObjectID;
if FAnnotationsList.ContainsKey( P ) then
begin
FAnnotationsList.TryGetValue( ( Annotation as ILocalObject ).GetObjectID, value );
Dispose( value );
FMKMapView.removeAnnotation( Annotation );
FAnnotationsList.Remove( P );
Annotation.release;
end;
end;
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.RemoveAllAnnotation;
var
I : integer;
ArrOverlays: TArray<Pointer>;
begin
if FAnnotationsList <> nil then
begin
ArrOverlays := FAnnotationsList.Keys.ToArray;
for I := 0 to high( ArrOverlays ) do
RemoveAnnotation( TMKPointAnnotation.Wrap( ArrOverlays[I] ) );
FAnnotationsList.Clear;
end;
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.RemoveOverlay( overlay: MKOverlay );
var
value: POverlayInfo;
p : Pointer;
begin
if assigned( FMKMapView ) and assigned( overlay ) and ( FOverlaysList.Count > 0 ) then
begin
P := ( Overlay as ILocalObject ).GetObjectID;
if FOverlaysList.ContainsKey( P ) then
begin
FOverlaysList.TryGetValue( ( Overlay as ILocalObject ).GetObjectID, value );
Dispose( value );
FMKMapView.removeOverlay( overlay );
FOverlaysList.Remove( P );
overlay.release;
end;
end;
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.RemoveAllOverlays;
var
I : integer;
ArrOverlays: TArray<Pointer>;
begin
if FOverlaysList <> nil then
begin
ArrOverlays := FOverlaysList.Keys.ToArray;
for I := 0 to high( ArrOverlays ) do
RemoveOverlay( TMKOverlay.Wrap( ArrOverlays[I] ) );
FOverlaysList.Clear;
end;
end;
{$ENDIF}
// ------------------------------------------------------------------------------
function TDPFMapView.GetUserLocationVisible: Boolean;
begin
Result := False;
{$IFDEF IOS}
if FMKMapView <> nil then
begin
// Fixed by Paul
Result := ( FMKMapView.userLocation.location <> nil ) and ( FMKMapView.UserLocationVisible );
end;
{$ENDIF}
end;
// ------------------------------------------------------------------------------
{$IFDEF IOS}
procedure TDPFMapView.Loaded;
begin
FMKMapView.setFrame( CGRectMake( Position.X, Position.Y, Width, Height ) );
FMKMapView.setHidden( not Visible );
SetShowUserLocation( FShowUserLocation );
SetMapType( FMapType );
SetZoomEnabled( FZoomEnabled );
SetScrollEnabled( FScrollEnabled );
SetUserTrackingMode( FUserTrackingMode );
if FZoomLevel = 0 then
FZoomLevel := DPF.iOS.MapKit.GetZoomLevel( FMKMapView );
SetZoomLevel( FZoomLevel );
SetBackgroundColor( FBackgroundColor );
FMKMapView.setDelegate( FMKMapViewDelegate.GetObjectID );
ReloadTileOverlay;
AddSubView( Self, ParentControl );
// ----------------------------
// Important
inherited;
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.ReloadTileOverlay;
begin
if not assigned( FMKMapView ) then
exit;
if assigned( FCustomOverlay ) then
begin
FMKMapView.removeOverlay( FCustomOverlay );
FCustomOverlay.release;
end;
if FTileOverlayType = otApple then
begin
FCustomOverlay := nil;
end
else if FTileOverlayType = otGoogle then
begin
FCustomOverlay := TMKTileOverlay.Wrap( TMKTileOverlay.alloc.initWithURLTemplate( NSStr( 'http://mt0.google.com/vt/x={x}&y={y}&z={z}' ) ) );
end
else if FTileOverlayType = otOpenStreet then
begin
FCustomOverlay := TMKTileOverlay.Wrap( TMKTileOverlay.alloc.initWithURLTemplate( NSStr( 'http://tile.openstreetmap.org/{z}/{x}/{y}.png' ) ) );
end;
if assigned( FOnDrawCustomImage ) and not assigned( FCustomOverlay ) then
begin
FCustomOverlay := TMKTileOverlay.Wrap( TMKTileOverlay.Alloc.initWithURLTemplate( NSStr( 'http://tile.openstreetmap.org/1/1/1.png' ) ) );
end;
if assigned( FCustomOverlay ) then
begin
FCustomOverlay.setCanReplaceMapContent( true );
FMKMapView.addOverlay( FCustomOverlay, MKOverlayLevelAboveLabels );
end;
FMKMapView.setNeedsLayout;
FMKMapView.setNeedsDisplay;
end;
{$ENDIF}
// ------------------------------------------------------------------------------
procedure TDPFMapView.Resize;
begin
inherited;
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.SetBackgroundColor( const Value: TAlphaColor );
begin
FBackgroundColor := Value;
{$IFDEF IOS}
if FMKMapView <> nil then
begin
if FBackgroundColor <> TAlphaColors.Null then
FMKMapView.setBackgroundColor( TColorToUIColor( FBackgroundColor ) )
else
FMKMapView.setBackgroundColor( TUIColor.Wrap( TUIColor.OCClass.clearColor ) );
end;
{$ENDIF}
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.SetMapType( const Value: TDPFMapKitMapType );
begin
FMapType := Value;
{$IFDEF IOS}
if FMKMapView <> nil then
begin
FMKMapView.setMapType( Integer( FMapType ) );
end;
{$ENDIF}
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.SetScrollEnabled( const Value: Boolean );
begin
FScrollEnabled := Value;
{$IFDEF IOS}
if FMKMapView <> nil then
begin
FMKMapView.setScrollEnabled( FScrollEnabled )
end;
{$ENDIF}
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.SetShowUserLocation( const Value: Boolean );
begin
FShowUserLocation := Value;
{$IFDEF IOS}
if FMKMapView <> nil then
begin
FMKMapView.setShowsUserLocation( FShowUserLocation )
end;
{$ENDIF}
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.SetTileOverlayType( const Value: TDPFMapViewTileOverlayType );
begin
FTileOverlayType := Value;
{$IFDEF IOS}
if FMKMapView <> nil then
ReloadTileOverlay;
{$ENDIF}
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.SetUserTrackingMode( const Value: TDPFMapKitUserTrackMode );
begin
FUserTrackingMode := Value;
{$IFDEF IOS}
if FMKMapView <> nil then
FMKMapView.setUserTrackingMode( Integer( FUserTrackingMode ), True );
{$ENDIF}
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.SetZoomEnabled( const Value: Boolean );
begin
FZoomEnabled := Value;
{$IFDEF IOS}
if FMKMapView <> nil then
FMKMapView.setZoomEnabled( FZoomEnabled );
{$ENDIF}
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.SetZoomLevel( const Value: byte );
begin
if ( Value < 2 ) or ( Value > 20 ) then
exit;
{$IFDEF IOS}
if FMKMapView <> nil then
if not setCenterCoordinate( FMKMapView, FMKMapView.centerCoordinate, Value, True ) then
exit;
{$ENDIF}
FZoomLevel := Value;
end;
// ------------------------------------------------------------------------------
// by Paul
procedure TDPFMapView.ZoomBy( ZoomChange: Double );
{$IFDEF IOS}
var
newRegion: MKCoordinateRegion;
{$ENDIF}
begin
{$IFDEF IOS}
if FMKMapView <> nil then
begin
newRegion.center := FMKMapView.region.center;
newRegion.span.latitudeDelta := Min( FMKMapView.region.span.latitudeDelta / ZoomChange, 180.0 );
newRegion.span.longitudeDelta := Min( FMKMapView.region.span.longitudeDelta / ZoomChange, 180.0 );
FMKMapView.setRegion( newRegion, True );
FMKMapView.regionThatFits( newRegion );
end;
{$ENDIF}
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.ZoomIn;
begin
{$IFDEF IOS}
ZoomBy( 1.2 ); // by Paul
(*
if FMKMapView <> nil then
begin
setCenterCoordinate( FMKMapView, FMKMapView.region.center, 10, True );
end;
*)
{$ENDIF}
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.ZoomOut;
{$IFDEF IOS}
// var newRegion: MKCoordinateRegion;
{$ENDIF}
begin
{$IFDEF IOS}
ZoomBy( 0.8 ); // By Paul
(*
if FMKMapView <> nil then
begin
newRegion.center := FMKMapView.region.center;
newRegion.span.latitudeDelta := Min( FMKMapView.region.span.latitudeDelta / 0.8, 180.0 );
newRegion.span.longitudeDelta := Min( FMKMapView.region.span.longitudeDelta / 0.8, 180.0 );
FMKMapView.setRegion( newRegion, True );
FMKMapView.regionThatFits( newRegion );
end;
*)
{$ENDIF}
end;
// ------------------------------------------------------------------------------
procedure TDPFMapView.Move;
begin
Resize;
end;
{$IFNDEF IOS}
// ------------------------------------------------------------------------------
procedure TDPFMapView.Paint;
var
x, r: integer;
Rect: TRectF;
begin
// Added by Fenistil
Canvas.BeginScene;
Canvas.Fill.Color := $FFF0EBD3;
Canvas.Fill.Kind := TBrushKind.Solid;
Canvas.FillRect( ClipRect, 0, 0, AllCorners, 1, TCornerType.Round );
r := 50;
if Min( Width, Height ) < 50 then
r := Round( Max( 5, Min( Width, Height ) / 3 ) );
Canvas.Stroke.Color := $FFE0D9BC;
Canvas.Stroke.Cap := TStrokeCap.Round;
Canvas.Stroke.Join := TStrokeJoin.Round;
Canvas.Stroke.Dash := TStrokeDash.Solid;
Canvas.Stroke.Thickness := 1;
// Vertical Lines
x := 10;
while x < Width do
begin
Canvas.DrawLine( PointF( x, 0 ), PointF( x, Height ), 1 );
inc( x, r );
end;
// Horizontal Lines
x := 10;
while x < Height do
begin
Canvas.DrawLine( PointF( 0, x ), PointF( Width, x ), 1 );
inc( x, r );
end;
// TrackingDot
BitmapToPosition( Self, iOS_GUI_Bitmaps.MapView.TrackingDot, Width / 2 - iOS_GUI_Bitmaps.MapView.TrackingDot.Width / 2, Height / 2 - iOS_GUI_Bitmaps.MapView.TrackingDot.Height / 2 );
BitmapToPosition( Self, iOS_GUI_Bitmaps.MapView.TrackingDotHalo, Width / 2 - iOS_GUI_Bitmaps.MapView.TrackingDotHalo.Width / 2, Height / 2 - iOS_GUI_Bitmaps.MapView.TrackingDotHalo.Height / 2 );
// Component Name
Rect := ClipRect;
Rect.Bottom := Rect.Bottom / 2;
Canvas.Font.Size := 14;
Canvas.Fill.Color := TAlphaColors.Black;
Canvas.FillText( Rect, name, True, 1, [], TTextAlign.Center, TTextAlign.Center );
Canvas.EndScene;
end;
{$ENDIF}
function TDPFMapView.PointToCoordinate( APoint: TPointF ): CLLocationCoordinate2D;