Page 1 of 2

How to find pixel coordinates ?

PostPosted: 02 Apr 2019 11:22
by FredT
Hi,

I would like from a custom image loaded in a GMap3, find pixel coordinates of a Marker in the Marker OnDragEnd event.

For this I use the method described here:
https://developers.google.com/maps/documentation/javascript/examples/map-coordinates
But I do not get the right pixel coordinates!
Have I forgotten something ?
Or is there another method ?

Here is my code:
Code: Select all
procedure TIWForm1.IWAppFormCreate(Sender: TObject);
var
    L: TstringList;
    LMarker: TIWCGJQGMap3MultipleMarker;
begin
    IWCGGoogleLibLoader.GoogleApiKey:= '...';
    IWCGJQGMap3.JQGMapV3Options.ImageMapType.Id := 'test';
    IWCGJQGMap3.JQGMapV3Options.ImageMapType.Options.MaxZoom := 7;
    IWCGJQGMap3.JQGMapV3Options.ImageMapType.Options.MinZoom := 3;
    IWCGJQGMap3.JQGMapV3Options.ImageMapType.Options.Name := 'test';
    IWCGJQGMap3.JQGMapV3Options.ImageMapType.Options.TileSize.Width := cTILE_SIZE;
    IWCGJQGMap3.JQGMapV3Options.ImageMapType.Options.TileSize.Height := cTILE_SIZE;
    IWCGJQGMap3.JQGMapV3Options.Map.Options.MapTypeCustomId := 'test';

    L := TStringList.Create;
    L.LoadFromFile(WebApplication.ApplicationPath + 'myMap.js');
    IWCGJQGMap3.JQGMapV3Options.ImageMapType.Options.GetTileUrl.Script := L.Text;
    L.Free;

    // Markers
    LMarker:= IWCGJQGMap3.JQGMapV3Options.Marker.Values.Add;
    LMarker.Data:= 'Marker 0';
    LMarker.Id := '0';
    LMarker.LatLng.Latitude:= 20;
    LMarker.LatLng.Longitude:= -10;
    LMarker.Options.Draggable := True;

    IWCGJQGMap3.JQGMapV3Options.Marker.Events.OnDragEnd.OnEvent:= GetCoordMarkerDragEndEvent;
end;

procedure TIWForm1.GetCoordMarkerDragEndEvent(Sender: TObject; AParams: TStringList);
var
    lat: Double;
    long: Double;
    myFormatSetting: TFormatSettings;
    LatLng: TIWCGJQGMap3LatLng;
    WorldCoord, PixelCoord: TPoint;
    iZoomLvl: Integer;
    x, y: Double;
begin
    myFormatSetting.DecimalSeparator:= '.';
    lat:= StrToFloat(AParams.Values['markerLat'],myFormatSetting);
    long:= StrToFloat(AParams.Values['markerLng'],myFormatSetting);

    LatLng := IWCGJQGMap3.JQGMapV3Options.Map.Options.Center;
    LatLng.Latitude := lat;
    LatLng.Longitude := long;

    WorldCoord := Project(LatLng);

    iZoomLvl := IWCGJQGMap3.JQGMapV3Options.Map.Options.Zoom;
    x := Floor(WorldCoord.X * Power(2, iZoomLvl));
    y := Floor(WorldCoord.Y * Power(2, iZoomLvl));

    PixelCoord := Point(Trunc(x),Trunc(y)); // Bad Coord !

    // ...
end;

function TIWForm1.Project(ALatLng: TIWCGJQGMap3LatLng): TPoint;
var
    siny: Double;
    x, y: Double;
begin
    siny := Sin(ALatLng.Latitude * PI / 180);
    siny := Min(Max(siny, -0.9999), 0.9999);

    x := cTILE_SIZE * (0.5 + ALatLng.Longitude / 360);
    y := cTILE_SIZE * (0.5 - System.ln((1 + siny) / (1 - siny)) / (4 * PI));

    Result := Point(Trunc(x),Trunc(y));
end;


Thank.

Re: How to find pixel coordinates ?

PostPosted: 03 Apr 2019 12:49
by Alexander Bulei
Hi FredT,

I think the problem is in zoom, i guess its not updated...you need pass the current zoom of map into your drag function.

Debug to verify that.

Best Regards.

Re: How to find pixel coordinates ?

PostPosted: 04 Apr 2019 10:48
by FredT
Hi,

The zoom value I get at the OnDragEnd is updated, I get values ​​from 3 to 7, which corresponds to the min and max zoom.

I wonder if the problem does not come from the centering of the map: when I created a marker with latitude 0 and longitude 0, it should I think to be in the center of the map but it does not appear.

I tried this but it does not change anything:
GMap3.JQGMapV3Options.Map.Options.Center.Latitude:=0;
GMap3.JQGMapV3Options.Map.Options.Center.Longitude:=0;

Thank

Re: How to find pixel coordinates ?

PostPosted: 05 Apr 2019 16:00
by Alexander Bulei
Hi FredT,

Please send me the simple testcase project.
TIA

Best Regards.

Re: How to find pixel coordinates ?

PostPosted: 08 Apr 2019 14:28
by FredT
Hi,

I send you the link to download the test project compressed with 7-zip (13Mo) to support@cgdevtools.com with object: "How to find pixel coordinates ?" because i can't put it in attachment.
To test, drag and drop a marker, I calculate the pixel coordinates in the function GetCoordMarkerDragEndEvent.

Thank.

Re: How to find pixel coordinates ?

PostPosted: 08 Apr 2019 15:49
by Alexander Bulei
Hi FredT,

Since it's not related to our comps, I will charge the premium support ticket.
Let me know, if you agree with that.

Best Regards.

Re: How to find pixel coordinates ?

PostPosted: 09 Apr 2019 08:48
by FredT
Hi Alexander,

Yes, we agree to open a ticket.

Thank.

Re: How to find pixel coordinates ?

PostPosted: 09 Apr 2019 11:49
by Alexander Bulei
Hi FredT,
Please check the changed files.

Best Regards.

Re: How to find pixel coordinates ?

PostPosted: 09 Apr 2019 13:28
by FredT
Hi Alexander,

Indeed it is always better to use the right types, however the values ​​returned are identical.

I will detail a little more what I wish to do:
In zoom 3 my image is composed of 2 * 2 tiles, the size of a tile is 208 * 208 pixels, so my image is 416 * 416 pixels.
When I drag'n drop the marker at the top left edge of the image I get the pixel coordinates (506, 506) and when I drag'n drop it to the top right edge I get (843, 506).

With these coordinates I get a width of 843 - 506 = 337 or the image is a width of 416, so it's wrong.

Ideally I would like to get for the top left edge (0, 0) and for the top right edge (416, 0).

Thank.

Re: How to find pixel coordinates ?

PostPosted: 09 Apr 2019 16:17
by Alexander Bulei
Hi FredT,

As you can see, the pixel coordinates now are same as google...
If it doesnt work for you, you need find another calcs for your needs.

Best Regards.