Google Map API - Mouse Wheel Zooming while Preventing Page Scroll

ASP.NET, Google Map API, Ruby on Rails No Comments »

Since version 2.78 the Google Map API exposes mouse wheel scroll zooming straight out of the box, obviating the need for hacks and custom programming to get this essential user experience to fly.

Mouse wheel zooming is enable by simply adding

map.enableScrollWheelZoom();

after the code that creates your map.

However, the API code will not disable the scrolling of the page so scrolling / zooming inside the map will also scroll the page up or down. This is detrimental to the user experience for all maps that are on a page that does not fit the browser window.

Esa has created the most solid workaround I’ve found, and this solution is also suggested by Google.

Here are implementations of Esa’s solution for two of the leading web development environments.

ASP.NET

For almost a year now I’ve used Reimers’ Google Maps .NET control that can be used with ASP.NET, wrapping most of the JavaScript classes and greatly simplifying inserting maps into ASP.NET pages.

Note: I use an older version of the control so the below code might not work with newer versions (it should though)

Code to prevent the page from scrolling, implementing Esa’s code with Reimer’s Google Maps .NET control:

bool preventPageScroll = true;
if (preventPageScroll)

            {

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            sb.Append (@”function preventPageScroll(e){if (!e){e = window.event}if (e.preventDefault){e.preventDefault()}e.returnValue = false;};);

                //Firefox et al.

            sb.Append(String.Format(GEvent.addDomListener(document.getElementById(\”{0}\”), \”DOMMouseScroll\”, preventPageScroll);, GoogleMap1.ClientID));

            sb.Append(System.Environment.NewLine);

                //IE

            sb.Append(String.Format({0}.onmousewheel = preventPageScroll;, GoogleMap1.ClientID));

            GoogleMap1.PostRenderScript += sb.ToString();

            }

document.getElementById looks redundant here but I couldn’t get it to work any other way.

Ruby on Rails

If you’re planning to use the Google Map API from Ruby on Rails you can’t pass by the fantastic ym4r_gm plugin by Guilhem Vellut.

Be sure to also walk through the Google Maps / Yahoo Traffic mash-up. The plugin supports mouse wheel zooming directly. You can enable it by simply adding

@map.interface_init (:scroll_wheel_zoom => true)

in the controller like so (line 7):

 1 class TrafficController < ApplicationController

 2 

 3   def index

 4     @map = GMap.new(map_div)

 5     @map.control_init(:large_map => true,:map_type => true)

 6     @map.center_zoom_init([38.134557,-95.537109],4)

 7     @map.interface_init(:continuous_zoom => true, :scroll_wheel_zoom => true, :prevent_pagescroll => true)

 8     @map.icon_global_init(GIcon.new(:image => /images/icon_incident.png, :icon_size => GSize.new(15,15),:icon_anchor => GPoint.new(7,7),:info_window_anchor => GPoint.new(9,2)),icon_incident)

 9     @map.icon_global_init(GIcon.new(:image => /images/icon_construction.png, :icon_size => GSize.new(15,15),:icon_anchor => GPoint.new(7,7),:info_window_anchor => GPoint.new(9,2)),icon_construction)

10 

11   end

You can prevent the page from scrolling when using the mouse wheel zooming taking these two simple steps (using Esa’s code):

1. Add the following function to the public>javascripts>ym4r-gm.js JavaScript file:

function preventPageScroll(e)

{

if (!e){

e = window.event

}

if (e.preventDefault){

e.preventDefault()

}

e.returnValue = false;

}

2. Add the following code to the interface_init method in vendor>plugins>ym4r_gm>lib>gm_plugin>map.rb

if !interface[:prevent_pagescroll].nil?

          if interface[:prevent_pagescroll]

            @init << #{@container}.onmousewheel = preventPageScroll;

            @init << GEvent.addDomListener(#{@container}, \”DOMMouseScroll\”,preventPageScroll);\n”

          end

        end

Now simply set :prevent_pagescroll => true in the controller as in the sample code above. I’m quite new to Ruby and Rails and there certainly might be a better way to implement this but the code works. Hope it helps.

Update: Due to a WordPress bug the backslashes to escape the double-quotes around “DOMMouseScroll” in and before the newline (n) are not visible in the above code snippets. You will have to add them manually before I upgrade my Wordpress install.
Update 2: WordPress updated. Backslashes should show now.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

100% View but no Order (at least not in SQL Server 2005)

SQL No Comments »

In SQL Server 2000 you could add ORDER BY to the query defining a view that started with SELECT TOP (100) PERCENT… which is the default query when you create a view with SQL Server Enterprise Manager for SQL Server 2000.

The TOP statement would pretend to limit the resultset (nothing is actually limited when 100 percent are returned) and ORDER BY could be applied. However, things changed with SQL Server 2005. The query optimizer now removes both TOP and ORDER BY clauses if the query is for the TOP 100 PERCENT. From SQL Server Books Online:

When ORDER BY is used in the definition of a view, inline function, derived table, or subquery, the clause is used only to determine the rows returned by the TOP clause. The ORDER BY clause does not guarantee ordered results when these constructs are queried, unless ORDER BY is also specified in the query itself.

This means that each (outer) query issued against the view must contain the ORDER BY statement to guarantee an ordered resultset. There is absolutely no warning in the query designer if you verify the syntax of your view:

image

 

 

 

 

 

Microsoft recently released a hotfix for legacy applications relying on the deprecated behaviour which remains unsupported.

Here’s an in-depth view (no pun intended) of this issue and a workaround/hack (issuing TOP (Very Large Integer)).

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

IDE Time Savers - Quickly Locate and Open Project Files

Programming, Ruby on Rails, Simplify Computing No Comments »

One of the greatest time-wasters using Visual Studio is navigating the Solution Explorer looking for files to open or edit. With multiple projects and more files and folders for each project it just gets worse and worse. So Chris O’Sullivan created this little Visual Studio Plugin to quickly find files to open and edit without leaving the keyboard. Lars Engel has improved it to work with Visual Studio 2003 (and 2005). Be sure to follow the installation instructions on Chris’  site.

The free DPack Package offers this functionality as well (besides many other goodies) with a version for Visual Studio 2008 (aka Orcas) in the pipeline and the benefit of enabling the selection of multiple files.

Of course Netbeans has this functionality built-in (Navigate>Go to file…, or Alt-Shift-O) in a fast and functional implementation which also lets you open multiple files in one go. In Eclipse/Aptana/RadRails it’s Navigate>Go to>Resource or Alt-Shift-R on the keyboard with only single files selectable. Hope it helps.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Netbeans IDE 6.0 Beta 1 Available for Download

Programming, Ruby on Rails No Comments »

Not sure if it’s actually released or not but if you’re looking for a snappy and complete IDE for Ruby on Rails you should try Netbeans 6.0 in the Ruby flavor, a mere 19 MB download (well, you’ll need a JDK too). The joy of RoR development is greatly enhanced by such an intelligent IDE where Ruby and Rails take center stage instead of being added as an afterthought as with some competing offerings. It’s amazing that the developer team was able to iron out quite a number of bugs in the two weeks since I downloaded a nightly built. Other people like it too.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

MSN Messenger Alternative to Beat the Windows Lock-In

Simplify Computing No Comments »

Starting MSN Messenger (7.0) today the following came up:
A newer version is available. You must install the newer version in order to continue. 

Clicking the What’s New… button brings up the following page in Firefox:

image
Yep, there is exactly nothing on this page except for some eyecandy (No content was disallowed or blocked on my side).
So not only is Microsoft blackmailing users as part of their Windows lock-in strategy, they also fail at creating a simple working web page to inform me why I should fall for that lock-in. And since I had tried that atrociously slow bloatware Live Messenger once before, I certainly needed some more convincing.

So I went to scavenge the web for MSN Messenger alternatives. The service itself is indispensable in Thailand as most users don’t bother to install any other IM client except maybe for Yahoo which is known to be very spam-infested here. I know about Gaim for Linux but there are other cross-platform clients. I came across aMSN repeatedly so I installed it. It’s Tk based so quite a bit slower than MSN Messenger and not quite as slick-looking but after using it for a couple of hours I can say that it works fine. Anyway, I’m not a fool for eye candy so I would say aMSN is a viable alternative, especially if you’re planning to move away from Windows in the future and want to get used to another client.

And then another thing happened:

After logging out of my aMSN connection I could now connect with my MSN Messenger 7.0 just like before. So much for “You must…”. As for Windows Live, kudos to MS for the Live Writer but I keep my hands off all the rest.

Update: Above “workaround” stopped working the next day and the annoying message reappeared. So I had to get rid of MSN Messenger for good.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in