2012-07-24 38 views
4

我正在寻找简单地添加谷歌地图使用谷歌地图API到我的网页在WordPress中的一个。有没有一种简单的方法来简单地复制和粘贴“Hello,World”谷歌地图代码以在地图上显示地图?WordPress的Google Map API没有插件?

谢谢

回答

9

是的,没有必要为这样的东西插件。首先,您将在header.php中包含Google地图脚本,或者您可以将其排入队列;

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script> 

然后我通常添加在的header.php以下 - 这增加条件代码为仅包含地图的页面的标签(在该情况下页374);

<body <?php if (is_page(374)) { echo 'onload="initialize()" onunload="GUnload()"'; } ?>> 

然后,我将创建联系人页面自定义模板(因为这是该页面的地图是正常的),并在模板该页面包含类似于下面的东西。是的,对于代码示例可能有点长,但我只是给你一个真实的例子,其中包含一个可点击的动画标记来显示客户的地址。您可以更改内联尺寸以适合您的设计,并且还可以偏移地图,使标记在中间不正确。

<div id="contact-content"> 

      <script type="text/javascript"> 
      function initialize() { 

       var leeds = new google.maps.LatLng(53.80583, -1.548903); 

       var firstLatlng = new google.maps.LatLng(53.80583, -1.548903);    

       var firstOptions = { 
        zoom: 16, 
        center: firstLatlng, 
        mapTypeId: google.maps.MapTypeId.ROADMAP 
       }; 

       var map = new google.maps.Map(document.getElementById("map_leeds"), firstOptions); 

       firstmarker = new google.maps.Marker({ 
        map:map, 
        draggable:false, 
        animation: google.maps.Animation.DROP, 
        title: 'Your Client', 
        position: leeds 
       }); 

       var contentString1 = '<p>The Address<br />Of your client<br />in<br />here</p>'; 


       var infowindow1 = new google.maps.InfoWindow({ 
        content: contentString1 
       }); 

       google.maps.event.addListener(firstmarker, 'click', function() { 
        infowindow1.open(map,firstmarker); 
       }); 

      } 
      </script> 

      <div class="map"> 

       <div id="map_leeds" style="width: 600px; height: 600px"></div> 

      </div> 

     </div> 

如果别人确实是不同的,更好的方法然后我会敏锐地看到它,但我已经使用这个网站上的负载和它的作品真的很好。

+0

非常感谢你,非常有帮助 – 2012-07-24 19:51:04

+0

我的荣幸 - 很高兴你发现它有帮助:) – McNab 2012-07-24 20:02:15