Source code and sample html available at: https://github.com/prabgupt/googleMap_virtual_tour
Usage: Mentioned in README present at git repo above
Purpose of Plugin: You can display virtual tours between places on map. Currently, above sample show animation of marker moving along actual direction path between places located on map in sequence specified.
Prequisite
- HTML, jQuery/JavaScript knowledge is required
- Basic knowledge about methods available in google Maps Javascript API v3 is recommended
Summary about method’s inside custom googleMaps js
You’ll need to mainly use two method just like used in sample html.
- showMapForDay- This method would render the map with places displayed as markers and routes connecting those places. It takes following arguments:
location 2D Array – It contains single locations in format [lat, lng] and routes in format [[lat1, lng2], [lat1, lng2]]. You can skip routes from this array if in your case, there will always be a route between two consecutive locations. Routes then, can be auto-generated inside this method after modifying its code a little bit.
zoom - zoom level which you want to set for your map
divId - This is div element Id inside which you want to show map. Google Maps libraries need this as an argument and hence passed here.
Description Array – This is array containing description about each location to be displayed in infoWindow when corresponding marker on map is clicked. This attribute allows HTML code inside it.
- animateTrip – This method displays virtual tour between places in sequence as specified. It also supports play/pause feature. If this function is called when tour is running, it resumes tour while if it is called when no tour is running, it starts tour. Few of the arguments in this method are same as above method:
divId - This is div element Id inside which you want to show map. Google Maps libraries need this as an argument and hence passed here.
location 2D Array – It contains single locations in format [lat, lng]
Description Array – This is array containing description about each location to be displayed in infoWindow when place is reached while virtual tour. This attribute allows HTML code inside it.
callBack Function – This function specifies what to do when tours end. In case not provided, map will halt at last location with its infoWindow opened.
Main function which is actually responsible for enabling virtual tour animation
- animate - We call this function periodically using ‘setTimeout’ Handler and each time this function is called, marker location is incremented by ‘step’(constant variable in js). In case current step is closer to the place we have located on map, marker is displayed for the location and corresponding infoWindow is opened by triggering ‘click’ action on the marker. Likewise, we keep proceeding until we reach ‘eol’(another variable in js) which signifies total path distance to be traveled on map and is calculated dynamically based on map’s locations.
Source code and sample available at: https://github.com/prabgupt/youtube_video_upload_plugin
Usage: Please note that sample is available for RoR3 but it can be easily converted for any underlying platform. Refer README in git repo above to know how to run it in RoR3. To see how to tweak code to use it for any other platform, keep reading the post.
Purpose of this Utility: It will upload end user’s video directly to youtube and is easily integratable within your website.
Few Advantage:
- No need to have storage on server machine as you can now dump/use all videos directly to/from youtube.
- In case you have youtube channel, can directly organize video uploaded through your site to youtube channel.
- NO need for end user to login to youtube first to upload video through your site.
Flow to upload video on YouTube
- Website requests token and upload URL from youtube for every new video request. This is needed to avoid having end user logging into his/her YouTube account.
- Post your video to URL received in step#1 with parameter ‘nexturl’. ‘nexturl’ is callback url to be called by YouTube with status and video_id once upload operation is completed with failure or success. You need to send token received in step#1 too along with.
- Youtube invokes ‘nexturl’ with status and video_id, if upload is successful. Process/store this video_id as this will be key from now onwards based on which you can perform operations on this video directly in YouTube.
Source code contains of following things that you need to know:
youtubeUtilAPIs.py- Python script that uses Google Data Python client library. This script contains few methods performing certain operation on youtube.
Usage: py [-t <comma separated title and description>] [-u <video_id>] [-s <video_id>] [-d <video_id>]
-t => to get token and url. Input required is comma separated video title and video description
-u => to get youtube URL for that video. Input required is video_id
-s => to get upload status of the video. Input required is video_id
-d => to delete video from youtube. Input required is video_id
Prerequisite:
-
- Please refer Getting Started guide. In nutshell, you need to install Python and then google Data Library as specified in the link.
- Replace following senstive data inside python script; like developer key, password, etc. with that of yours. You will need to sign in for YouTube developer key. Once signed in, register you product and get values for below attributes. ‘source’ and ‘client_id’ will be the name of the product you registered before.
yt_service.developer_key = ‘xxxxxxx’
yt_service.client_id = ‘xxxxxxx’
yt_service.email = ‘xxxxxxx’
yt_service.password = ‘xxxxxxxx’
yt_service.source = ‘xxxxxxxxxx’
- Since above utility contains some sensitive data, hence for security reasons you would need to introduce a server side handler that will execute above script on client request and can send the required details back to client. In our case, we have introduced method ‘uploadToken’ inside multimedia controller which execute python script and returns back token and url needed to upload video on youtube.
- Below is the HTML code you’ll plug into your website to let end user select the video and upload it to youtube. Code just contains a form to have end use select the video and has action, token value blank at render time. If you have already noticed, there is async ajax request present at end of code which fetches upload URL and token from server at time page is loading and populate it inside form once ajax request gets completed.
Source code and README available at: https://github.com/prabgupt/geocode_autocompletion_js
Purpose of this Utility: It will auto suggest world places based on user input
Instructions as well as sample HTML to use the code are mentioned inside README mentioned above.
Making of geocode_autocomplete.js
I’m assuming here that you are already familiar with JavaScript/jQuery. You’ll need to understand following things so as to proceed further:
- autocomple jQuery UI: This jquery displays suggested list based on result passed to it
- Google Maps GeoCoding API usage: I’d recommend you to go through ‘Results‘ structure as it will help you to identify attributes you need to process based on your requirement. Once understood, rest would just be getting those attributes from result.
StepWise description of above code:
# First we bind ‘focus’ event to element so that on focus, we can attach autocomplete handler to it.
# source requires map list that will get displayed as suggestion list. We are calling geocode function of google maps here treating input value as some address. You can change the attribute as per what you expect end user to provide.
On receiving response, we are populating map which will be shown as suggested list. An entry in this suggested list is kinda map containing multiple keys/values. You can add/remove keys as per your requirement.
# minLength attr specifies minimum length of input after which it should try to find suggested list.
# select attr contains functions which gets executed once an entry is selected. In our case, once an entry is selected we process its location(lat/lng) and displays it on UI. You can add your logic that you need to execute once an entry is selected.
This post will guide you on how to add wikipedia search on your web site and furthermore how to customize the search output, depending on need for your website. Also all the intra links available inside wikipedia content would be made to open inside your site only rather going to their actual WikiPedia page.
Pre-requisite:
Refer wikipedia API documentation: http://en.wikipedia.org/w/api.php
Basic knowledge about HTML, Javascript/JQuery
Getting Started:
Sample 1:
Click here for Demo. This contains normal input text box and would display wiki content on the same page after some customization in output. Source code for the same can be found here.
- Include google JQuery script as well as some of the wikipedia stylesheets so as to have the content rendered elegantly. Please refer <head>content in source code(link mentioned above) for complete list.
- Create a form to accept the search input
<form accept-charset=”UTF-8″ action=”" id=”inputform” method=”post”><label for=”name”>Name</label><input id=”name” name=”name” type=”text”><input name=”commit” type=”submit” value=”Search”></form>
- Create div tags to hold wiki content returned after search
<div id=”container”><div id=”wiki_container”></div><div id=”wiki_attribution” style=”text-align:left; border-top:1px solid #c73734; font-size:13px;”></div></div>
- Call JavaScript function using JQuery when above form is submitted. Below logic would prevent the whole page to load again but just the page section getting changed (by now you would have understood that I’m talking about AJAX)
$(‘#inputform’).submit(function(){var name = $(‘#name’).attr(“value”);getAreaMetaInfo_Wikipedia(name);return false;});
- Implement the JavaScript function being called above.
- Get content about the object being searched from Wikipedia
function getAreaMetaInfo_Wikipedia(page_id) {title = page_id;$.ajax({url: ‘http://en.wikipedia.org/w/api.php’,data: {action:’parse’,prop:’text’,page:title,format:’json’},dataType:’jsonp’,success: function(data) {wikipage = $(“<div>”+data.parse.text['*']+”</div>”);wikipage.find(‘sup’).remove();$(“#wiki_container”).html(wikipage);$(‘#wiki_attribution’).html(“Description above from the Wikipedia article <a href=’http://en.wikipedia.org/wiki/” +title+”‘ target=’wikipedia’>”+title.replace(/_/g,’ ‘)+“</a>, licensed under <a href=’http://creativecommons.org/licenses/by-sa/3.0/’ target=’wikipedia’>CC-BY-SA</a>, full list of contributors <a href=’http://en.wikipedia.org/w/index.php?title=”+title+”&action=history’ target=’wikipedia’>here</a>. Community Pages are not affiliated with, or endorsed by, anyone associated with the topic.”);}});}
Above post would get content from WikiPedia for object being searched and would also append attribution to wikipedia for the content (as per wikipedia licence)
-
- To have all links inside wiki content to open inside your website only rather having them go to their actual wikipedia page. Include below code inside success funtion above
wikipage.find(‘a’).each(function() {var href = $(this).attr(‘href’);/* put a check for wikipedia link. do not touch rest of the links*/$(this).attr(‘href’, “javascript:getAreaMetaInfo_Wikipedia(‘”+ href.substring(href.lastIndexOf(‘/’)+1) +”‘)”)});
-
- Customize wiki content to display on your side like mentioned below
$(“#References”).hide();$(“div.reflist”).hide(); // hide reference list on wiki$(‘table.infobox’).hide(); //hide info boxes on wiki$(‘table.toc’).hide(); // hide table of content$(‘table.navbox’).hide(); // hide navigation box$(‘span.editsection’).hide(); // hide edit link
Sample 2:
Click here for Demo. This would display wiki summary about the link text when clicked upon, in an inline popup appearing on the same page. Also all the internal links inside that would open in the same popup box rather going to respective wiki page. Source code for the same can be found here.
Concept remains the same as before. Difference here is that we would only be displaying first para of wiki content inside popup which was done using wikipage = $(“<div>”+data.parse.text['*']+”<div>”).children(‘p:first’).
This sample will let you develop a private chatRoom using juggernaut. It push chats in realtime based on publish/subscribe model, hence making it extremely fast and scalable.
- Resources
Nodejs – http://nodejs.org/
Redis – http://code.google.com/p/redis/
Juggernaut – https://github.com/maccman/juggernaut/
JQuery – http://mirror.ozdiy.com/assets/b8/2f96a12bc919b37e09d303b86ea1b9_1251811910.html
- Getting started
1. Install node-v0.2.5 – Download
2. Install redis 2.0.4 – Download
3. Run Redis server
./redis-server redis.conf4. Install juggernaut
git clone git://github.com/maccman/juggernaut.git --recursive
cd juggernaut
sudo node server.jsTill this point, you would have all things in place except that juggernaut would still not be able to publish chats onto channels. This feature will get enabled after Step#5
5. Now you are ready to implement private chat app in RoR3. To get a sample chat application:
git clone git://github.com/prabgupt/chatclient.git --recursive
cd chatclient6. To enable publishing by Juggernaut
sudo gem install juggernaut7. run rails server
rails serveropen URLS: localhost:3000/chat/channel1
localhost:3000/chat/channel2
channel1 and channel2 are assumed to be two users in this application who are trying to chat.
- In case you are interested to build your own app from scratch
Install jquery inside your application first before getting started
rails plugin install git://github.com/aaronchi/jrails.git
copy the javascript files in the plugin folder to your javascripts directory.
Develop two view html which will actually be chat windows for two different users say channel1 and channel2
Include javascripts library references
<%= javascript_include_tag 'jquery', 'application'%>
subscribe for private channel1_channel2 between users channel1 and channel2
jug.subscribe("/chats/channel1_channel2", function(data){
var li = $("<li/>");
li.text(data);
$("#mesg").append(li);
});
Add ajax enabled form:
<% form_tag('/chat/send', :id => 'chat_window', :remote => true) do %> <%= text_field_tag 'msg_body', '', :size => 50 %> <%= text_field_tag 'sender', 'channel1'%><%= submit_tag 'Send Message' %> <% end %>
In the text_field_tag above sender can be choosen dynamically based on user if in session.
Add code inside controller associated with the view to publish message to server
#chat_controller.rb
require "juggernaut"class ChatController < ApplicationController
def send_message
@messg = params[:msg_body]
@sender = params[:sender]
Juggernaut.publish(select_channel("/channel1_channel2"), parse_chat_message(params[:msg_body], params[:sender]))
respond_to do |format|
format.js
end
end
...
Sub methods’ implementation are present inside code sample. You can anyways implement those in any way you like. Just keep in mind that juggernaut publish API requires channel name as first argument and second argument as message which is to be published to channel provided before. Here you can choose the channel dynamically based on users between whom message is currently being exchanged. Also you can write logic to persist messages, if you need, inside this method of controller.
Now final step would be to send_message.js.erb file with default name ‘send_message’ inside which we will reset the text fiend values
#send_message.js.erb
$("#chat_window")[0].reset();
You can perform various other operation here where in you can update only specific portion/fields of page depending on the user interface you choose for your chat window.
You are now ready to run this application. Please note that intend of current demo application is to just show how to build a private chat app using third party plugins in ruby on rails. You can anyways enhance it based on your usage.
I have listed below few issues which I faced while building this application which are applicable for any general applicatin being developed on ruby on rails 3.0
#1 : even after running ‘sudo gem install juggernaut’, it was showing following error at line: require “juggernaut”, which was present at top of application controller
no such file to load -- juggernaut
Solution: On adding below line inside Gemfile of your project:
gem 'juggernaut', :git => 'git://github.com/maccman/juggernaut.git'
and then running ‘sudo gem install juggernaut’, the issue was gone.
#2 : On running application, javascript console was throwing below error
jQuery is not definedIn my view html, I was calling jquery before application, however in default application layout call to default javascripts library was present which was mesing things up by calling application.js before jQuery was called.
Solution: Either change default javascript libraries setting or remove/change the call for the same inside application layout generated by default.
#3 : Error:
"Property '$' of object [object DOMWindow] is not a function" at line: $(document).ready(function()
Solution: This was coming because I was using jQuery in no conflict [jQuery.noConflict();]. After removing this mode, it worked fine. Though if you want to use jQuery mode in no conflict mode, which is btw recommended, you can check this thread: http://www.sitepoint.com/forums/showthread.php?t=712582
As per it, in “no-confict” mode, the $ shortcut is not available and the longer jQuery is used. To use the default jQuery shortcut of $, you can use the following wrapper around your code:
jQuery(document).ready(function($) {
// $() will work as an alias for jQuery() inside of this function
});
