Make map marker direct link onclick for gmaps4rails
For this kind of needs, I pass a block to the gmaps4rails
method in the controller (doc here):
@json = User.all.to_gmaps4rails do |user, marker|
marker.json "\"id\": #{user.id}"
# or
marker.json "\"link\": #{method_to_create_link}"
end
This way I can have any additional information I need to create a link or anything.
That said, you could update your listener this way:
base_url = "whatever you need";
google.maps.event.addListener(marker, 'click', function(){
window.location(base_url + marker.id);
// or
window.location(marker.link);
});
Gmaps4rails : How to change appearance of marker when user clicks link in sidebar?
You've a javascript issue. Actually, you can't make a closure with a changing counter: it will always be referenced with it's last value.
Gmaps.map.callback = function(){
for(var i = 0; i < Gmaps.map.markers.length; i++){
marker = Gmaps.map.markers[i];
google.maps.event.addListener(marker.serviceObject, 'click',
(function(marker){
return function(){
console.log($(marker.serviceObject.ne.ga).attr("src", "/assets/marker_sprite2.png"));
}
})(marker)
)
}
}
Your main issue is a bit long, not complex but long. The idea is the following:
add an
id
to each marker, you could use the block argument of theto_gmaps4rails
method and add some more jsoncreate the sidebar yourself and add the id to each line to know which marker you want when you click the li
loop all markers to get the one with the proper id
change it's picture
How to get information from the currently clicked marker in gmaps4rails?
You have to add a listener on click:
var handler = Gmaps.build('Google');
handler.buildMap({ provider: { }, internal: {id: 'map'}}, function(){
// I assume this is the way you retrieve the amrkers array
var json_data = <%=raw @hash.to_json %>;
var markers = handler.addMarkers(json_data);
_.each(json_data, function(json, index){
var marker = markers[index];
json.marker = marker;
google.maps.event.addListener(marker.getServiceObject(), 'click', function(){
console.log(json);
//whatever you need here
});
});
});
Gmaps4rails get marker detail when click on particular marker on map in ruby on rails4
all your json is in <%=raw @vnote_hash.to_json %>
So:
var raw_json = <%=raw @vnote_hash.to_json %>;
markers = handler.addMarkers(raw_json);
And in your loop:
raw_json[i].vnote_id
An alternative is to merge object properties so you only use one object.
GoogleMaps4Rails not centering first marker
do this in the buildMap
callback
google.maps.event.trigger(markers[0].getServiceObject(), 'click');
What is the standard way in gmaps4rails for dynamically resetting marker icons / pictures?
I resolved this issue by using the replaceMarkers API method. Here's the code that worked:
var replacementMarkers = new Array();
for (var k = 0; k < activeMarkers.length; ++k) {
var marker = activeMarkers[k];
var iconUrl = null;
if ($.inArray(marker, taskMarkers) >= 0) {
iconUrl = "/assets/purple_MarkerT.png";
} else {
iconUrl = "/assets/green_MarkerS.png";
}
replacementMarkers.push({
"lat": marker.lat.toString(),
"lng": marker.lng.toString(),
"picture": iconUrl,
"id": marker.id,
"description": marker.description
});
}
// Delay the replacement operation because in some instances, we reach this branch via the Gmaps.map.callback.
// In that case, I prefer to defer touching the gmap4rails until the current branch is done.
window.setTimeout(function() {
Gmaps.map.replaceMarkers(replacementMarkers);
resetAddressMarkers();
}, 25);
gmaps4rails : HOW do I set map options such as container ID for a directions map?
You only want a single map right?
In this case:
<%= gmaps( :map_options => { :container_class => "foo", :id => "bar", :class => "baz" },
"direction" => { "data" => { "from" => "Paris, france", "to" => "Toulon, france" }}
) %>
Related Topics
How to Display Error Messages in a Multi-Model Form with Transaction
How to Display a Link to Individual Microposts? (Ruby on Rails 3)
Assets Precompiling Error with Jquery UI Plugin
How to Sign Out in a Rails App, Using Devise Gem, No Route Matches /Users/Sign_Out
How to Extract Specific Elements from an Array
Delayedjob: "Job Failed to Load: Uninitialized Constant Syck::Syck"
With Nokogiri I am Getting Error "Initialize': Getaddrinfo: No Such Host Is Known. (Socketerror)"
When Passing a Ruby Array as an Argument, Why Does '<<' Append While '+=' Does Not
Ruby on Rails - Error Making New App (Rb:55:In 'Require')
Ruby Implementation Win32API Get Mouse Scroll/Wheel Input
Selenium Webdriver & Chrome Driver - Not Able to Run Chrome Driver
Deprecation Warning on Save with Ruby and Active_Record
What Is the Ruby Regex for Including Apostrophes
Windows/Ruby Background Processing
Chef: Undefined Node Attribute or Method '<<' on 'Node' When Trying to Add
Implicit User Creation with Authlogic and Authlogic Oauth Plugin