Announcement

Collapse
No announcement yet.

garmin sample part 2

Collapse
X
  • Time
  • Show
Clear All
new posts

  • garmin sample part 2

    //Garmin.DeviceControl call-back methods:

    onFinishFindDevices: function (json) {
    alert("one");
    this.findDevicesButton.disabled = false;
    this.cancelFindDevicesButton.disabled = true;

    if (json.controller.numDevices > 0) {
    var devices = json.controller.getDevices();
    this._setStatus("Found " + devices.length + " devices.");

    this._listDevices(devices);

    this.cancelReadDataButton.onclick = function () {
    this.readDataButton.disabled = false;
    this.cancelReadDataButton.disabled = true;
    this._hideProgressBar();

    this.garminController.cancelReadFromDevice();
    }.bind(this)

    this.readDataButton.disabled = false;
    this.readDataButton.onclick = function () {
    this.readDataButton.disabled = true;
    this.cancelReadDataButton.disabled = false;
    this._showProgressBar();

    this.garminController.readFromDevice();
    }.bind(this)

    } else {
    this._setStatus("No devices found.");
    }
    },

    onStartFindDevices: function (json) {
    this._setStatus("Looking for connected Garmin devices");
    },

    onCancelFindDevices: function (json) {
    this._setStatus("Find cancelled");
    },

    onProgressReadFromDevice: function (json) {

    this._updateProgressBar(json.progress.getPercentage());
    this._setStatus(json.progress);
    },

    onCancelReadFromDevice: function (json) {
    this._setStatus("Read cancelled");
    },

    onFinishReadFromDevice: function (json) {
    this.readDataButton.disabled = false;
    this.cancelReadDataButton.disabled = true;
    this._hideProgressBar();
    this._setStatus("Data read from device.");

    var gpsData = json.controller.gpsData;

    this._setStatus("Parsing track data...");
    var factory = new Garmin.GpsDataFactory();
    factory.parseGpxDocument(gpsData);
    this.tracks = factory.getTracks();
    this.waypoints = factory.getWaypoints();
    this._setStatus(this.tracks.length + " tracks and " + this.waypoints.length + " waypoints found");

    this._listTracks(this.tracks);
    this._listWayPoints(this.waypoints);
    },


    _intializeController: function () {
    try {
    this.garminController = new Garmin.DeviceControl();
    this.garminController.unlock("http://mydomain.com", "pasteYourKeyInHere");
    this.garminController.register(this);
    } catch (e) {
    this._setStatus(e.message);
    }
    },

    _showProgressBar: function () {
    Element.show(this.progressBar);
    },

    _hideProgressBar: function () {
    Element.hide(this.progressBar);
    },

    _updateProgressBar: function (value) {
    var percent = (value <= 100) ? value : 100;

    this.progressBarDisplay.style.width = percent + "%";
    },

    _listDevices: function (devices) {
    for (var i = 0; i < devices.length; i++) {
    this.deviceSelect.options[i] = new Option(devices[i].getDisplayName(), devices[i].getNumber());
    if (devices[i].getNumber() == this.garminController.deviceNumber) {
    this.deviceSelect.selectedIndex = i;
    this._showDeviceInfo(devices[i]);
    }
    }
    this.deviceSelect.onchange = function () {
    var device = this.garminController.getDevices()[this.deviceSelect.value];
    this.showDeviceInfo(device);
    this.garminController.setDeviceNumber(this.deviceSelect.value);
    }.bind(this)
    this.deviceSelect.disabled = false;
    },

    _showDeviceInfo: function (device) {
    this.deviceInfo.innerHTML = "Part Number:\t\t" + device.getPartNumber() + "\n";
    this.deviceInfo.innerHTML += "Software Version:\t" + device.getSoftwareVersion() + "\n";
    this.deviceInfo.innerHTML += "Description:\t\t" + device.getDescription() + "\n";
    this.deviceInfo.innerHTML += "Id:\t\t\t" + device.getId();
    },

    _listTracks: function (tracks) {
    this._clearTracks();
    for (var i = 0; i < tracks.length; i++) {
    var trk = tracks[i];
    if (trk.isDrawable()) {
    this.readTracksSelect.options[i] = new Option(trk.getStartDate() + " - (" + trk.getDuration() + ")", i);
    }
    }
    this._displayTrack(tracks[this.readTracksSelect.selectedIndex]);
    this.readTracksSelect.onchange = function () {
    var trk = this.tracks[this.readTracksSelect.selectedIndex];
    this._displayTrack(trk);
    }.bind(this)
    this.readTracksSelect.disabled = false;
    },

    _listWayPoints: function (waypoints) {
    this._clearWayPoints();
    for (var i = 0; i < waypoints.length; i++) {
    var wpt = waypoints[i];
    this.readWaypointsSelect.options[i] = new Option(wpt.getName(), i);
    }
    this.readWaypointsSelect.onchange = function () {
    var wpt = this.waypoints[this.readWaypointsSelect.selectedIndex];
    this._displayWayPoint(wpt);
    }.bind(this)
    this.readWaypointsSelect.disabled = false;
    },

    _clearTracks: function () {
    for (var i = 0; i < this.readTracksSelect.options.length; i++) {
    this.readTracksSelect.options[i] = null;
    }
    },

    _clearWayPoints: function () {
    for (var i = 0; i < this.readWaypointsSelect.options.length; i++) {
    this.readWaypointsSelect.options[i] = null;
    }
    },

    _displayTrack: function (track) {
    this.mc.map.clearOverlays();
    this.mc.centerAndScale(track.getStartLat(), track.getStartLng());
    this.mc.addTrack(track);
    },

    _displayWayPoint: function (wpt) {
    this.mc.map.clearOverlays();
    this.mc.centerAndScale(wpt.getLat(), wpt.getLng());
    this.mc.addWayPoint(wpt);
    },

    _setStatus: function (statusText) {
    this.status.innerHTML = statusText;
    }
    };

    //display is created when HTML page is loaded
    var display;

    function load() {
    display = new GarminDeviceControlDemo("statusText", "readMap");
    }
    </script>


    </body>
    </html>[/HTML]
Working...
X