New "GetPolygonCenter" (Code Incl.)

Please consider the following new GetPolygonCenter JavaScript routine to be included in the WebGMaps product.

Assuming you have a hidden input named "result" this would do the trick:

In WebGMapsConst.pas

  ' function GetPolygonCenter(aPolygonId)'               + #13 +
  ' {'                                                   + #13 +
  '   var bounds = new google.maps.LatLngBounds(); '     + #13 +
  '   var i; '                                           + #13 +
  '   var polygonCoords = [];'                           + #13 +
  '     var polygonBounds = allpolygons[aPolygonId].getPath();'   + #13 +
  '     var xy;'                                         + #13 +
  '     for (var j = 0; j < polygonBounds.length; j++) ' + #13 +
  '     { '                                              + #13 +
  '       xy = polygonBounds.getAt(j);'                  + #13 +
  '       polygonCoords.push(new google.maps.LatLng(xy.lat(), xy.lng()));' + #13 +
  '     }'                                               + #13 +
  '  for (i = 0; i < polygonCoords.length; i++) { '      + #13 +
  '    bounds.extend(polygonCoords); '                + #13 +
  '  } '                                                 + #13 +
  ' '                                                    + #13 +
  // Save the result string into our hidden input (i.e. "result")
  '   document.getElementById("result").value = bounds.getCenter(); ' + #13 +
  ' }' + #13 +
  ' ' + #13 +

In WebGMaps.pas

function TWebGMaps.GetPolygonCenter(APolygonId: Integer;
                                    var ACenterLat: Double;
                                    var ACenterLong: Double): String;
{Returns the center as a string (i.e. "(44.1234567,-103.1234567)") but also
 returns the lat and long via ACenterLat and ACenterLong. }
var
  CenterStr: String;
  LatStr, LongStr: String;
begin
  Result := Trim( InvokeScript( Format( 'GetPolygonCenter(%d)', [ APolygonId ] ) ) );

  CenterStr := Result;

  if ( Trim(CenterStr) <> '' ) then
  begin
    LatStr := Copy( CenterStr, 2, Pos(',', CenterStr ) -2 );
    ACenterLat := StrToFloat( LatStr );

    Delete( CenterStr, 1, Pos(',', CenterStr ));

    LongStr := Copy( CenterStr, 1, Pos(')', CenterStr ) -1 );
    ACenterLong := StrToFloat( LongStr );
  end
  else
  begin
    ACenterLat := -1;
    ACenterLong := -1;
  end;
end;

Kind Regards,
Richard
Hi,

Thank you for your suggestion, we'll consider adding this functionality in a future version.