简体   繁体   English

Google Maps v3-放大标记时出错

[英]Google maps v3 - Error when zooming to markers

I am trying to load a google maps v3 element into my website, where a user can dynamically select some filters to show certain markers. 我正在尝试将google maps v3元素加载到我的网站中,用户可以在其中动态选择一些过滤器以显示某些标记。 The result is then serialized into a JSON file for the maps object to use to set the specified markers. 然后将结果序列化为JSON文件,以供maps对象用来设置指定的标记。

I've tried to make a test page as follows: 我尝试制作如下测试页:

@section headerScripts {
    <script src="~/Scripts/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>

    <script type="text/javascript">
        var map;
        var latlng = new google.maps.LatLng(56.29216, 10.34912);
        var markerList = [];

        function initialize() {
            var mapOptions = {
                zoom: 7,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
        }

        function addMarkers(stores) {
            for (var i = 0; i < stores.length; i++) {
                var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(stores[i].Latitude, stores[i].Longitude),
                    map: map,
                    title: stores[i].Name
                });
                markerList.push(marker);
            }
            zoomToMarkers();
        }

        function zoomToMarkers() {
            var latlngbounds = new google.maps.LatLngBounds();
            markerList.forEach(function (n) {
                latlngbounds.extend(n);
            });
            map.setCenter(latlngbounds.getCenter());
            map.fitBounds(latlngbounds);
        }

        $(document).ready(function () {
            initialize();
            $.getJSON("/Product/GetStore/", addMarkers);
        });

    </script>
}

<div id="map-canvas" style="width: 500px; height: 500px;"></div>

It sets the markers alright, and that works, but I'd like a function to zoom, so that only the markers on the maps is shown in the "zoom". 它可以正确设置标记,并且可以,但是我想要一个缩放功能,以便“缩放”中仅显示地图上的标记。 But when I call the zoomToMarkers (based on a function I found online) - I get an error from the google maps js file: 但是当我调用zoomToMarkers时(基于我在网上找到的函数)-我从google maps js文件中收到错误:

Unhandled exception at line 16, column 353 in https://maps.gstatic.com/intl/da_dk/mapfiles/api-3/12/9/main.js 0x800a01b6 - Microsoft JScript runtime error: Object doesn't support property or method 'lat' 第16行第https://maps.gstatic.com/intl/da_dk/mapfiles/api-3/12/9/main.js 0x800a01b6中未处理的异常-Microsoft JScript运行时错误:对象不支持属性或方法“ lat”

I have no idea why this is happening, as it seems that alot of other people have gotten it to work. 我不知道为什么会这样,因为似乎很多其他人已经开始使用它了。 Can you guys help? 你们可以帮忙吗?

I've found the error myself. 我自己发现了错误。 Problem was that in this line 问题是在这条线

latlngbounds.extend(n);

I was added the marker object to the latlngbounds instead of the coordinates. 我将标记对象添加到latlngbounds而不是坐标。 So I corrected it to this: 因此,我对此进行了更正:

function addMarkers(stores) {
    for (var i = 0; i < stores.length; i++) {
        var marker_latlng = new google.maps.LatLng(stores[i].Latitude, stores[i].Longitude);
        var marker = new google.maps.Marker({
            position: marker_latlng,
            map: map,
            title: stores[i].Name
        });
        latlngbounds.extend(marker_latlng);
    }
    zoomToMarkers();
}

function zoomToMarkers() {
    map.setCenter(latlngbounds.getCenter());
    map.fitBounds(latlngbounds);
}

Which works perfectly! 哪个完美!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM