{"size": 1706, "ext": "js", "max_stars_repo_path": "map.js", "max_stars_repo_name": "rsommerard/map-electron", "max_stars_repo_stars_event_min_datetime": "2017-06-14T05:15:03.000Z", "max_stars_repo_stars_event_max_datetime": "2020-11-02T20:11:22.000Z", "max_issues_repo_path": "map.js", "max_issues_repo_name": "rsommerard/map-electron", "max_issues_repo_issues_event_min_datetime": "2019-06-14T03:21:08.000Z", "max_issues_repo_issues_event_max_datetime": "2019-06-14T04:01:15.000Z", "max_forks_repo_path": "map.js", "max_forks_repo_name": "rsommerard/map-electron", "max_forks_repo_forks_event_min_datetime": "2017-02-07T03:12:43.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-09T07:36:55.000Z"}
{"max_stars_count": 10, "max_issues_count": 1, "max_forks_count": 9, "avg_line_length": 20.5542168675, "max_line_length": 68, "alphanum_fraction": 0.5984759672}
var config = require('./config');

var URL = config.url;
var markers = [];
var _map;
var isMapInitialized = false;
var isIntervalSetted = false;

function computeCenter(positions) {
  if (positions.length == 0) {
    return 0;
  }

  var latitude = 0;
  var longitude = 0;

  for (var i = 0; i < positions.length; i++) {
    var position = positions[i];

    latitude += position.lat;
    longitude += position.lon;
  }

  latitude /= positions.length;
  longitude /= positions.length;

  return {lat: latitude, lng: longitude};
}

function initMap() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function () {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      var positions = JSON.parse(xhttp.responseText);
      var cntr = computeCenter(positions);

      if (isMapInitialized === false) {
        _map = new google.maps.Map(document.getElementById('map'), {
          zoom: 15,
          center: cntr
        });
        isMapInitialized = true;
      } else {
        _map.setCenter(cntr);
      }

      deleteMarkers();
      for (var i = 0; i < positions.length; i++) {
        var position = positions[i];
        addMarker(position);
      }
    }
  };
  xhttp.open('GET', URL + '/locations');
  xhttp.send();

  if (isIntervalSetted === false) {
    setInterval(initMap, 1000);
    isIntervalSetted = true;
  }
}

function clearMarkers() {
  for (var i = 0; i < markers.length; i++) {
    markers[i].setMap(null);
  }
}

function deleteMarkers() {
  clearMarkers();
  markers = [];
}

function addMarker(pos) {
  var marker = new google.maps.Marker({
    position: {lat: pos.lat, lng: pos.lon},
    map: _map
    //title: pos.id
  });

  markers.push(marker);
}
