[Google Maps] Proposed Change - Get Map Center

Forge Component
Published on 2018-10-18 by Labs
27 votes
Published on 2018-10-18 by Labs

The following adds the ability to get the lat/long in the center of a map.  To make it work you need a hidden or unhidden text field that will receive the coordinates, as two comma separated values.

Modify the javascript of the Map object to include:

    this.getCenter = function(mapId,decimals) {
        var result;
        this.getMap(mapId).executeOnLoad(mapId, function(OSMap) {
            var gMap = OSMap.gMap;
            var center = gMap.getCenter();
            result = center.lat().toFixed(decimals) + ',' + center.lng().toFixed(decimals);
        return result;

Then add a JavaScriptHandler called "JS_MapCenter" that accepts MapId (text), DecimalPlaces (integer) and ControlId (text) which will be the widget's id.  It uses a RunJavaScript to execute:

"(function() {

    var MapId = "+Utils_WrapJSString(MapId)+";
    " + ControlID + ".value = osGoogleMap.getCenter(MapId," + DecimalPlaces + ");

Add a public logic item called "GetMapCenter" that accepts MapId, ControlId and decimal places, that calls the "JS_GetMapCenter".  Decimal Places defaults to 6, which gives a maximum coarse resolution of 4 1/2 inches for latitudes and 3 inches for longitudes at the equator.


Hi Ian,

thanks for sharing this with the community :)