Google Maps API 3 - 限制平移/地图边界 [英] Google Maps API 3 - limit pan/map bounds

查看:23
本文介绍了Google Maps API 3 - 限制平移/地图边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Google 地图完全陌生,正在创建我的第一张地图,以便可以将其合并到我的网站中.

I am completely new to Google Maps and am just creating my first map so that I can incorporate it into my website.

我试图将用户可以移动的区域限制在英国,并查看了此处的几篇帖子,所有帖子都给出了非常相似的答案,但是我无法让代码对我有用.这个解决方案是我得到的最接近的解决方案每当我尝试移动地图时,它都会以我的边界点之一为中心,而我无法将其移动到其他任何地方.

I am trying to limit the area that the user can move around to just the UK and have looked at several posts on here that all give very similar answers, however I haven't been able to get the code to work for me. This solution is the closest that I have got however whenever I try and move the map at all it centers on one of my boundary points and I am unable to move it anywhere else.

我可能犯了一个非常愚蠢的错误,在这种情况下我道歉,但我无法弄清楚出了什么问题.有没有人有任何想法?

I may have made a really silly mistake, in which case I apologise, however I can't work out what is wrong. Does anyone have any ideas?

谢谢

我的代码如下(取自 Google 的示例代码,然后添加到) - 与边界相关的部分靠近底部,从为英国设置边界开始:

My code is below (taken from Google's sample code and then added to) - the part that is relevant to the bounds is near the bottom, starting with setting the bounds for the UK:

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <title>Google Maps</title>
    <style type="text/css">
        html { height: 100% }
        body { height: 100%; margin: 0; padding: 0 }
        #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
        src="https://maps.googleapis.com/maps/api/js?key=MYKEY&sensor=false">
    </script>

    <?
    //code to get long and lat from http://www.webmonkey.com/2010/02/get_started_with_google_geocoding_via_http
    $apikey = MYKEY;
    $geourl = "http://maps.google.com/maps/geo?q=LS255AA&output=csv&key=$apikey";

    // Create cUrl object to grab XML content using $geourl
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, $geourl);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    $csvContent = trim(curl_exec($c));
    curl_close($c);

    // Split pieces of data by the comma that separates them
    list($httpcode, $elev, $lat, $long) = split(",", $csvContent);
    ?>

    <script type="text/javascript">
    var lat = "<?= $lat ?>";
    var long = "<?= $long ?>";
    </script>

    <script type="text/javascript">
        function initialize() {
            //sets the long and lat of map
            var myLatlng = new google.maps.LatLng(lat, long);

            //options for the map
            var myOptions = {
                center: myLatlng,
                zoom: 9,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            //creates the map
            var mymap = new google.maps.Map(document.getElementById("map_canvas"),
                myOptions);

            //sets min and max zoom values
            var opt = { minZoom: 7, maxZoom: 11 };
                mymap.setOptions(opt);

            //creates marker
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: mymap,
                title:"Hello World!"
            });

            //content of infowindow
            var contentString = '<h2>I am an info window</h2>'+'<p>Hello my name is infowindow, I need more text to test how big I get</p>';

            //creates infowindow
            var infowindow = new google.maps.InfoWindow({
                    content: contentString,
            });

            //infowindow options
            infowindow.setOptions({maxWidth:200}); 

            //listens for click and opens infowindow
            google.maps.event.addListener(marker, 'click', function() {
                 infowindow.open(mymap,marker);
            });
            // Bounds for UK
            var strictBounds = new google.maps.LatLngBounds(
                    new google.maps.LatLng(60.88770, -0.83496), 
                    new google.maps.LatLng(49.90878, -7.69042)
            );

            // Listen for the dragend event
            google.maps.event.addListener(mymap, 'dragend', function() {
                if (strictBounds.contains(mymap.getCenter())) return;

                // We're out of bounds - Move the map back within the bounds
                var c = mymap.getCenter(),
                x = c.lng(),
                y = c.lat(),
                maxX = strictBounds.getNorthEast().lng(),
                maxY = strictBounds.getNorthEast().lat(),
                minX = strictBounds.getSouthWest().lng(),
                minY = strictBounds.getSouthWest().lat();

                if (x < minX) x = minX;
                if (x > maxX) x = maxX;
                if (y < minY) y = minY;
                if (y > maxY) y = maxY;

                mymap.setCenter(new google.maps.LatLng(y, x));
            });
        }
    </script>
</head>
<body onload="initialize()">
    <div id="map_canvas" style="width:50%; height:50%"></div>

</body>
</html>

推荐答案

您的 strictBounds 混淆了 - 更改它们的顺序,它应该可以正常工作.

You have your strictBounds mixed up - change the order of them and it should work fine.

A LatLngBounds 应该是 SW 角第一,NE 角第二:http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLngBounds

A LatLngBounds should be SW corner first, NE corner second: http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLngBounds

var strictBounds = new google.maps.LatLngBounds(
  new google.maps.LatLng(49.90878, -7.69042),
  new google.maps.LatLng(60.88770, -0.83496) 
);

这篇关于Google Maps API 3 - 限制平移/地图边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆