1

I use GMLib to work with Google maps and now I have come to a point where I am very confused. I have the functions GetDistance and GetHeading to calculate the distance and compass direction between 2 markers on my map. When I call them from my procedure GetHeadingDistance I get the result I expect (distance and direction is correct)- aSearchCallInfo is a class containing info that needs to be updated with the values.

Now I am trying to add a function that lets the user press the right mouse button on the map and the get info about that location.

But in this case I get very wrong results. As far as I can see of the results it uses GMMarker.Items[1].Position as source even when I know that it is GMMarker.Items[0].Position I send as parameter.

When I try to debug the functions by writing values to a textfile during calculation, I can see that it is the correct values it gets to work with at the correct position.

(GMMarker.Items[0].Position is the position of the user of the software)

Any ideas as what I could try to get this solved?

procedure TfrmQthMap.GMMapRightClick(Sender: TObject; LatLng: TLatLng; X, Y: Double);
var
  MessageText: string;
  LL: TLatLng;
  Heading: double;
  Distance: double;
  Qra: string;
begin
  if GMMarker.Count > 0 then
    begin
      LL := TLatLng.Create;
      try
        LL.Lat    := LatLng.Lat;
        LL.Lng    := LatLng.Lng;
        Heading   := GetHeading(GMMarker.Items[0].Position, LL);
        Distance  := GetDistance(GMMarker.Items[0].Position, LL);
        Qra       := Maidenhead(LatLng.LngToStr, LatLng.LatToStr);
      finally
        FreeAndNil(LL);
      end;
      MessageText := 'Data for det sted du klikkede på: ' + sLineBreak + sLineBreak +
                     Format('Længdegrad: %s', [LatLng.LngToStr(Precision)]) + sLineBreak +
                     Format('Breddegrad: %s', [LatLng.LatToStr(Precision)]) + sLineBreak +
                     Format('Afstand: %6.1f km', [Distance]) + sLineBreak +
                     Format('Retning: %6.1f °', [Heading]) + sLineBreak +
                     Format('Lokator: %s', [Qra]);
      ShowMessage(MessageText);
    end;
end;

procedure TfrmQthMap.GetHeadingDistance(aSearchCallInfo: TCallInfo);
var
  Heading: double;
  Distance: double;
begin
  if GMMarker.Count > 1 then
    begin
      Heading   := GetHeading(GMMarker.Items[0].Position, GMMarker.Items[1].Position);
      Distance  := GetDistance(GMMarker.Items[0].Position, GMMarker.Items[1].Position);

      barFooter.Panels[PanelDist].Text  := Format('Afstand: %6.1f km', [Distance]);
      barFooter.Panels[PanelDir].Text   := Format('Retning: %6.1f°', [Heading]);

      aSearchCallInfo.Distance          := Format('%6.1f km', [Distance]);
      aSearchCallInfo.Heading           := Format('%6.1f °', [Heading]);
      aSearchCallInfo.SaveToDatabase;
    end;
end;
function TfrmQthMap.GetDistance(aOrigin, aDest: TLatLng): double;
var
  Distance: double;
begin
  Distance  := TGeometry.ComputeDistanceBetween(GMMap, aOrigin, aDest);
  Distance  := Distance / 1000;
  Result    := Distance;
end;

function TfrmQthMap.GetHeading(aOrigin, aDest: TLatLng): double;
var
  Heading: double;
begin
  Heading := TGeometry.ComputeHeading(GMMap, aOrigin, aDest);
  Heading := 180 + Heading;
  Result  := Heading;
end;
geocodezip
  • 158,664
  • 13
  • 220
  • 245
OZ8HP
  • 1,443
  • 4
  • 31
  • 61
  • I don't see any obvious error, the only uncertainty is the users click on the map might not be exactly the same as `GMMarker.Items[1].Position`. If you put a breakpoint on the first line of `GetHeading` (and `GetDistance`) are the `aOrigin` and `aDest` the same when called from `GetHeadingDistance` and from `GMMapRightClick`. What exactly are the values (edit into your question). If the `aOrigin` values differ, then your problem is in code you haven't shown. – Tom Brunberg Apr 23 '16 at 06:36
  • Sorry for not getting back sooner - I have found out after many hours of looking at the GMLib code that everything wasn't working like I thought it did. As long as there was 2 fixed markers everything looked OK but with the 'floating' marker I added it didn't What was wrong was that the markers wasn't given the indexnumbers I expected so instead of calculating from source to destination I actually calculated from destination to source and that gave the wrong results. But it is fixed now. – OZ8HP May 04 '16 at 09:13

0 Answers0