+ Reply
Page 5 of 8 FirstFirst ... 34567 ... LastLast
Results 81 to 100 of 141

Your favorite Apple, iPhone, iPad, iOS, Jailbreak, and Cydia site.


Thread: Improving our Coding Skills

is a discussion within the

File Mods

forums, a part of the

iPhone Modding

section;
Well indexOf only accepts one parameter - so you couldn't go putting extra ones in there as you'd just get a syntax error, so you'd just check the results with
...
  1. #81
    Evil Genius Developer GroovyCarrot's Avatar
    Join Date
    Jan 2012
    Location
    Liverpool, United Kingdom
    Posts
    529
    Thanks
    99
    Thanked 2,006 Times in 450 Posts

    Well indexOf only accepts one parameter - so you couldn't go putting extra ones in there as you'd just get a syntax error, so you'd just check the results with || (or) and that kind of code execution doesn't cost anything really, you can use new lines and tabs to make it look visually easier if you want

    Read documentation for JS indexOf function and you'll understand how to use it properly

  2. The Following 2 Users Say Thank You to GroovyCarrot For This Useful Post:

    NewdestinyX (11-11-2013), RKO1195 (11-13-2013)

  3. #82
    ----NewD 2.0---- NewdestinyX's Avatar
    Join Date
    Jan 2012
    Posts
    9,624
    Thanks
    4,016
    Thanked 8,840 Times in 4,439 Posts

    Thanks, Groovy - as always..

    Okay -- here's one I've googled thoroughly but have found no answers to my questions....

    Google's JSON call parses its location information into as many as 9 address formats from which you can grab data to display. BUT for some locations there's no data of any kind in data formats '6,7 & 8'.. Unless I comment them out - the widget fails.

    Code:
    var GoogleTest = true;
    
    	var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + obj.coords + "&sensor=false";
    	$.getJSON(url, function(data) {
    	
    	if (GoogleTest == true) {
    	obj.A = data.results[0].formatted_address;
    	obj.B = data.results[1].formatted_address;
    	obj.C = data.results[2].formatted_address;
    	obj.D = data.results[3].formatted_address;
    	obj.E = data.results[4].formatted_address;
    	obj.F = data.results[5].formatted_address;
    	//obj.G = data.results[6].formatted_address;
    	//obj.H = data.results[7].formatted_address;
    	//obj.J = data.results[8].formatted_address;
    	}
    }); 	//closes JSON call
    But there are some other locations where I need the data in obj.G, H and J.. when there's data Google's transmitting - and I uncomment out those last three objects - the widget works.. How can I test for whether there's any data in those fields. All my attempts at using if ((obj.G != "") && (obj. != null)) come up empty.. Even having them active in the code - when Google's not providing any information in data.results [6], [7] or [8] will fail the widget. Any thoughts?

    Thanks in advance,
    Grant
    Last edited by NewdestinyX; 11-14-2013 at 02:27 PM.
    Here to help if I can. If I can't I know someone who can!

  4. #83
    ----NewD 2.0---- NewdestinyX's Avatar
    Join Date
    Jan 2012
    Posts
    9,624
    Thanks
    4,016
    Thanked 8,840 Times in 4,439 Posts

    After a long search.. I finally found someone at StackOverflow's site that needed to do this 'very simple task' that I would think isn't all that uncommon. I need to make sure that all data returned from a JSON call.. is in initial cap format.. Yet sometimes - Google returns, randomly, some fields in all caps...

    So I 'often' need to change: "VILLANOVA" to "Villanova" or "HUNTINGDON VALLEY" to "Huntingdon Valley". Though this is simple with the CSS transform:capitalize command.. I need the value changed for IF statements all in javascript.. Though I'm still trying to understand what all is going on in this command series.. Here's what worked for me...

    Let's say the object I need the conversion for is obj.one... all you do after you get the results from Google is use this command:

    Code:
    obj.one = obj.one.toLowerCase().replace(/\b[a-z]/g, function(letter) {
                return letter.toUpperCase();
            });
    If the parsed result from Google was VILLANOVA - you now get Villanova. Doesn't matter how many words in the string.
    Here to help if I can. If I can't I know someone who can!

  5. The Following User Says Thank You to NewdestinyX For This Useful Post:

    King_O_Hill (11-25-2013)

  6. #84
    ----NewD 2.0---- NewdestinyX's Avatar
    Join Date
    Jan 2012
    Posts
    9,624
    Thanks
    4,016
    Thanked 8,840 Times in 4,439 Posts

    Been looking for a long time for a test with IF when looking for a numeric digit in a string where you expect only text.

    If MyVar is our variable we want to test, the code is:
    Code:
    if (/\d/.test(MyVar)) {........}
    this returns TRUE for there being any numeric digits in the string

    It would also work for an object:
    Code:
    if (/\d/.test(obj.cityname)) {........}
    Here to help if I can. If I can't I know someone who can!

  7. The Following User Says Thank You to NewdestinyX For This Useful Post:

    King_O_Hill (11-27-2013)

  8. #85
    ----NewD 2.0---- NewdestinyX's Avatar
    Join Date
    Jan 2012
    Posts
    9,624
    Thanks
    4,016
    Thanked 8,840 Times in 4,439 Posts

    I'm guessing that my Google search questions are getting more refined that I'm finding things I need faster.. Yay.. It's about time..

    I ran into a situation today.. one, that's actually probably been haunting me for a while, and not let certain IF/THEN/ELSE tests produce a TRUE condition for me... I decided recently to use Google's reverse Geocode JSON call to get more complete info for those of us in the 17 states that use townships and municipalities - since it seems like this time Yahoo has chosen to permanently delete that info from <extraLocCity> (boohoo!).. But finding a workaround is all part of making me a better coder..

    So Google's data output formats aren't very consistent.. but one of them uses a complete address that looks like
    "912-998 Fitzwatertown Road, Abington, PA 19001, USA" - so the first thing you do is 'Split off' only the data you want - which in this case I want the 'city name' which is Abington.. So you apply the "split" command - in my case - split everything at the commas..
    Code:
    obj.cityGOOGLE = data.results[0].formatted_address.split(",")[1];
    - the number [1] at the end gets me the characters AFTER the 1st comma and before the 2nd comma.
    So I've been thinking -- I would get "Abington" as my result string that I could do my IF/THEN tests with.. But they kept failing as if there was a different string there... So -- duh... When I split at that first comma - there is still a 'leading space' before the "A" of "Abington". So really what the 'split' command did was leave me with:
    " Abington" and not "Abington". So I needed some kind of 'trim leading/trailing spaces' command.. And here it is.
    Code:
    myVariable = myVariable.replace(/^\s+|\s+$/g,'');
    ..where myVariable is whatever object I'm working with..
    Then - any command that comes after a period "." can be added to the end of other commands.. So in the end my code line, with the split and replace leading/trailing spaces, looks like this:
    Code:
    obj.cityGOOGLE = data.results[0].formatted_address.split(",")[1].replace(/^\s+|\s+$/g,'');
    Hope that helps!
    Last edited by NewdestinyX; 11-29-2013 at 11:08 AM.
    Here to help if I can. If I can't I know someone who can!

  9. The Following User Says Thank You to NewdestinyX For This Useful Post:

    King_O_Hill (11-29-2013)

  10. #86
    ----NewD 2.0---- NewdestinyX's Avatar
    Join Date
    Jan 2012
    Posts
    9,624
    Thanks
    4,016
    Thanked 8,840 Times in 4,439 Posts

    Well I learned something valuable today. My favorite code-help dudes have been very busy and not able to help me sort this one out for a while - so I started the laborious process of debugging it myself. And the moment I looked at my code for the first pass - something popped out... and though I don't think it's 100% my problem - I've had greater stability with my Ultimate Location iWidget after making this change - that had to do with two variables having the same name within a function.

    When the widget's set to use the Google JSON - I've had trouble with the iWidget disappearing on any respring.. And on Reboot from within any tweak that allows rebooting. In these times the only thing that gets it back is a HARD REBOOT.. But when set to the pure XML from Yahoo side (Widget Weather for all) or the Weather Underground JSON (for city name only) - to parse data from... this "disappear on respring" problem doesn't exist..

    It would appear that, ON THE PHONE (no failures when testing on computer)... When you have 2 different JSON calls within one JQuery - even if one of the them is nested within an IF/THEN statement... they must have UNIQUE variable names.. You can't have 2 "var url =" statements. One needs to be "var url =" and the other "var url2 =", etc...

    So I needed it to look this way (changes in red):
    Code:
    function updateLocation() {
    if (test == true) {var path = "widgetweather.xml";} else {var path = "file:///private/var/mobile/Documents/widgetweather.xml";}
    
    //Get Google, WU and XML info	
    jQuery.get(path, function(data){ 
    	
    obj.updatetimestring = $(data).find('updatetimestring').text();
    if (updateFileTimer != obj.updatetimestring) {
    obj.time24hour = new Array;
    obj.hwhere = new Array;
    
    //GET DATA FROM XML	
    $(data).find('currentcondition').each( function() {
    obj.city =$(this).find('name').text();
    obj.city2 =$(this).find('extraLocCity').text();
    obj.neighborhood =$(this).find('extraLocNeighborhood').text();
    obj.county =$(this).find('extraLocCounty').text();
    obj.statecode =$(this).find('extraLocStateCode').text();
    obj.state =$(this).find('extraLocState').text();
    obj.country =$(this).find('extraLocCountry').text();
    obj.coords = $(this).find('extraLocName').text().replace(/ /g,"");
    });
    
    //GET DATA FROM WU's JSON
    if (UseWU == true) {
    	var url = "http://api.wunderground.com/api/"+ WUKey +"/geolookup/q/"+ obj.coords +".json"
    	$.getJSON(url, function(data) {
    	obj.cityWU = data.location.city;
    	updateFileTimer = obj.updatetimestring;
    	dealWithLocation(obj);
    	}); //Closes get WU DATA
    	}
    //GET DATA FROM GOOGLE JSON
    var url2 = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + obj.coords + "&sensor=false";
    $.getJSON(url2, function(data) {
    obj.statecodetemp = data.results[0].formatted_address.split(",")[2];
    	if (/\d/.test(obj.statecodetemp)) {obj.cityGO = data.results[0].formatted_address.split(",")[1].replace(/^\s+|\s+$/g,'')
    	} else {
    	obj.cityGO = data.results[0].formatted_address.split(",")[2].replace(/^\s+|\s+$/g,'');
    	}
    obj.citytest = data.results[0].formatted_address.split(",")[1].replace(/^\s+|\s+$/g,'');
    
    //GET REST OF GOOGLE INFO	
    obj.zero = data.results[0].formatted_address;
    //etc,etc..........rest of parsing........
    
    updateFileTimer = obj.updatetimestring;
    dealWithLocation(obj);
    }); 
    	
    }//closes updateFileTimer
    	
    }).fail(function() {
    document.getElementById("xmlupdate").innerHTML = "No XML file !";
    });	
    
    refreshTimer = setTimeout(updateLocation, 20*1000);
    
    }
    Weather Underground and Google never function at the same time because of the IF statement I create there.. But still the widget has refresh problems if I don't name those two variables uniquely. Does anything else account for my issue that anyone can see?
    Last edited by NewdestinyX; 12-21-2013 at 10:16 AM.
    Here to help if I can. If I can't I know someone who can!

  11. #87
    King Kong ain't got nothing on me! King_O_Hill's Avatar
    Join Date
    Apr 2011
    Location
    USA
    Posts
    9,148
    Thanks
    40,436
    Thanked 17,067 Times in 6,236 Posts

    Yes, never ever use the same variable name for 2 different functions.
    Widget-Weather App Now Available! Also with MyLocation Support!
    Check out the thread here - Widget-Weather
    To install the Widget-Weather app, just go to Cydia and install. This will also remove MyLocation, as it is built in!
    Want to protect your JailBreak! Add this repo http://cydia.myrepospace.com/iLEXiNFO/ & Install "iLEX R.A.T."
    It will allow you to bring your phone back to a fresh jailbroken state!

  12. The Following 2 Users Say Thank You to King_O_Hill For This Useful Post:

    NewdestinyX (12-21-2013), RKO1195 (12-22-2013)

  13. #88
    ----NewD 2.0---- NewdestinyX's Avatar
    Join Date
    Jan 2012
    Posts
    9,624
    Thanks
    4,016
    Thanked 8,840 Times in 4,439 Posts

    Any of you coding gurus out there know to test for the "success" of data being returned from a JSON call with JQuery? Sometimes Google has no return results for a particular set of coordinates I enter. I'd like to test and set a flag if there's actual data being reported back or not? So that I can use a different JSON if the data comes back as failure.

    Been through about 30 articles on stack overflow and I'm just not really finding the specific answer that I need.

    Thanks in advance for any help.
    Here to help if I can. If I can't I know someone who can!

  14. #89
    What's Jailbreak?
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    0
    Thanked 4 Times in 2 Posts

    Yes you have!
    It is the "status", last line in the json response file.

    Code:
    function codeLatLng() {
        var latlng = new google.maps.LatLng(latitude,longitude);
        geocoder.geocode({'latLng': latlng}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
              map.setZoom(19);
    		  marker = new google.maps.Marker({
                  position: latlng,
                  map: map
              });
    		  var address=results[0].formatted_address;
             $("#address").text(address);
    		  }
          } else {
            alert("Geocoder failed due to: " + status);
          }
        });
    	}
    will get json response file with "status":"OK"
    see JSON response file (last line).
    Now add a switch to test if Status ==OK or undefined, no response file no status.

  15. The Following 3 Users Say Thank You to noobdude For This Useful Post:

    King_O_Hill (03-13-2014), NewdestinyX (03-13-2014), RKO1195 (03-13-2014)

  16. #90
    ----NewD 2.0---- NewdestinyX's Avatar
    Join Date
    Jan 2012
    Posts
    9,624
    Thanks
    4,016
    Thanked 8,840 Times in 4,439 Posts

    Well Jake (Groovy Carrot) came through in a big way for me via a conversation earlier with this great data.. A couple ways to accomplish what I asked there...
    Way 1 - sets up using different JSON in the case of failure from first one:
    Code:
    $.ajax({
      dataType: "json",
      url: url,
      data: data,
      success: function success(response) {
        // handle response var
      }
    })
    .fail(function(){
       // didn't work
    });
    This next way allows for the use of an IF statement... though Jake makes the noted comment at end - that I don't 'fully understand'.. but..
    Code:
    //Of course you could just set a bool to whether or not it worked before the scope:
    var worked = false;
    $.ajax({
      dataType: "json",
      url: url,
      data: data,
      success: function success(response) {
        // handle response
      var worked = true;
      }
    });
    
    if(worked) {
    
    }
    
    //Not 100% in if the success scope has access to worked var though
    Then his last thought - was for a coding strand that would be more in keeping with the best coding practices.:
    Code:
    See it also takes an error parameter which is what you're after
    
    So you can use that as:
    $.ajax({
      dataType: "json",
      url: url,
      data: data,
      success: function(response) {
        // handle response var
      },
      error: function() {
        // didn't work
      }
    });
    For this last one - it looks like you're not setting a variable that you then test with an IF statement - but rather when the data doesn't return from the JSON call - you direct the program (widget) to start another function which would contain another JSON request (via JQuery).. if I'm understanding correctly.

    Thanks again, Jake! If any others would chime in to help choose the best... GooglemapsAPI fails for a lot of latitude/longitude coordinates in the world - not the least of which is Bermuda where our good friend, Simon lives.. Saudi Arrabia is another place.. I want to use Google as my 'dealWithLocation' function for all times unless it fails.. Then I want to use WidgetWeather's data.
    Last edited by NewdestinyX; 03-13-2014 at 03:03 PM.
    Here to help if I can. If I can't I know someone who can!

  17. The Following User Says Thank You to NewdestinyX For This Useful Post:

    RKO1195 (03-13-2014)

  18. #91
    King Kong ain't got nothing on me! King_O_Hill's Avatar
    Join Date
    Apr 2011
    Location
    USA
    Posts
    9,148
    Thanks
    40,436
    Thanked 17,067 Times in 6,236 Posts

    Quote Originally Posted by noobdude View Post
    Yes you have!
    It is the "status", last line in the json response file.

    Code:
    function codeLatLng() {
        var latlng = new google.maps.LatLng(latitude,longitude);
        geocoder.geocode({'latLng': latlng}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
              map.setZoom(19);
      marker = new google.maps.Marker({
                  position: latlng,
                  map: map
              });
      var address=results[0].formatted_address;
             $("#address").text(address);
      }
          } else {
            alert("Geocoder failed due to: " + status);
          }
        });
    }
    will get json response file with "status":"OK"
    see JSON response file (last line).
    Now add a switch to test if Status ==OK or undefined, no response file no status.
    I like this method! Nice work. I see you only have 2 posts, where have you been since now?
    Widget-Weather App Now Available! Also with MyLocation Support!
    Check out the thread here - Widget-Weather
    To install the Widget-Weather app, just go to Cydia and install. This will also remove MyLocation, as it is built in!
    Want to protect your JailBreak! Add this repo http://cydia.myrepospace.com/iLEXiNFO/ & Install "iLEX R.A.T."
    It will allow you to bring your phone back to a fresh jailbroken state!

  19. The Following User Says Thank You to King_O_Hill For This Useful Post:

    RKO1195 (03-13-2014)

  20. #92
    What's Jailbreak?
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    0
    Thanked 4 Times in 2 Posts

    Quote Originally Posted by King_O_Hill View Post
    I like this method! Nice work. I see you only have 2 posts, where have you been since now?
    Thanks, but sadly it is not my code.
    Credit has to go to Rasputin007 who wrote UniAW-GoogleMaps cydget.

  21. #93
    ----NewD 2.0---- NewdestinyX's Avatar
    Join Date
    Jan 2012
    Posts
    9,624
    Thanks
    4,016
    Thanked 8,840 Times in 4,439 Posts

    Quote Originally Posted by noobdude View Post
    Thanks, but sadly it is not my code.
    Credit has to go to Rasputin007 who wrote UniAW-GoogleMaps cydget.
    Thanks, "Ras".. I think I see it..
    Last edited by NewdestinyX; 03-21-2014 at 08:41 AM.
    Here to help if I can. If I can't I know someone who can!

  22. #94
    King Kong ain't got nothing on me! King_O_Hill's Avatar
    Join Date
    Apr 2011
    Location
    USA
    Posts
    9,148
    Thanks
    40,436
    Thanked 17,067 Times in 6,236 Posts

    Quote Originally Posted by NewdestinyX View Post
    Thanks, "Ras".. I think I see it..
    Good eye bro. I see it too now.
    Widget-Weather App Now Available! Also with MyLocation Support!
    Check out the thread here - Widget-Weather
    To install the Widget-Weather app, just go to Cydia and install. This will also remove MyLocation, as it is built in!
    Want to protect your JailBreak! Add this repo http://cydia.myrepospace.com/iLEXiNFO/ & Install "iLEX R.A.T."
    It will allow you to bring your phone back to a fresh jailbroken state!

  23. #95
    ----NewD 2.0---- NewdestinyX's Avatar
    Join Date
    Jan 2012
    Posts
    9,624
    Thanks
    4,016
    Thanked 8,840 Times in 4,439 Posts

    Would any of you veteran coders be able to tell me why the following code - creates a situation where I have to refresh my preview 'twice' before I see my changes? It seems like there's a hiccup somewhere in the double JQueryGet.. Is that it?

    Code:
    var test = true;
    var coordstest = "";
    var docnum = "";
    function updateLocation() {
    
    if (test == true) {var path = "myLocation.txt"; var path2 = "widgetweather.xml";} else {var path = "file:///private/var/mobile/Documents/myLocation.txt";}
    
    //GET DATA FROM MYLOCATION.TXT
    jQuery.get(path, function(appdata){
    	var substr = appdata.split('\n');
    	var templatitude=substr[0].split('=');
    	var templongitude=substr[1].split('=');
    	var latitude = $.trim(templatitude[1]);
    	var longitude = $.trim(templongitude[1]);
    
    	obj.coords = latitude + "," + longitude;	
    	if (test == true) {
    		if (coordstest == "") { coords = obj.coords; }
    		else { coords = coordstest; }
    	} else { coords = obj.coords }
    	obj.coordsdis = obj.coords.replace(/,/g,", ");
    });
    
    //GET DATA FROM WIDGETWEATHER.XML	
    jQuery.get(path2, function(data){
    	$(data).find('currentcondition').each( function() {
    	
    	obj.xmlcoords =$(this).find('extraLocName').text();
    	if (test == true) {
    		if ((coordstest == "") && (docnum == "")) { coords = obj.xmlcoords; }
    	        }
    	obj.coordsdis = obj.xmlcoords.replace(/,/g,", ");
    });
    
    //GET DATA FROM GOOGLE JSON
    var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + coords + "&sensor=false";
    $.getJSON(url, function(data) {
    	//GET ALL RAW Google Data	
    	obj.zero = data.results[0].formatted_address;
    	obj.one = data.results[1].formatted_address.split(",")[0].replace(/^\s+|\s+$/g,'');
    		
    	dealWithLocation(obj);
    			
    	});//CLOSES Google JSON call
    
    }); //CLOSES JQueryGet
    
    refreshTimer = setTimeout(updateLocation, 20*1000);
    
    } //CLOSES functionUpdateLocation
    Here to help if I can. If I can't I know someone who can!

  24. #96
    Addicted to Code Dacal's Avatar
    Join Date
    Nov 2010
    Posts
    1,891
    Thanks
    2,898
    Thanked 8,485 Times in 1,500 Posts

    Quote Originally Posted by NewdestinyX View Post
    Would any of you veteran coders be able to tell me why the following code - creates a situation where I have to refresh my preview 'twice' before I see my changes? It seems like there's a hiccup somewhere in the double JQueryGet.. Is that it?

    Something like that maybe ?

    var test = true;
    var obj = new Array;
    var coordstest = "";
    var docnum = "";
    var refreshTimer;

    if (test == true) {
    var path = "myLocation.txt";
    var path2 = "widgetweather.xml";
    } else {
    var path = "file:///private/var/mobile/Documents/myLocation.txt";
    var path2 = "file:///private/var/mobile/Documents/widgetweather.xml";
    }

    function updateLocation() {
    if (test == false ) {
    updateCoordinatesFromMyLocation();
    updateCoordinatesFromWW();
    } else {
    obj.coords = coordstest;
    }
    getDataFromGoogle();
    refreshTimer = setTimeout(updateLocation, 20*1000);
    }

    //GET DATA FROM MYLOCATION.TXT
    function updateCoordinatesFromMyLocation() {
    jQuery.get(path, function(appdata) {
    var substr = appdata.split('\n');
    var templatitude=substr[0].split('=');
    var templongitude=substr[1].split('=');
    var latitude = $.trim(templatitude[1]);
    var longitude = $.trim(templongitude[1]);
    obj.coords = latitude + "," + longitude;
    });
    }

    //GET DATA FROM WIDGETWEATHER.XML
    function updateCoordinatesFromWW() {
    jQuery.get(path2, function(data) {
    $(data).find('currentcondition').each( function() {
    obj.coords =$(this).find('extraLocName').text();
    });
    });
    }
    BTW, if you have both files, the coordinates will be taken from the last call (WW XML). I suppose it's what you want ? I not, you can easily create a fallback (for exemple, get the coordinates from myLocation, if not exist, check for WW XML).
    Last edited by Dacal; 04-19-2014 at 06:55 AM.


    My themes : Jade HD, Silex HD, BaSalt HD,ToPaz
    All are on Cydia and/or Theme It.

  25. The Following User Says Thank You to Dacal For This Useful Post:

    King_O_Hill (04-19-2014)

  26. #97
    ----NewD 2.0---- NewdestinyX's Avatar
    Join Date
    Jan 2012
    Posts
    9,624
    Thanks
    4,016
    Thanked 8,840 Times in 4,439 Posts

    Thanks Dacal!!

    1)What would the fallback code look like?
    2) in my original code what is the reason for the hiccup ( double hit refresh key)? When I remove the 2nd JQueryGet - then I only have to hit the refresh button once. I need the coordinates from BOTH .txt & .xml stored in a variable for IF/ ELSE stuff in dealWithLocation()..

    Thanks in advance..
    Here to help if I can. If I can't I know someone who can!

  27. #98
    Addicted to Code Dacal's Avatar
    Join Date
    Nov 2010
    Posts
    1,891
    Thanks
    2,898
    Thanked 8,485 Times in 1,500 Posts

    Quote Originally Posted by NewdestinyX View Post
    Thanks Dacal!!

    1)What would the fallback code look like?
    2) in my original code what is the reason for the hiccup ( double hit refresh key)? When I remove the 2nd JQueryGet - then I only have to hit the refresh button once. I need the coordinates from BOTH .txt & .xml stored in a variable for IF/ ELSE stuff in dealWithLocation()..

    Thanks in advance..
    Sincerely, a lot of small things are wrong... I don't have tried to understand .

    For the fallback :

    var test = true;
    var obj = new Array;
    var coordstest = "";
    var docnum = "";
    var refreshTimer;

    if (test == true) {
    var path = "myLocation.txt";
    var path2 = "widgetweather.xml";
    } else {
    var path = "file:///private/var/mobile/Documents/myLocation.txt";
    var path2 = "file:///private/var/mobile/Documents/widgetweather.xml";
    }

    function updateLocation() {
    if (test == false ) {
    updateCoordinatesFromMyLocation();
    } else {
    obj.coords = coordstest;
    }
    getDataFromGoogle();
    refreshTimer = setTimeout(updateLocation, 20*1000);
    }

    //GET DATA FROM MYLOCATION.TXT
    function updateCoordinatesFromMyLocation() {
    jQuery.get(path, function(appdata) {
    var substr = appdata.split('\n');
    var templatitude=substr[0].split('=');
    var templongitude=substr[1].split('=');
    var latitude = $.trim(templatitude[1]);
    var longitude = $.trim(templongitude[1]);
    obj.coords = latitude + "," + longitude;
    }).fail(function() {
    updateCoordinatesFromWW();
    });
    }

    //GET DATA FROM WIDGETWEATHER.XML
    function updateCoordinatesFromWW() {
    jQuery.get(path2, function(data) {
    $(data).find('currentcondition').each( function() {
    obj.coords =$(this).find('extraLocName').text();
    });
    });
    }

    //GET DATA FROM GOOGLE JSON
    function getDataFromGoogle() {
    var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + obj.coords + "&sensor=false";
    $.getJSON(url, function(data) {
    obj.zero = data.results[0].formatted_address;
    obj.one = data.results[1].formatted_address.split(",")[0].replace(/^\s+|\s+$/g,'');
    dealWithLocation(obj);
    });
    }


    My themes : Jade HD, Silex HD, BaSalt HD,ToPaz
    All are on Cydia and/or Theme It.

  28. The Following 3 Users Say Thank You to Dacal For This Useful Post:

    bombmom (04-22-2014), King_O_Hill (04-19-2014), NewdestinyX (04-28-2014)

  29. #99
    ----NewD 2.0---- NewdestinyX's Avatar
    Join Date
    Jan 2012
    Posts
    9,624
    Thanks
    4,016
    Thanked 8,840 Times in 4,439 Posts

    Dacal, bombmom, Marty.... - can you test the failure of one parsed field of several from a JSON call that contains no data?

    For all US addresses - Google returns data for all these fields:
    obj.zero = data.results[0].formatted_address;
    obj.one = data.results[1].formatted_address;
    obj.two = data.results[2].formatted_address;
    obj.three = data.results[3].formatted_address;
    obj.four = data.results[4].formatted_address;
    obj.five = data.results[5].formatted_address;

    But occasionally I need data from field '6'.. But - the call to Google will FAIL (and fail the widget) if there's NO data in field 6 when I call for it..

    So how can I use the .fail function or a boolean or an alert I can test for - so that if there's NO data coming back from Google in field 6 -- I can simply set {obj.six = "no data";} - so the widget won't fail when Google's only reporting fields 0-5?

    Thanks!
    Grant

    My current code -- whenever I 'uncomment' the obj.six line - the widget fails - UNLESS there's data coming from Google in field 6.

    Code:
    var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + coords + "&sensor=false";
    $.getJSON(url, function(data) {
    //GET ALL RAW Google Data	
    obj.zero = data.results[0].formatted_address;
    obj.one = data.results[1].formatted_address.split(",")[0].replace(/^\s+|\s+$/g,'');		
    obj.two = data.results[2].formatted_address.split(",")[0].replace(/^\s+|\s+$/g,'');		
    obj.three = data.results[3].formatted_address.split(",")[0].replace(/^\s+|\s+$/g,'');	
    obj.four = data.results[4].formatted_address.split(",")[0].replace(/^\s+|\s+$/g,'');
    obj.five = data.results[5].formatted_address.split(",")[0].replace(/^\s+|\s+$/g,'');
    //obj.six = data.results[6].formatted_address.split(",")[0].replace(/^\s+|\s+$/g,'');
    )};
    Last edited by NewdestinyX; 04-28-2014 at 10:39 AM.
    Here to help if I can. If I can't I know someone who can!

  30. #100
    Addicted to Code Dacal's Avatar
    Join Date
    Nov 2010
    Posts
    1,891
    Thanks
    2,898
    Thanked 8,485 Times in 1,500 Posts

    Quote Originally Posted by NewdestinyX View Post
    Dacal, bombmom, Marty.... - can you test the failure of one parsed field of several from a JSON call that contains no data?

    For all US addresses - Google returns data for all these fields:
    obj.zero = data.results[0].formatted_address;
    obj.one = data.results[1].formatted_address;
    obj.two = data.results[2].formatted_address;
    obj.three = data.results[3].formatted_address;
    obj.four = data.results[4].formatted_address;
    obj.five = data.results[5].formatted_address;

    But occasionally I need data from field '6'.. But - the call to Google will FAIL (and fail the widget) if there's NO data in field 6 when I call for it..

    So how can I use the .fail function or a boolean or an alert I can test for - so that if there's NO data coming back from Google in field 6 -- I can simply set {obj.six = "no data";} - so the widget won't fail when Google's only reporting fields 0-5?

    Thanks!
    Grant

    My current code -- whenever I 'uncomment' the obj.six line - the widget fails - UNLESS there's data coming from Google in field 6.
    More simple to use the XML url for Google Maps and do something like that.

    Code:
    function GoogleMaps(coords) {
    			var url = "http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + coords + "&sensor=false";
    			jQuery.get(url, function(data) {
    			obj.address = [];
    			for (var i=0; i <=6; i++) { obj.address[i] = "NO DATA"; }
    			var t=0;
    				$(data).find('result').each( function() {
    					obj.address[t] = $(this).find('formatted_address').text().split(",")[0].replace(/^\s+|\s+$/g,'');
    					t++;
    				});
    			dealwithdata();
    			});
    }
    
    function dealwithdata() {
    			document.getElementById("address0").innerHTML = obj.address[0];
    			document.getElementById("address1").innerHTML = obj.address[1];
    			document.getElementById("address2").innerHTML = obj.address[2];
    			document.getElementById("address3").innerHTML = obj.address[3];
    			document.getElementById("address4").innerHTML = obj.address[4];
    			document.getElementById("address5").innerHTML = obj.address[5];
    			document.getElementById("address6").innerHTML = obj.address[6];    
    }


    My themes : Jade HD, Silex HD, BaSalt HD,ToPaz
    All are on Cydia and/or Theme It.

  31. The Following 3 Users Say Thank You to Dacal For This Useful Post:

    King_O_Hill (04-29-2014), NewdestinyX (05-01-2014), RKO1195 (04-29-2014)

+ Reply
Page 5 of 8 FirstFirst ... 34567 ... LastLast
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts