TradeAreaService.js

TradeAreaService()


constructor

The TradeAreaService object is responsible for computing the area around a location for a given time or distance.

Parameter Type Description
customer String The customer name.
key String The authorization key necessary for using the [TradeAreaService].

computeTradeArea()


method

Computes the trade area.

Options:
  • travelMode(String)The travel mode. Accepted values: vehicle, pedestrian. Default is vehicle.
  • computationType(String)The computation type. Indicates if it takes into consideration the time or the distance. Accepted values: time, distance. Default is 'distance'.
  • precision(Number)The precision of the output shape. 0.5 is the best precision, and 1.0 is the lowest, but the fastest.
Parameter Type Description
center LatLng The center of the trade area.
value Number The value of the distance/time unit. (meters if computationType option is distance and minutes if computationType option is time)
options Object The options to set for computing the trade area.
returns
TradeAreaServiceResult - The TradeAreaService results.

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;}
}