Frequently Asked Component Specific Questions

Options

Display all FAQ items

Search FAQ items:


Displaying items 1 to 1 of 1, page 1 of 1

<< previous next >>

TMS iCL
Zooming to fit all annotations in MapView

Below is some code that zooms the map to view all annotations that have been added. Add the unit FMX.TMSNativeUICore in the uses list to have access to the MKCoordinateRegion record.
procedure TForm1.ZoomToFit;
var
  maxCoord: TTMSFMXNativeMKMapLocation;
  minCoord: TTMSFMXNativeMKMapLocation;
  coord: TTMSFMXNativeMKMapLocation;
  I: Integer;
  rgnm: MKCoordinateRegion;
begin
  maxCoord.Latitude := -90;
  maxCoord.Longitude := -180;
  minCoord.Latitude := 90;
  minCoord.Longitude := 180;

  for I := 0 to TMSFMXNativeMKMapView1.Annotations.Count - 1 do
  begin
    coord := TMSFMXNativeMKMapView1.Annotations[I].Location;
    if coord.Longitude > maxCoord.Longitude then
      maxCoord.Longitude := coord.Longitude;

    if coord.Latitude > maxCoord.Latitude then
      maxCoord.Latitude := coord.Latitude;

    if coord.Longitude < minCoord.Longitude then
      minCoord.Longitude := coord.Longitude;

    if coord.Latitude < minCoord.Latitude then
      minCoord.Latitude := coord.Latitude;
  end;

  rgnm.center.longitude := (minCoord.Longitude + maxCoord.Longitude) / 2;
  rgnm.center.latitude := (minCoord.Latitude + maxCoord.Latitude) / 2;
  rgnm.span.longitudeDelta := maxCoord.Longitude - minCoord.Longitude;
  rgnm.span.latitudeDelta := maxCoord.Latitude - minCoord.Latitude;

  TMSFMXNativeMKMapView1.MapView.setRegion(rgnm, True);
end;