Skip to content

Instantly share code, notes, and snippets.

@DelphiWorlds
Created May 2, 2021 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DelphiWorlds/2e65ad9998a017351778ad2f321fdbd1 to your computer and use it in GitHub Desktop.
Save DelphiWorlds/2e65ad9998a017351778ad2f321fdbd1 to your computer and use it in GitHub Desktop.
Create 2 markers, not far from each other
type
TForm1 = class(TForm)
Map: TMapView;
private
FMarkerA: TMapMarker;
FMarkerB: TMapMarker;
procedure AddMarkers;
public
constructor Create(AOwner: TComponent); override;
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
constructor TForm1.Create(AOwner: TComponent);
begin
inherited;
AddMarkers;
end;
procedure TForm1.AddMarkers;
var
LDescriptor: TMapMarkerDescriptor;
begin
Map.Location := TMapCoordinate.Create(30.3970, -97.7300);
LDescriptor := TMapMarkerDescriptor.Create(Map.Location, 'EMBT');
FMarkerA := Map.AddMarker(LDescriptor);
Map.Location := TMapCoordinate.Create(30.3966, -97.7275);
LDescriptor := TMapMarkerDescriptor.Create(Map.Location, 'Near EMBT');
FMarkerB := Map.AddMarker(LDescriptor);
Map.Zoom := 18;
end;
@DelphiWorlds
Copy link
Author

Related to this SO question: https://stackoverflow.com/questions/67300467/mapview-marker-show-previous-on-ios

In FMX.Maps.iOS, there is the TMapKitMapView.MapEvent method:

procedure TMapKitMapView.MapEvent(const Event: TMapEvent);
begin
  case Event.Kind of
    TMapEventKind.CameraChanged:
      begin
        SyncCameraPosition;
        FHostControl.DoCameraChanged;
      end;
    TMapEventKind.MarkerClick:
      begin
        // Marker has been selected
        FSelectedMarker := Event.Marker;
        FHostControl.DoMarkerClick(Event.Marker);
      end;
    TMapEventKind.MarkerDeselect:
        FSelectedMarker := nil;
    TMapEventKind.MarkerDragStart:
      FHostControl.DoMarkerDragStart(Event.Marker);
    TMapEventKind.MarkerDrag:
      FHostControl.DoMarkerDrag(Event.Marker);
    TMapEventKind.MarkerDragEnd:
      FHostControl.DoMarkerDragEnd(Event.Marker);
    TMapEventKind.MapClick:
      if FDragState = MKAnnotationViewDragStateNone then
        if FSelectedMarker = nil then
          FHostControl.DoMapClick(Event.LatLng)
        else
          FHostControl.DoMarkerClick(FSelectedMarker);
    TMapEventKind.MapLongClick:
      if FDragState = MKAnnotationViewDragStateNone then
        if FSelectedMarker = nil then
          FHostControl.DoMapLongClick(Event.LatLng);
    TMapEventKind.MapDoubleClick:
      if FSelectedMarker = nil then
        FHostControl.DoMapDoubleClick(Event.LatLng)
      else
        FHostControl.DoMarkerDoubleClick(FSelectedMarker);
  end;
end;

Using the steps in the report, i.e.:

  1. Tap Marker A
  2. Tap Marker B
  3. Tap Map
  4. Tap Marker A

Here are the events fired in TMapKitMapView.MapEvent for each step

Event.Kind = MapClick
Event.Kind = MarkerClick (i.e. Marker A)

Event.Kind = MapClick
Event.Kind = MarkerDeselected (i.e. Marker A)
Event.Kind = MarkerClick (i.e. Marker B)

Event.Kind = MapClick
Event.Kind = MarkerDeselected (i.e. Marker B)

Event.Kind = MapClick
Event.Kind = MarkerClick (i.e. Marker A)

Unfortunately because MapClick is called in step 3, and FSelectedMarker is non-nil at this point, the OnMarkerClick event is being called for the marker that is about to be deselected. This seems to be an undesirable effect

@retwas
Copy link

retwas commented May 14, 2021

If you are interessed to see the effect I describe, you can download GaletPocket on the App Store, and write "Dijon" in the search bar. I can give you access to the source code too. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment