简体   繁体   English

Google Maps停留在左上角-桌面解决方案不适用于移动设计

[英]Google Maps stuck on top-left corner - solution for Desktop doesn't work for mobile design

I have had a problem with Google Maps where I am rendering the map inside a hidden DIV element. 我在使用隐藏的DIV元素渲染地图时遇到了Google Maps问题。 The map rendered but only the left top corner was showing, even after I move the map. 渲染了地图,但即使我移动了地图,也仅显示了左上角。

The solution to this as I found was to wait with rendering the map, and the points until user actually displays the map. 我发现的解决方案是等待渲染地图,然后点直到用户实际显示地图。 I do this by having a interval counter iterating every half second to see if user clicked the button to open/display the element. 我通过使间隔计数器每半秒迭代一次来执行此操作,以查看用户是否单击了按钮以打开/显示元素。

This works on desktop browser, but it doesn't solve the same problem for the mobile browsers. 这适用于桌面浏览器,但不能解决移动浏览器的相同问题。 The corner is still stuck, and I'm not sure how to handle this from the mobile perspective. 角落仍然卡住,我不确定如何从移动角度处理该问题。

<script type="text/javascript">
    var locations = [SOME DATA];

    var map;
    var bounds;

    function initialize() {
        var myLatlng = new google.maps.LatLng(0, 0);
        var mapOptions = {
            zoom: 5,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
        bounds = new google.maps.LatLngBounds();

        for (var i = 0; i < locations.length; i++) {
            var entity = locations[i];
            var myLatLng = new google.maps.LatLng(entity[6], entity[7]);

            var marker = new google.maps.Marker({
                position: myLatLng,
                map: map,
                icon: pinBase + pin
            });

            if(entity[8] < 5) {
                bounds.extend(marker.position);
            }

            var infowindow = new google.maps.InfoWindow({});

            if(entity[5] == "0") {
                entity[5] = "N/A";
            }

            contentString = "<div class=\"mapBaloon\"><b>" + entity[0] + "</b><br />WWW: <a href=\"http://" + entity[2] + "\">http://" + entity[2] + "</a><br />City: " + entity[3] + "<br />State: " + entity[4] + "<br />Phone: " + entity[5] + "<br />Distance: " + entity[8] + " miles</div>";
            bindInfoWindow(marker, map, infowindow, contentString);
        }

        map.fitBounds(bounds);
    }

    function bindInfoWindow(marker, map, infowindow, html) {
        google.maps.event.addListener(marker, 'click', function() {
            infowindow.setContent(html);
            infowindow.open(map, marker);
        });
    } 

    var myVar = setInterval(function() {
        google.maps.event.addDomListener(window, 'load', initialize);

        if($(".commArea2").is(":visible")) {
            clearInterval(myVar);
            google.maps.event.trigger(map, 'resize');
            map.fitBounds(bounds);
        }
    }, 500);         
</script>

I had a similar issue for mobile use. 我在移动设备上也遇到了类似的问题。 I called the initialize() function when the div was triggered to display. 当div触发显示时,我调用了initialize()函数。

$('#open_btn').bind('touchstart', function(event){
    initialize();
})

Off the top of my head there are a few possible causes which could lead to this. 在我头顶上方,有几种可能的原因可能导致这种情况。 Because it worked on the desktop, and not mobile, I would suspect you've got a race condition. 因为它可以在台式机上工作,而不是在移动设备上工作,所以我怀疑您遇到了竞争状况。 The desktop is faster, it is going to load the Google Maps Javascript faster. 桌面速度更快,它将更快地加载Google Maps Javascript。 Your delay of 500 milliseconds doesn't sound like enough of a time to wait on a mobile device. 您的500毫秒的延迟听起来不足以等待移动设备上的时间。 BUT, you don't raise the setInterval. 但是,您不会提高setInterval。 Just get rid of it. 摆脱它。

Google provides the ability to add a callback to your " Google提供了向您的“

https://developers.google.com/maps/documentation/javascript/examples/map-simple-async https://developers.google.com/maps/documentation/javascript/examples/map-simple-async

The other issue I've encountered is that you really have to make sure there is an explicit width / height on the map before doing anything. 我遇到的另一个问题是,在执行任何操作之前,您确实必须确保地图上有明确的宽度/高度。 Either setting the width/height properties, doing a "position:absolute" with all top/left/bottom/right defined, or something else. 设置width / height属性,执行“ position:absolute”,定义所有顶部/左侧/底部/右侧,或其他方式。 But if Maps API can't figure out the explicit width of the parent element, you will get a nice box in the corner. 但是,如果Maps API无法弄清父元素的显式宽度,您将在角落看到一个漂亮的框。 But like you said, it works in one place so likely this isn't the issue. 但是就像您说的那样,它在一个地方起作用,因此这可能不是问题。 Just make sure you're to using something in HTML or CSS that isn't supported on your mobile browser. 只需确保您要使用移动浏览器不支持的HTML或CSS格式。

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

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