2010-09-14 54 views
4

我在设置基于地址的标记的网站上有Google地图。Google Maps API未将标记居中

下面是一个例子(点击位置选项卡):http://www.weddinghouse.com.au/wedding-directory/zoning-in-personal-training/

正如你可以看到有在地图上没有标记。但是,如果向上滚动,标记就坐在视线之外。

我的代码有问题吗?奇怪的事情是很少地址实际显示正确,但大多数没有。我的代码有问题吗?还是Google?

这里是我的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); 
      } 
     }); 
    } 
} 
+0

我看到一个标记标记在地图的中心......不关闭屏幕? – Ken 2010-09-14 01:02:38

+0

也适合我。 – RedBlueThing 2010-09-14 01:12:27

回答

5

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

这也有一个错误,有时标记不居中,特别是在隐藏的div。解决这个问题的最佳方法是将Google地图的iFrame放入单独的文件(例如pages/map.html)中,然后在页面中将iFrame的源代码放入。

EG-代替IFRAME SRC =“maps.google.com/等等等等” 它作为SRC =“页/ map.html”

无论如何,这个职位是6个月大,但比晚好决不!

0

对我来说,问题是当地图加载时,iframe的大小为0(display:hidden)。我添加了一个延迟,加载了iframe后加载地图。这种竞争条件可以解释为什么某些地址可以正确显示。

也许试试这个:

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

(感谢https://groups.google.com/forum/#!topic/google-maps-api/nN0y2pSr2dQ