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.