0

我正在将 Google Maps 实施到一个新项目中,并且一页应该返回加拿大的大 (100% * 100%) 地图。我还使用地理编码来确定要传递给地图的 latlng。

这是代码:

var myLatlng = new google.maps.LatLng(37.775, -122.4183333);
        var myOptions ={
            zoom: 5,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.TERRAIN
        }

        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        var address = "Canada";
        var geocoder = new google.maps.Geocoder(address);
        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                //map.setCenter(results[0].geometry.location);
                            //The above was too far north
                map.fitBounds(results[0].geometry.viewport);
                map.setZoom(5);
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });

它工作得很好,只是它“向左有点太远了”。从本质上讲,它切断了大部分海洋省份。可以在这里看到一个例子:

http://50.28.69.176/~sequoia/locations.php

任何人都知道如何巧妙地解决这个问题,以便国家更好地集中在视口中?

谢谢你的帮助。

4

1 回答 1

1

我建议不要在每次加载页面时都进行地理编码,而是获取LatLng您想要的正确信息并存储它。

如果您仍然想坚持这种方法,您可以使用getCenter()a 上的方法LatLngBounds来获取它的中心。

这样,您可以调用

map.fitBounds(results[0].geometry.viewport.getCenter())
map.setZoom(5)
于 2012-02-08T14:21:05.400 回答