{"size": 7175, "ext": "js", "max_stars_repo_path": "docs/js/app.js", "max_stars_repo_name": "codeformatsudo/MatsudoHeatmap", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "docs/js/app.js", "max_issues_repo_name": "codeformatsudo/MatsudoHeatmap", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "docs/js/app.js", "max_forks_repo_name": "codeformatsudo/MatsudoHeatmap", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 34.0047393365, "max_line_length": 260, "alphanum_fraction": 0.5880139373}
Date.prototype.format = function (mask, utc) {
    return dateFormat(this, mask, utc);
};

// Attibution: SODA API requests based on this example: https://github.com/chriswhong/soda-leaflet

L.TimeDimension.Layer.SODAHeatMap = L.TimeDimension.Layer.extend({

    initialize: function(options) {
        var heatmapCfg = {
            radius: 50,
            maxOpacity: .8,
            scaleRadius: false,
            useLocalExtrema: false,
            latField: 'lat',
            lngField: 'lng',
            valueField: 'count'
        };
        heatmapCfg = $.extend({}, heatmapCfg, options.heatmatOptions || {});
        var layer = new HeatmapOverlay(heatmapCfg);
        L.TimeDimension.Layer.prototype.initialize.call(this, layer, options);
        this._currentLoadedTime = 0;
        this._currentTimeData = {
            max: this.options.heatmapMax || 10,
            data: []
        };
        this._baseURL = this.options.baseURL || null;
        this._period = this.options.period || "P1D";
    },

    onAdd: function(map) {
        L.TimeDimension.Layer.prototype.onAdd.call(this, map);
        map.addLayer(this._baseLayer);
        if (this._timeDimension) {
            this._getDataForTime(this._timeDimension.getCurrentTime());
        }
    },

    _onNewTimeLoading: function(ev) {
        this._getDataForTime(ev.time);
        return;
    },

    isReady: function(time) {
        return (this._currentLoadedTime == time);
    },

    _update: function() {
        this._baseLayer.setData(this._currentTimeData);
        return true;
    },

    _getDataForTime: function(time) {
        if (!this._baseURL || !this._map) {
            return;
        }
        var url = this._constructQuery(time);
        var fileName = this._getFileName(time);
        //console.log(fileName);
        
        $.getJSON(fileName, (function(data) {
            //console.log(data);
            delete this._currentTimeData.data;
            this._currentTimeData.data = [];
            for (var i = 0; i < data.length; i++) {
                var marker = data[i];
                if (marker.location) {
                    this._currentTimeData.data.push({
                        lat: marker.location.latitude,
                        lng: marker.location.longitude,
                        count: 1
                    });
                }
            }
            this._currentLoadedTime = time;
            if (this._timeDimension && time == this._timeDimension.getCurrentTime() && !this._timeDimension.isLoading()) {
                this._update();
            }
            this.fire('timeload', {
                time: time
            });
        }).bind(this));
    },

    _constructQuery: function(time) {
        var bbox = this._map.getBounds();
        var sodaQueryBox = [bbox._northEast.lat, bbox._southWest.lng, bbox._southWest.lat, bbox._northEast.lng];

        var startDate = new Date(time);
        var endDate = new Date(startDate.getTime());
        L.TimeDimension.Util.addTimeDuration(endDate, this._period, false);

        var where = "&$where=created_date > '" +
            startDate.format('yyyy-mm-dd') +
            "' AND created_date < '" +
            endDate.format('yyyy-mm-dd') +
            "' AND within_box(location," +
            sodaQueryBox +
            ")&$order=created_date desc";

        var url = this._baseURL + where;
        return url;
    },

    _getFileName: function(time) {
        var startDate = new Date(time);
        var endDate = new Date(startDate.getTime());
        L.TimeDimension.Util.addTimeDuration(endDate, this._period, false);
        //return 'data/Hornet_' + startDate.format('yyyy_mm') + '.json';
        console.log('data/kiken_Hour_' + startDate.format('dd') + '.json');
        return 'data/kiken_Hour_' + startDate.format('dd') + '.json';
    }

});

L.timeDimension.layer.sodaHeatMap = function(options) {
    return new L.TimeDimension.Layer.SODAHeatMap(options);
};

var currentTime = new Date();
currentTime.setUTCDate(1, 0, 0, 0, 0);

// スマホ判定
var _ua = (function(u){
  return {
    Tablet:(u.indexOf("windows") != -1 && u.indexOf("touch") != -1 && u.indexOf("tablet pc") == -1) 
      || u.indexOf("ipad") != -1
      || (u.indexOf("android") != -1 && u.indexOf("mobile") == -1)
      || (u.indexOf("firefox") != -1 && u.indexOf("tablet") != -1)
      || u.indexOf("kindle") != -1
      || u.indexOf("silk") != -1
      || u.indexOf("playbook") != -1,
    Mobile:(u.indexOf("windows") != -1 && u.indexOf("phone") != -1)
      || u.indexOf("iphone") != -1
      || u.indexOf("ipod") != -1
      || (u.indexOf("android") != -1 && u.indexOf("mobile") != -1)
      || (u.indexOf("firefox") != -1 && u.indexOf("mobile") != -1)
      || u.indexOf("blackberry") != -1
  }
})(window.navigator.userAgent.toLowerCase());

// 最大化ボタンは表示（スマホの場合は非表示）
var displayFullScreenCntl = true;
// 中心座標は21世紀公園の西あたり（スマホの場合は上本郷駅あたり）
var cnterlat = 35.804602;
var cnterlng = 139.929651;
if(_ua.Mobile){
    displayFullScreenCntl = false;
    cnterlat = 35.789481;
    cnterlng = 139.9138103;
}

var map = L.map('map', {
    zoom: 11.5,
    fullscreenControl: displayFullScreenCntl,
    //dragging: false, // マウスドラッグによるパン操作を不可
    //touchZoom: false, // タッチによるズーム操作を不可
    //scrollWheelZoom: false, // スクロールによるズーム操作を不可
    //doubleClickZoom: false, // ダブルクリックによるズーム操作を不可
    //boxZoom: false, // [Shift] + ドラッグによるボックスズーム操作を不可
    //tap: false, // タップによるズーム操作を不可
    //keyboard: false, // キーボードによる操作を不可
    //zoomControl: false, // ズーム コントロールの非表示
    timeDimension: true,    
    timeDimensionOptions: {
        //timeInterval: "2016-04-01/" + currentTime.toISOString(), 
        timeInterval: "2016-01-04/2016-01-22", 
        period: "P1D"
        //currentTime: currentTime
    },
    center: [cnterlat, cnterlng], // 松戸中心座標
});

//var layer = new L.StamenTileLayer("toner-lite");
var layer = new L.tileLayer(
	'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
	{
		attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a>',
	}
);
map.addLayer(layer);

var testSODALayer = L.timeDimension.layer.sodaHeatMap({
    baseURL: 'https://data.cityofnewyork.us/resource/erm2-nwe9.json?$select=location,closed_date,complaint_type,street_name,created_date,status,unique_key,agency_name,due_date,descriptor,location_type,agency,incident_address&complaint_type=Noise - Commercial',
});
testSODALayer.addTo(map);
//map.attributionControl.addAttribution('<a href="https://nycopendata.socrata.com/Social-Services/311-Service-Requests-from-2010-to-Present/erm2-nwe9">NYC OpenData</a>');

L.Control.TimeDimensionCustom = L.Control.TimeDimension.extend({
    _getDisplayDateFormat: function(date){
        //console.log(date);
        var hh = date.getDate();
        var ampm = "午前";
        if(hh >= 13)
        {
            ampm = "午後";
            hh = hh - 12;
        }
        //return date.format("dd mmmm yyyy");
        return "発生時間帯：" + ampm + hh + "時";
    }    
});
var timeDimensionControl = new L.Control.TimeDimensionCustom({
    playerOptions: {
        buffer: 1,
        minBufferReady: -1,
	startOver: true
    }
});
map.addControl(this.timeDimensionControl);
