google-maps - Google Maps API 未将标记居中

标签 google-maps

我在一个基于地址设置标记的网站上有谷歌地图。

这是一个示例(单击位置选项卡):http://www.weddinghouse.com.au/wedding-directory/zoning-in-personal-training/

如您所见, map 上没有标记。但是,如果您向上滚动,标记就在视线之外。

我的代码有问题吗?奇怪的是,很少有地址能正确显示,但大多数地址不能正确显示。我的代码有问题还是谷歌?

这是我的 JavaScript 代码:

<script type="text/javascript">
$(document).ready(function(){
    load('Zoning In Personal Training', '27 Sitella Drive, berwick, VIC, 3806');
});
</script>

-
function load(title, address, type) {
    if (GBrowserIsCompatible()) {
        var map;
        var geocoder;

        map_id = document.getElementById("map");
        map = new GMap2(map_id);
        map.addControl(new GSmallMapControl());
        map.setCenter(new GLatLng(24, 0), 17);
        map.enableDoubleClickZoom();

        if (type == 'sat') {
            map.setMapType(G_SATELLITE_MAP);
            map.addControl(new GHierarchicalMapTypeControl());
        } else {
            map.setMapType(G_NORMAL_MAP);
        }

        geocoder = new GClientGeocoder();
        geocoder.getLocations(address, function (response) {
            map.clearOverlays();
            if (!response || response.Status.code != 200) {
                //map_id.innerHTML('Could not find address on Google Maps');
            } else {
                place = response.Placemark[0];
                point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

                map.setCenter(point, 17);

                // Create our "tiny" marker icon
                var icon = new GIcon();
                icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
                icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
                icon.iconSize = new GSize(12, 20);
                icon.shadowSize = new GSize(22, 20);
                icon.iconAnchor = new GPoint(6, 20);
                icon.infoWindowAnchor = new GPoint(5, 1);

                // Creates one of our tiny markers at the given point
                function createMarker(point, index) {
                  var marker = new GMarker(point, icon);
                  var myMarkerContent = "<div style=\"width:200px; overflow:auto;\"><strong>" + title + "</strong><br />" + address + "</div>";
                  map.addOverlay(marker);
                  marker.openInfoWindowHtml(myMarkerContent);
                  GEvent.addListener(marker,"click",function() {
                     marker.openInfoWindowHtml(myMarkerContent);
                   });
                }
                createMarker(point);
            }
        });
    }
}

最佳答案

如果您在谷歌地图中加载页面,然后单击“链接”功能,它会为您提供将网站作为 iFrame 嵌入的代码,而不是搞乱 API - 您可以试试这个吗?

还有一个错误,有时标记不居中,特别是在隐藏的 div 上。我最好的解决方法是将谷歌地图的 iFrame 放在一个单独的文件中(例如 pages/map.html),然后将其作为页面中 iFrame 的来源。

例如-而不是 iFrame src="maps.google.com/etc etc"
将其设置为 src="pages/map.html"

无论如何,这篇文章已有 6 个月的历史,但迟到总比没有好!

关于google-maps - Google Maps API 未将标记居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3705336/

相关文章:

javascript - 如何制作 Google Maps API v3 六边形平铺 map ,最好是基于坐标的 map ?

php - 如何使用坐标将标记移动 100 米

php - 使用 Php 的谷歌地图显示不工作

javascript - 谷歌地图/JS : How with a list of street address can we populate a Google Map?

javascript - 使用 google api 地址更新 jQuery jqgrid 列

android - 带有 IntentChooser 的导航应用程序——这是同一个查询吗?

iphone - 使用 Google Maps for iOS 检索多个标记的位置、标题和片段

api - 使用Google API-GClientGeocoder()

ios - Google Places 添加的地方未显示

javascript - 谷歌地图绘制两点之间的路线