{"size": 1232, "ext": "js", "max_stars_repo_path": "static/location-tracking/receiver.js", "max_stars_repo_name": "OAlm/pb2", "max_stars_repo_stars_event_min_datetime": "2018-02-13T09:55:19.000Z", "max_stars_repo_stars_event_max_datetime": "2018-03-05T17:20:42.000Z", "max_issues_repo_path": "static/location-tracking/receiver.js", "max_issues_repo_name": "OAlm/pb2", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "static/location-tracking/receiver.js", "max_forks_repo_name": "OAlm/pb2", "max_forks_repo_forks_event_min_datetime": "2017-02-27T07:55:49.000Z", "max_forks_repo_forks_event_max_datetime": "2018-02-12T12:46:44.000Z"}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 25.1428571429, "max_line_length": 95, "alphanum_fraction": 0.6168831169}
'use strict';
/* global $ PB2 document L */
const pb2 = new PB2(window.location.hostname, 'location-tracking-demo');
pb2.setReceiver(onMessage);

let map;
let marker;

let users = {};
function updateMarker(userid, username, lat, lon) {
    console.log('add marker '+lat+' '+lon);
    console.log(map);

    if(userid in users) {
        map.removeLayer(users[userid]);
    }
    marker = new L.marker([lat, lon]);
    marker.addTo(map)
        .bindPopup(username+' <br/>Latitude: ' + lat + '<br/> Longtitude: ' + lon).
        openPopup();
    users[userid] = marker;
}

function onMessage(data) {
    // const c = pos.coords;
    console.log(data);
    updateMarker(data.socketid, data.json.user, data.json.lat, data.json.lon);
}

function error(err) {
  console.warn('ERROR(' + err.code + '): ' + err.message);
}


function initMap() {
    map = new L.Map('map');
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
    map.attributionControl.setPrefix('');

    // map view before we get the location
    map.setView(new L.LatLng(51.505, -0.09), 13);
}

$(document).ready(function() {
    initMap();
});
