user409858
user409858

Reputation: 1201

Triggering Click On Google Map API3 Markers From A Link Elsewhere In The Page

I have been reading the many similar threads and cannot see how to make this work in my code... any advice would be appreciated.

I have numerous markers set via AJAX and I create a table below my map from data returned in the JSON results. I want to make a link clickable in my datatable that will emulate a click on the corresponding marker on the map and open the info window already defined for an actual click on the marker...

function display( json_results ) {

        $("#map").gmap3({action:'clear'});

        $("#map").gmap3(
            {action: 'init',
              options:{
                center:true,
                zoom:13,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: true,
                mapTypeControlOptions: {
                        style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
                },
                navigationControl: true,
                scrollwheel: true,
                streetViewControl: true
                }
            },
            {action: 'addMarkers',
                radius:100,
                markers: json_results,
                clusters:{
                            maxZoom: 10,
                            // This style will be used for clusters with more than 0 markers
                            20: {
                            content: '<div class="cluster cluster-1">CLUSTER_COUNT</div>',
                            width: 53,
                            height: 52
                            },
                            // This style will be used for clusters with more than 20 markers
                            50: {
                            content: '<div class="cluster cluster-2">CLUSTER_COUNT</div>',
                            width: 56,
                            height: 55
                            },
                            // This style will be used for clusters with more than 50 markers
                            100: {
                            content: '<div class="cluster cluster-3">CLUSTER_COUNT</div>',
                            width: 66,
                            height: 65
                            }

                    },
                marker: {
                    options: {
                        //icon: new google.maps.MarkerImage('http://maps.gstatic.com/mapfiles/icon_green.png'),
                        clickable: true
                        },
                    events:{
                        click: function(marker,event,data) {
                            $(this).gmap3({action: 'clear', name : 'infowindow'});
                            $(this).gmap3({action: 'addinfowindow', anchor: marker, options: { content:
                            '<div class="text"><strong><div style="color:navy;">' + data.itype + '</strong><br/><div id="address" snum="' + data.streetnum + '" snam="' + data.streetnam + '" styp="' + data.streettyp + '">'+ data.iaddress +'</div><br/>' + data.inum + '<br/>'+ data.datetime +'</div><hr>'+data.notes+'</div>'} })
                        },  
                        mouseover: function(marker, event, data){
                            $(this).gmap3(
                                { action:'clear', name:'overlay'},
                                { action:'addOverlay',
                                    latLng: marker.getPosition(),
                                    content:    '<div class="infobulle">' +
                                                '<div class="bg"></div>' +
                                                '<div class="text">' + data.itype +'</div>' +
                                                '</div>' +
                                                '<div class="arrow"></div>',
                                    offset: {
                                        x:-46,
                                        y:-73
                                    }
                                });
                        },
                        mouseout: function(){
                            $(this).gmap3({action:'clear', name:'overlay'});
                            }

                    } //end events
                } // end marker

                }
                ,{action:"autofit"} //end action

                );
            };

I call this function from some JQUERY when the page is loaded and when a form with search results gets submitted. All works perfectly. Now I want to add a link outside the map that will trigger the click on the corresponding marker...

For example: <a href="javascript:google.maps.event.trigger(markers[i], "click")">See This Infowindow</a> where i would be a value that I pass in my JSON at the same time I pass the lat/long and infowindow data to the function above. I assume that the 1st array of data to get sent for mapping would be 0, the second one would be 1 etc, so I would make i=0 for the first link, i=1 for the second etc...

Not sure if that logic makes sense, maybe there is a better way to pass the reference to the marker...

Can anyone help me out with this? Maybe a simply function where I can pass the marker's value into my existing code? Or whatever you think would be the best way...

Thanks gurus!

Upvotes: 0

Views: 2728

Answers (1)

Felix
Felix

Reputation: 141

Did you try defining an own function for handling the clicks?

function myclick(i) {
    google.maps.event.trigger(markers[i],"click");
  }

and

 function setHTML() {
    var html = "";
    for (var i=0; i<markers.length; i++) {
        html += '<a href="javascript:myclick(' + i + ')">' + marker[i].id + '<\/a>';
    }
    document.getElementById("table").innerHTML = html;
  }

Upvotes: 1

Related Questions