Search code examples
javascriptpostblogsblogger

how to retrieve location via json in Blogger


I use this script in Blogger to show labeled posts via JSON, as you see through this code I can retrieve posts' titles, thumbnails and dates, etc

 <script type='text/javascript'>
    //<![CDATA[
    function labelthumbs(json) {
        document.write('<ul id="label_with_thumbs">');
        for (var i = 0; i < numposts; i++) {
            var entry = json.feed.entry[i];
            var posttitle = entry.title.$t;
            var posturl;
            if (i == json.feed.entry.length) break;
            for (var k = 0; k < entry.link.length; k++) {
                if (entry.link[k].rel == 'replies' && entry.link[k].type == 'text/html') {
                    var commenttext = entry.link[k].title;
                    var commenturl = entry.link[k].href;
                }
                if (entry.link[k].rel == 'alternate') {
                    posturl = entry.link[k].href;
                    break;
                }
            }
            var thumburl;
            try {
                thumburl = entry.media$thumbnail.url;
            } catch (error) {
                s = entry.content.$t;
                a = s.indexOf("<img");
                b = s.indexOf("src=\"", a);
                c = s.indexOf("\"", b + 5);
                d = s.substr(b + 5, c - b - 5);
                if ((a != -1) && (b != -1) && (c != -1) && (d != "")) {
                    thumburl = d;
                } else thumburl = 'https://1.bp.blogspot.com/-etyXYLaEXP8/WrmiznwRzXI/AAAAAAAAAnQ/Pu4OVqzx_4kmgwnmxJGDuf6j-l6ARuHbgCLcBGAs/s1600/nothumbnailimage.png';
            }
            var postdate = entry.published.$t;
            var cdyear = postdate.substring(0, 4);
            var cdmonth = postdate.substring(5, 7);
            var cdday = postdate.substring(8, 10);
            var monthnames = new Array();
            monthnames[1] = "January";
            monthnames[2] = "February";
            monthnames[3] = "March";
            monthnames[4] = "April";
            monthnames[5] = "May";
            monthnames[6] = "June";
            monthnames[7] = "July";
            monthnames[8] = "August";
            monthnames[9] = "September";
            monthnames[10] = "October";
            monthnames[11] = "November";
            monthnames[12] = "December";
            document.write('<li>');
            if (showpostthumbnails == true)
                document.write('<a class="posturl" href="' + posturl + '" target ="_top"><img class="label_thumb" src="' + thumburl + '"/></a>');
            document.write('<a class="posturl" href="' + posturl + '" target ="_blank"><h3 class="posttitle">' + posttitle + '</h3><span class="dateposted">' + monthnames[parseInt(cdmonth, 10)] + ' ' + cdday + ', ' + cdyear + '></span></a>');
            if ("content" in entry) {
                var postcontent = entry.content.$t;
            } else
            if ("summary" in entry) {
                var postcontent = entry.summary.$t;
            } else var postcontent = "";
            var re = /<\S[^>]*>/g;
            postcontent = postcontent.replace(re, "");
            if (showpostsummary == true) {
                if (postcontent.length < numchars) {
                    document.write('');
                    document.write(postcontent);
                    document.write('');
                } else {
                    document.write('');
                    postcontent = postcontent.substring(0, numchars);
                    var quoteEnd = postcontent.lastIndexOf(" ");
                    postcontent = postcontent.substring(0, quoteEnd);
                    document.write(postcontent + '...');
                    document.write('');
                }
            }
            var towrite = '';
            var flag = 0;
            if (showpostdate == true) {
                flag = 1;
            }
            if (showcommentnum == true) {
                if (flag == 1) {
                    towrite = towrite + ' | ';
                }
                if (commenttext == '1 Comments') commenttext = '1 Comment';
                if (commenttext == '0 Comments') commenttext = 'No Comments';
                commenttext = '<a href="' + commenturl + '" target ="_top">' + commenttext + '</a>';
                towrite = towrite + commenttext;
                flag = 1;;
            }
            if (displaymore == true) {
                if (flag == 1) flag = 1;
            }
            document.write(towrite);
            document.write('</li>');
            if (displayseparator == true)
                if (i != (numposts - 1))
                    document.write('');
        }
        document.write('</ul>');
    }
    //]]>
</script>

My question is that in every post in my blog does have Location Name that I can specify optionally eg: USA,etc.

So how can I retrieve the location as well via JSON?

P.S This is the HTML of location Name that appears in my blogger template

<span class='published-location' expr:title='data:post.location.name'><data:post.location.name/></span>

Solution

  • The location data entered via the post editor is available in the JSON feed via the key georss$featurename. You will have to include that in your code

    ..
    var entry = json.feed.entry[i];
    var posttitle = entry.title.$t;
    var location = entry.georss$featurename.$t;
    var posturl;
    ..

    And then you print it on the page via the document.write statement in the above code -

    document.write('Location: ' + location + '<br/><a class="posturl" href="' + posturl + '" target ="_blank"><h3 class="posttitle">' + posttitle + '</h3><span class="dateposted">' + monthnames[parseInt(cdmonth, 10)] + ' ' + cdday + ', ' + cdyear + '></span></a>');