如何使用jquery gmap3从点击链接显示infowindow

时间:2014-04-03 18:31:02

标签: jquery jquery-gmap3

我正在使用jquery gmap3来显示带有标记的地图,这些标记用于从我构建的搜索中找到的每个结果。每个标记都有一个infowindow,显示标记所在位置的详细信息,当有人单击地图上的标记时,infowindow会显示该信息。在地图下方,我显示了找到的结果列表,对于找到的每个结果,我想要做的是有一个链接,当有人点击该链接时,页面滚动到地图然后居中到结果所属的标记和infowindow会自动弹出。那可能吗?这是我的代码:

  var base_lat = 33.609063;
  var base_lon = -112.105135;
  var base_markers = [{"latLng":["33.609063","-112.105135"],"data":"<div class=\"estate_info\"><img src=\"\/img\/realestate\/test_estate.jpg\"><b><a href=\"\/charming-payette-lake-cabin-with-private-dock-beach-rl2\">1022 E. St. Mill Valley, CA 85282<\/a><\/b><ul><li><label>Beds:<\/label>6<\/li><li><label>Baths:<\/label>3.0<\/li><li><label>Levels:<\/label>2<\/li><\/ul><ul><li><label>Price:<\/label>$520,000<\/li><li><li><label>Sqft:<\/label>5,600<\/li><li><a href=\"\/charming-payette-lake-cabin-with-private-dock-beach-rl2\">View Details &raquo;<\/a><\/li><\/ul><\/div>","options":{"icon":"\/img\/markers\/number_1.png"}},{"latLng":["33.479913","-111.699535"],"data":"<div class=\"estate_info\"><img src=\"\/img\/realestate\/default_estate.jpg\"><b><a href=\"\/single-level-mountain-view-home-in-central-laveen-rl1\">South of Baseline Rd \/ 51st Ave Laveen, CA 85384<\/a><\/b><ul><li><label>Beds:<\/label>3<\/li><li><label>Baths:<\/label>2.5<\/li><li><label>Levels:<\/label>1<\/li><\/ul><ul><li><label>Price:<\/label>$124,563<\/li><li><li><label>Sqft:<\/label>2,500<\/li><li><a href=\"\/single-level-mountain-view-home-in-central-laveen-rl1\">View Details &raquo;<\/a><\/li><\/ul><\/div>","options":{"icon":"\/img\/markers\/number_2.png"}}];

        $('#searchmap').gmap3({

        map:{

            options:{

                center:[base_lat,base_lon],
                zoom: 9,
                mapTypeControl: true,
                mapTypeControlOptions: {
                  style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
                },
                navigationControl: true,
                scrollwheel: true,
                streetViewControl: true
            }

        },

        marker:{

            values: base_markers,

            options:{

                draggable: false

            },

            events:{

                click: function(marker, event, context) {

                    var map = $(this).gmap3("get"),

                    infowindow = $(this).gmap3({get:{name:"infowindow"}});

                    if (infowindow) {

                        infowindow.open(map, marker);
                        infowindow.setContent(context.data);

                    }else {

                        $(this).gmap3({

                            infowindow:{

                                anchor:marker,
                                options:{content: context.data}

                            }

                        });

                    }

                }

            }

        }

    });

以下是我在地图下显示的结果:

    <ul>
<li class="listing first">
                                       <div class="rphoto"><a href="#" style="background-image:url('/img/realestate/test_estate.jpg');"></a></div>
                                       <div class="details">
                                         <strong>1. <a href="#">Charming Payette Lake Cabin with private dock & beach</a></strong>
                                         <span>Type: Condo | Built: 1995 | Status: Backup or Contingent</span>
                                         <div class="info">This charming cabin is located on Payette Lake with private dock and beach. It offers 2 Bedroom and 1 Bath. Sleeps 8 with 2 Doubles, 1 bunk and 1 queen hideabed...<nav>Mill Valley, CA | Price: $520,000 | <a href="#">View Details &raquo;</a></nav></div>
                                         <ol>
                                           <li>6<br>Bed</li>
                                           <li>3.0<br>Bath</li>
                                           <li class="last">5,600<br>Sqft</li>
                                         </ol>
                                       </div>
                                      </li>
<li class="listing last">
                                       <div class="rphoto"><a href="#" style="background-image:url('/img/realestate/default_estate.jpg');"></a></div>
                                       <div class="details">
                                         <strong>2. <a href="#">Single-level mountain-view home in central Laveen</a></strong>
                                         <span>Type: House | Built: 2003 | Status: Active</span>
                                         <div class="info">With 3,200 square feet of living area and located just one lot back from the ocean, makes childhood dreams of fantastic sandcastles come true - and you can be one of the...<nav>Laveen, CA | Price: $124,563 | <a href="#">View Details &raquo;</a></nav></div>
                                         <ol>
                                           <li>3<br>Bed</li>
                                           <li>2.5<br>Bath</li>
                                           <li class="last">2,500<br>Sqft</li>
                                         </ol>
                                       </div>
                                      </li>
</ul>

再次在搜索结果中,如果他们点击其中一个结果的链接然后将地图居中于该标记所在的位置,那么我怎么能拥有它呢?所以示例第二个结果将居中于地图上的第二个标记然后信息窗口会自动显示。我怎么能使用jquery gmap3代码呢?这是我所指的一个例子:

http://koti.mbnet.fi/ojalesa/boundsbox/makemarker_sidebar_plain.htm

1 个答案:

答案 0 :(得分:0)

好的,我能够搞清楚。这是我添加的jquery代码:

    $(".propertyclick").click(function() {

        //We first scroll to top of page
        $("html,body").animate({ scrollTop: 0 },"fast");

        //We then get the current id of the marker being clicked
        var id = $(this).attr('data-marker');

        //We now get the marker in the map that they want to see
        var marker = $("#estate_searchmap").gmap3({ get: { id: id } });

        //We now simulate that marker being clicked via the map
        google.maps.event.trigger(marker,'click');

        return false;

    });

在列表结果中,我添加了一个链接供他们点击以触发此操作:

<a href="" class="propertyclick" data-marker="property1"></a>

data-marker保存我在原始代码base_markers中的json值中为每个标记分配的id。所以现在你会在每个标记中看到一个新值,例如id:&#34; property1&#34;然后下一个将是id:&#34; property2&#34;等等。这就是我在上面的点击功能中引用的方式,以了解要调用的标记。