Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for 'dragend' event in v3 API #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,41 @@ Here is another example with custom icons + clustering
@map.markers << marker2
</code></pre>

Here is an example of handling a marker being dropped.

In your controller...
<pre><code>
@map = Cartographer::Gmap.new( 'map' )
@map.zoom = :bound
@map.icons &lt;&lt; Cartographer::Gicon.new
marker1 = Cartographer::Gmarker.new(:name=&gt; &quot;taj_mahal&quot;, :marker_type =&gt; &quot;Building&quot;,
:position =&gt; [27.173006,78.042086],
:draggable =&gt; true, # make it draggable
:drag_end =&gt; 'handleMarkerDrop(mouseEvent.latLng);', # javascript code to handle the event.
:info_window_url =&gt; &quot;/url_for_info_content&quot;)
marker2 = Cartographer::Gmarker.new(:name=&gt; &quot;raj_bhawan&quot;, :marker_type =&gt; &quot;Building&quot;,
:position =&gt; [28.614309,77.201353],
:info_window_url =&gt; &quot;/url_for_info_content&quot;)

@map.markers &lt;&lt; marker1
@map.markers &lt;&lt; marker2
</code></pre>

In your view...
<pre><code>
# for Rails 3+ you need to make use of 'raw'
&lt;%= raw Cartographer::Header.new.to_s %&gt;
&lt;%= raw @map.to_html %&gt;
&lt;div style=&quot;width:600px;height:400px;&quot; id=&quot;map&quot; &gt; [Map] &lt;/div&gt;

&lt;script type="text/javascript"&gt;
function handleMarkerDrop(latLng){
alert('You moved marker to LAT: '+latLng.lat()+' and LNG: '+latLng.lng()+'!');
}
&lt;/script&gt;
</code></pre>


Adsense for Maps
----------------

Expand Down
7 changes: 6 additions & 1 deletion lib/v3/cartographer/gmarker.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Cartographer::Gmarker
#include Reloadable
attr_accessor :name, :marker_type, :highlight, :icon, :position, :click, :info_window, :info_window_url, :map, :min_zoom, :max_zoom, :dblclick, :draggable
attr_accessor :name, :marker_type, :highlight, :icon, :position, :click, :info_window, :info_window_url, :map, :min_zoom, :max_zoom, :dblclick, :draggable, :drag_end

def initialize(options = {})
@name = options[:name] || "marker"
Expand All @@ -9,6 +9,7 @@ def initialize(options = {})
@icon = options[:icon] || Cartographer::Gicon.new
@click = options[:click] # javascript to execute on click
@dblclick = options[:dblclick] # javascript to execute on double click
@drag_end = options[:drag_end] # javascript to execute on dragend event
@info_window = options[:info_window] # html to pop up on click
@info_window_url = options[:info_window_url] # html to pop up on click fetched from a URL
@map = options[:map]
Expand Down Expand Up @@ -75,6 +76,10 @@ def to_js(marker_mgr_flag = false, marker_clusterer_flag = false)
script << "google.maps.event.addListener(#{name}, 'dblclick', function() {#{@dblclick}});\n"
end

if @drag_end
script << "google.maps.event.addListener(#{name}, 'dragend', function(mouseEvent) {#{@drag_end}});\n"
end

script << " // Add the marker to a new overlay on the map" if @debug
script << " #{@name}.setMap(#{@map.dom_id});\n" if self.highlight || (!marker_mgr && !marker_clusterer)
return @debug? script.join("\n ") : script.join.gsub(/\s+/, ' ')
Expand Down
10 changes: 10 additions & 0 deletions spec/gmarker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@

end

it "should accept configuration for dragend event" do
marker = Cartographer::Gmarker.new(:name=> "org11", :marker_type => "Organization",
:position => [ 36.031332, -21.093750],
:drag_end => "alert(\"DragEnd Works!\");",
:icon => @icon,
:map=>@map)
marker.to_js().should include("google.maps.event.addListener(org11, 'dragend', function(mouseEvent) {alert(\"DragEnd Works!\");});")

end

it "should accept configuration for info window url" do
marker = Cartographer::Gmarker.new(:name=> "org11", :marker_type => "Organization",
:position => [ 36.031332, -21.093750],
Expand Down