TradeAreaRenderer.js

TradeAreaRenderer()


constructor

The TradeAreaRenderer object is responsible for displaying the computed trade area on the map.

Parameter Type Description
map Map The map.

displayTradeArea()


method

Displays the computed trade area on the map.

Options:
  • centerMarker(Boolean)Specifies if a marker should be added in the center of the trade area.
  • centerMarkerPath(String)The URL to the image used for the trade area center point.
  • centerMarkerOffset(Point)The offset of the trade area center point.
  • zoomToTradeArea(Boolean)Specifies if the map will be zoomed to the tradea area bounds.
  • stroke(Boolean)Specifies if the tradea area shape will have a stroke or not. Default is true.
  • strokeColor(String)The stroke color. The color hex code should be provided. Default is 428BCA.
  • strokeOpacity(Number)The stroke opacity. Default is 1.
  • strokeWeight(Number)The stroke weight. Default is 2.
  • fill(String)Specifies if the tradea area shape will be filled with a color or not. Default is true.
  • fillColor(Number)The fill color. The color hex code should be provided. Default is 428BCA.
  • fillOpacity(Number)The fill opacity. Default is 1.
Parameter Type Description
computedTradeArea Object The computed tradea area returned by the {TradeAreaService}.
options Object The options to set for displaying the trade area.

Example

$(document).ready(function(){

    var mapContainer = T.DomUtil.get("map"),
        mapoLayer = new T.TibcoLayer(),
        vectorLayer = new T.VectorLayer({useCanvas: false});

    var map = new T.Map(
        mapContainer,					
        {
            zoom: 5, 
            center: new T.LatLng(34.0198672, -97.7998047)
        }
    );								

    map.addLayer(mapoLayer);
    map.addLayer(vectorLayer);

    // Add map event listeners
    map.events.on("press", onPress, this);

    // Distance computation
    this.tradeAreaService = new T.TradeAreaService("demo", "2u7kdn4DnYE=", "https://geowebservices.maporama.com/");

    // Init trade area renderer
    var tradeAreaRenderer = new T.TradeAreaRenderer(map);

    var computeTradeAreaHandler = function () {
        var centerStringNoSpaces = $("#center").val().replace(" ", "");

        var center = new T.LatLng(centerStringNoSpaces.split(',')[0], centerStringNoSpaces.split(',')[1]),
            value = $("#computation-value").val();

        // Add overlay
        showHideOverlay(true);

        // Save options
        var tradeAreaOptions = {
            travelMode: $("#travel-mode").val(),
            computationType: $("#computation-type").val(),
            precision: $("#precision").val()
        };

        // Compute distance
        this.tradeAreaService.computeTradeArea(center, value, tradeAreaOptions).then(function (tradeAreaServiceResults) {

            if (tradeAreaServiceResults) {
                // Remove itinerary
                tradeAreaRenderer.removeTradeArea();

                tradeAreaRenderer.displayTradeArea(tradeAreaServiceResults, {
                    centerMarker: true,
                    zoomToTradeArea: true,
                    stroke: true,
                    strokeColor: "#FFF000",
                    strokeOpacity: 0.9,
                    strokeWeight: 2,
                    fill: true,
                    fillColor: "#FF0000",
                    fillOpacity: 0.5
                });

                showHideOverlay(false);
            }

        }, function (tradeAreaServiceError) {

        });
    };

    function onPress (evt) {
        var pressEffectTimer = null,
            positionX = (evt.clientX - $(".press-effect").width() / 2 >= 0) ? (evt.clientX - $(".press-effect").width() / 2) : 0,
            positionY = (evt.clientY - $(".press-effect").height() / 2 >= 0) ? (evt.clientY - $(".press-effect").height() / 2) : 0,
            mousePosition = map.computeMousePosition(evt.clientX, evt.clientY),
            latLng = map.containerPointToLatLng(mousePosition);

        clearTimeout(pressEffectTimer);

        $(".press-effect").css("top", positionY);
        $(".press-effect").css("left", positionX);
        $(".press-effect").show();

        pressEffectTimer = setTimeout(function () {
            $(".press-effect").hide();
        }, 500);

        $("#center").val(latLng.lat + "," + latLng.lng);
    };

    var zoomToTradeArea = function () {
        var i = 0,
            bounds = new T.LatLngBounds(); 

        for (i = 0; i < markersLayer.markers.length; i++) {
            bounds.extend(markersLayer.markers[i].latlng);
        }

        map.fitToBounds(bounds);
    };

    var showHideOverlay = function (show) {

        if (show) {
            var docHeight = $(document).height();

            $("body").append("<div id='overlay'></div>");

            $("#overlay")
                .height(docHeight)
                .css({
                'opacity' : 0.4,
                'position': 'absolute',
                'top': 0,
                'left': 0,
                'background-color': 'black',
                'width': '100%',
                'z-index': 5000
            });
        } else {
            $("#overlay").remove();
        }
    };

    // Event listeners
    $("#compute-trade-area-btn").click(T.Util.bind(computeTradeAreaHandler, this));
});
<html>    
    <head>
        <title>TibcoMaps - Trade Area Service</title>
        <meta charset="UTF-8">

        <!-- CSS -->
        <link rel="stylesheet", href="trade-area-service.css"/>

        <!-- Javascript -->
        <script type="text/javascript" src='../../lib/GeoAnalytics.js'></script>
        <script type="text/javascript" src='../../lib/jquery-1.9.1.min.js'></script>
        <script type="text/javascript" src="trade-area-service.js"></script>
    </head>

    <body>
        <div class="container">
            <div id="form">
                <div class="form-group">
                    <div class="title">Travel mode:</div>
                    <select id="travel-mode">
                        <option value="vehicle">Vehicle</option>
                        <option value="pedestrian">Pedestrian</option>
                    </select>
                </div>
                <div class="form-group">
                    <div class="title">Computation type:</div>
                    <select id="computation-type">
                        <option value="distance">Distance</option>
                        <option value="time">Time</option>
                    </select>
                </div>
                <div class="form-group">
                    <div class="title">Precision:</div>
                    <div class="inputs">
                        <input id="precision" type="text" placeholder="Precision" value="0.8"/>
                    </div>
                </div>
                <div class="form-group">
                    <div class="title">Computation value:</div>
                    <div class="inputs">
                        <input id="computation-value" type="text" placeholder="Meters/Minutes" value="2000"/>
                    </div>
                </div>
                <div class="form-group">
                    <div class="title">Trade Area center:</div>
                    <div class="inputs">
                        <input id="center" type="text" placeholder="Click on the map to set the center" value=""/>
                    </div>
                </div>

                <div id="btn-group">
                    <button id="compute-trade-area-btn">Compute Trade Area</button>
                </div>
            </div>
            <div id="map"></div>
            <div class="press-effect"></div>
        </div>
    </body>
</html>
body, html { 
    height: 100%;
    width: 100%;
    margin: 0px;
}

.container  {
    display: flex;
    height: 100%;
    padding: 0 0 0 15px;
}

.container #form {
    width: 400px;
    height: 100%;
    padding-right: 15px;
    overflow: auto;
}

.container #form .options {
    border-bottom: 1px solid black;
    padding-bottom: 10px;
}

.container #form .form-group {
    display: flex;
    margin-top: 10px;
}

.container #form .form-group .title {
    width: 150px;
    font-weight: bold;
    margin-bottom: 5px;
}

.container #form .form-group .inputs > input {
    margin-bottom: 5px;
    padding: 5px;
    width: 220px;
}

.container #map {			
    flex: 1;
}

.container #btn-group {
    margin: 20px 0;
}

.press-effect {
    border: 3px solid #f00;
    -webkit-border-radius: 30px;
    height: 18px;
    width: 18px;
    position: absolute;
    -webkit-animation: pulsate 0.5s ease-out;
    -webkit-animation-iteration-count: infinite; 
    opacity: 0.0;
    display: none;
}

@-webkit-keyframes pulsate {
    0% {-webkit-transform: scale(0.1, 0.1); opacity: 0.0;}
    50% {opacity: 1.0;}
    100% {-webkit-transform: scale(1.2, 1.2); opacity: 0.0;}
}

removeTradeArea()


method

Removes the tradea area from the map.