0

我正在尝试将多个 Google 地图附加到一个页面。但我好像遇到了一些麻烦。

这将是我用来(使用 Handlebars.js )多次创建相同块的模板,大约 50 次:

<script type="text/x-handlebars-template">
{{#each productListing}}
    <div class="product-listing-wrapper">
        <div class="product-listing">
            <div class="left-side-content">
                <div class="thumb-wrapper" data-image-link="{{ThumbnailUrl}}">
                    <i class="thumb">
                        <img src="{{ThumbnailUrl}}" alt="Thumb">
                        <span class="zoom-image"></span>
                    </i>
                </div>
                <div class="google-maps-wrapper">
                    <div class="google-coordonates-wrapper">
                        <div class="google-coordonates">
                            <p>{{LatLon.Lat}}</p>
                            <p>{{LatLon.Lon}}</p>
                        </div>
                    </div>
                    <div class="google-maps-button">
                        <a class="google-maps" href="#">Google Maps</a>
                    </div>
                </div>
            </div>
            <div class="right-side-content">
                <div class="map-canvas-wrapper">
                    <div id="map-canvas" class="map-canvas" data-latitude="{{LatLon.Lat}}" data-longitude="{{LatLon.Lon}}"></div>
                </div>
                <div class="content-wrapper"></div>
            </div>
        </div>
    </div>
{{/each}}

我正在尝试将地图附加到#map-canvasid。使用以下代码块,我正在绘制:

Cluster.prototype.initiate_map_assembling = function() {
    return $(this.map_canvas_wrapper_class).each(function(index, element) {
        var canvas = $(element).children();
        var latitude = $(canvas).attr('data-latitude');
        var longitude = $(canvas).attr('data-longitude');

        var coordinates = new google.maps.LatLng(latitude, longitude);

        var options = {
            zoom: 9,
            center: coordinates,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map($(canvas), options);

        var marker = new google.maps.Marker({
            position: coordinates,
            map: map
        });
    });     
};

这样,我将“循环”通过我试图将地图附加到的 id 的所有父类,但地图只会附加到第一个 id。我试图以其他方式将其附加到所有 id,但结果相同。

那么你会建议我怎么做才能让它像我期望的那样工作,将地图附加到每个 id 上?

4

2 回答 2

1

假设您正在使用像 jQuery 这样的库,然后更改

var map = new google.maps.Map($(canvas), options);

var map = new google.maps.Map($(canvas)[0], options);

这可能会起作用,否则指向您的演示的链接会有所帮助。

于 2012-06-19T21:11:25.423 回答
1

在 HTML 中,您不能ID对多个节点使用相同的。(实际上你可以,但你不能选择第一个之后的那些。)所以一个简单的解决方案来了:只需省略idcanvas 的div,或者在它之后追加index

于 2012-06-19T20:51:36.427 回答