views:

49

answers:

0

what's wrong to my code..where my return value found undefind...

var so;
var imgid_callback1;
const DIV_ID = 'locationsample';

function setup() {
    try {
        so = device.getServiceObject("Service.Location", "ILocation");
    }
    catch (e) {
        alert('<setup> ' + e);
    }
}


function getLocation(imgId) {
    var updateoptions = new Object();
    // Setting PartialUpdates to 'FALSE' ensures that user get atleast
    // BasicLocationInformation (Longitude, Lattitude, and Altitude.)
    updateoptions.PartialUpdates = false;

    var criteria = new Object();
    criteria.LocationInformationClass = "BasicLocationInformation";
    criteria.Updateoptions = updateoptions;

    try {
        var result = so.ILocation.GetLocation(criteria);
        if (!checkError("ILocation::getLocation", result, DIV_ID, imgId)) {
            document.getElementById(DIV_ID).innerHTML = showObject(result.ReturnValue);
        }
    }
    catch (e) {
        alert("getLocation: " + e);
    }
}

function getLocationAsync(imgId) {
    var updateoptions = new Object();
    updateoptions.PartialUpdates = false;
    var criteria = new Object();
    criteria.LocationInformationClass = "BasicLocationInformation";
    criteria.Updateoptions = updateoptions;
    imgid_callback1 = imgId;

    try {
        var result = so.ILocation.GetLocation(criteria, callback1);
        if (!checkError("ILocation::getLocationAsync", result, DIV_ID, imgId)) {
            showIMG(imgId, "");
        }
    }
    catch (e) {

        alert("getLocationAsync: " + e);
    }
}

function callback1(transId, eventCode, result) {
    var latitude = result.ReturnValue.Latitude; //<-----Error: Undefined Value
    var longitude = result.ReturnValue.Longitude;

    var req = null;
    try {
        req = new XMLHttpRequest();

        if (typeof req.overrideMimeType != "undefined") {
            req.overrideMimeType("text/xml");
        }

        req.onreadystatechange = function () {
            if (req.readyState == 4) {
                if (req.status == 200) {}
            } else {
                alert("Error");
            }
        }

        req.open("POST", "http://localhost:8080/GPS/location", true);
        req.setRequestHeader("longitude", +longitude);
        req.setRequestHeader("latitude", +latitude);

        req.send();
    } catch (ex) {
        alert(ex);
    }
}