Polygon infowindows in Gmaps4Rails

淺唱寂寞╮ 提交于 2019-12-11 00:56:08

问题


I'm using gmap4rails to draw a bunch of fairly complicated polygons that serve as district boundaries. I've got two things i would like help with:

Firstly, I want to associate an info window on click with these polygons. This is an example of my use-case: https://google-developers.appspot.com/maps/documentation/javascript/examples/polygon-arrays

I've tried registering a click event handler in the gmap's callback but it doesn't work and I don't think it's the right approach.

Gmaps.map.callback = function() 
{
        console.log("'sup");
        var infowindow = new google.maps.InfoWindow
        ({
            content: 'you clicked me!',
            suppressMapPan:true
        });
        google.maps.event.addListener(Gmaps.map.polygons[0], 'click', function() 
        {
            console.log("the click event fired");
            infowindow.open(map, Gmaps.map.polygons[0]);
        });

}

Secondly, I'd like to be able to change the fill-color of these polygons via jquery on some user-events (user clicks a checkbox for example). How would I go about that using the gem?


回答1:


Replace your method with:

Gmaps.map.callback = function() 
{
    console.log("'sup");
    Gmaps.map.polygons[0].infowindow = new google.maps.InfoWindow
    ({
        content: 'you clicked me!'
    });

    google.maps.event.addListener(Gmaps.map.polygons[0].serviceObject, 'click', function(event) 
    {
        console.log("the click event fired");
        infowindow = Gmaps.map.polygons[0].infowindow;
        infowindow.setPosition(event.latLng);
        infowindow.open(Gmaps.map.map);
    });

}

And change this gem's js line to put true instead of false. I did set to false since I didn't like the cursor change. Anyway it should be in configuration options. Please create an issue on github so that I remember to fix it.



来源:https://stackoverflow.com/questions/10170883/polygon-infowindows-in-gmaps4rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!