Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Database and Distance function

$
0
0

Hello there, i'm stuck in an'annoying problem with Database. I want to create a table view which shows in very row the distance between a point and a fixed location. The app retrieve the data from a Database and use them in the distance function. The problem is that when the latitude or the longitude is retrieved from the database i have this error "Undefined" is not a function when evaluating 't.toRad() in app.js.

I can't figure out what is the problem, maybe there's some problem in how I stored the latitude and longitude in the database, i used the float field type... Can someone help me please?

This is the code of the app...

var myapp = {}; // app's namespace
 
// Install the database
Ti.Database.install('hotel.sqlite', 'hotel');
 
myapp.convertTemp = function(temp) {
    if(Ti.App.Properties.getString('units','c')==='f') {
        return Math.round(temp*1.8+32) +'\u00b0F'; // convert to Fahrenheit & append degree symbol-F
    } else {
        return temp +'\u00b0C';
    }
};
 
 
 
Number.prototype.toDeg = function() {
    return this * 180 / Math.PI;
}
Number.prototype.toRad = function() {
    return this * Math.PI / 180;
}
 
function getDistance(lat1,lon1,lat2,lon2){
    var R = 6371; // km
    var dLat = (lat2-lat1).toRad();
    var dLon = (lon2-lon1).toRad();
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
    Math.sin(dLon/2) * Math.sin(dLon/2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    var d = R * c;
    d=(d.toFixed(2));
    return d
}       
 
var lat1,lon1,lat2,lon2;
lat1=51; // The coordinates of the current location should go here somehow.
lon1=4; // The coordinates of the current location should go here somehow.
lat2=48;
lon2=14; 
 
 
/*
 * getRows() queries the database and builds the rows of our weather table
 */
myapp.getRows = function() {
    var db = Ti.Database.open('hotel');
    var data = [];
    var rs = db.execute('SELECT name, latitude, longitude FROM hotel ORDER BY name');
    while(rs.isValidRow()) {
 
        var tblRow = Ti.UI.createTableViewRow();
 
 
 
                var view = Ti.UI.createView({
                    height:"400px",
                    width:Ti.UI.SIZE,
                    layout:"vertical"
                });
                var label = Ti.UI.createLabel({
                left: 10,
                height:"100 px",
                text: rs.fieldByName('name')});
 
                var label2 = Ti.UI.createLabel({
                left: 10,
                height:"100 px",
                text: rs.fieldByName('longitude') });
 
                var label3 = Ti.UI.createLabel({
                left: 10,
                height:"100 px",
                text: rs.fieldByName('latitude') });
 
                var fixedlat = rs.fieldByName('latitude');
                var fixedlon = rs.fieldByName('longitude');
 
     var distance =  Titanium.UI.createLabel({
    text:getDistance(fixedlat,fixedlon,lat2,lon2) + " km",
    width:'auto',
    textAlign:'left',
    top:25,
    left:75,
    height:12,
 
});
 
 
 
 
 
 
 
 
 
                view.add(label);
                view.add(label2);
                view.add(label3);
                view.add(distance);
 
        tblRow.add(view);
 
 
        data.push(tblRow);
        rs.next();
    }
    db.close();
    return data;
};
 
/*
 * createWxWindow() creates the window that contains our weather table
 */
myapp.createWxWindow = function(){
    var win = Ti.UI.createWindow({
        title: 'Current Conditions',
        backgroundColor:'white'
    });
    var table = Ti.UI.createTableView({
    allowsSelection:false,
    backgroundColor:"transparent"
 
});
    // define, then call a function to populate our weather table
    function populate(){
        table.setData(myapp.getRows());
    }
    populate();
    // add an event listener to repopulate our table when the units preferences are changed
    Ti.App.addEventListener('app:unitschanged', populate);
    win.add(table);
    return win;
};
/*
 
};
 
/*
 * Define our tab group
 */
myapp.createTabs = function() {
    var tabGroup = Titanium.UI.createTabGroup();
 
    tabGroup.addTab(Ti.UI.createTab({
        title:'Conditions',
        window:myapp.createWxWindow()
    }));  
    tabGroup.addTab(Ti.UI.createTab({
        title:'Units',
        window:myapp.createPrefsWindow()
    }));  
    return tabGroup;
}
 
myapp.ui = myapp.createTabs();
myapp.ui.open();

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>