1

I have a TMapView on my Form. The TMapView works fine so far but I want to add the possibility to draw a Line between two coordinates on the TMapView. How can I do that?

I tried the following which does not work in any way:

var
A,B: TMapCoordinate;
AB: TMapPolylineDescriptor;
begin
A.Latitude:=51.88;
A.Longitude:=10.56;
B.Latitude:=51.9;
B.Longitude:=10.7;
//MapView1.Canvas.DrawLine(A,B,50); //doesnt work
// AB.Points:=[A,B]; //doesnt work too
AB.StrokeWidth:=50;
//MapView1.AddPolyline(AB); //doesnt work too
end;

I guess the canvas thing wont work as I want as I think it just connects points on the TMapView as a Bitmap and not on the map itself. Please suggest me how I can draw a Line between two coordinates on the map (if that is possible)

I use Delphi 10.3.3 Community Edition and try to compile it on my android 10 phone.

I found this SO Link where the Polyline is used in Objective-C for MapKit on iPhone. I guess I have to do the same thing in delphi, but I have no idea how to do this.. but I saw Polyline draws a maps route between two points. Thats good to know too, but I just want my Line drawn and no maps route suggestion from Google.

if I use this code:

  if MapView1.Canvas.BeginScene then
  begin
    Canvas.Fill.Color := $FF111111 + random($FFFFFF);
    Canvas.Fill.Kind := TBrushKind.Solid;
    Canvas.FillRect(RectF(random(300), random(300), random(300), random(300)),
    0,
    0, [], 1);
    Canvas.EndScene;
  end;

it works and draws a colored Rectangle somwhere in some color, but is disappears immediately after showing up.. maybe thats why my Lines arent visible too. Maybe they're painted but overpainted immediately. I Dont know much about canvas of my TMapView or Canvas in general, but after painting the "square" it dissappears and I guess those Lines are just disappearing too. Can I somehow make those selfpainted canvas changes permanent until I overpaint them by myself?

qGold
  • 341
  • 2
  • 14
  • You probably have to convert from geographic coordinates (latitude, longitude) to canvas (bitmap) coordinates. Try drawing a line from top left corner (0, 0) to bottom line corner (with, height) to see if the line is visible. – fpiette Dec 26 '20 at 16:29
  • I tried painting canvas but the code I used didn't paint anything:```delphi var A,B: TPoint; begin A.x:=12; A.y:=12; B.x:=strtoint(MapView1.Width.ToString); B.y:=strtoint(MapView1.Height.ToString); MapView1.Canvas.DrawLine(A,B,50); //doesnt work end; ``` So I didn't even manage to draw any line – qGold Dec 26 '20 at 16:50
  • ```delphi with MapView1.Canvas do begin BeginUpdate; Stroke.Color := TAlphaColors.Black; Stroke.Thickness := 2.0; Stroke.Kind := TBrushKind.Solid; DrawLine(PointF(10, 10), PointF(100, 10), 1); EndUpdate; end; ``` this doesnt work too on TMapView – qGold Dec 26 '20 at 17:04
  • This doesn't seem to have anything to do with [google-maps] – geocodezip Dec 26 '20 at 17:14
  • @geocodezip TMapView uses Google Maps but yes, I see the point. the tag is removed – qGold Dec 26 '20 at 17:18

1 Answers1

2

try to use like that

var
A,B: TMapCoordinate;
AB: TMapPolylineDescriptor;
begin
  A := TMapCoordinate.Create(51.88,10.56);
  B := TMapCoordinate.Create(51.9,10.7);

  AB := TMapPolylineDescriptor.Create([A,B]);
  AB.StrokeWidth:=5;
  MapView1.AddPolyline(AB);
end;
Moskwa
  • 121
  • 5