Google Map和Wordpress:动态创建标记的问题

时间:2011-09-15 11:56:30

标签: php javascript wordpress dynamically-generated

我想在Wordpress中的GoogleMap上创建dinamycally标记。标记是根据后标记(所有位置)计算的。我在计算坐标和创建php数组方面没有问题。当我必须在地图上绘制存储在数组中的动态生成数据时会出现问题,因为没有显示指针

我在WP header.php中指定了以下说明:

  <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=mykey" type="text/javascript"></script>

  <script src="<?php bloginfo('template_directory'); ?>/mapLocations_cache.php" type="text/javascript"></script> 
  <script src="<?php bloginfo('template_directory'); ?>/map_functions.js" type="text/javascript"></script>

动态创建的数组(我保存在mapLocations_cache.php中)具有以下格式:

   var markers = [
   {
  'latitude': 62.3908358,
  'longitude': 17.3069157,
  'title': 'it happens in Sundsvall',
  'news': 'che noia5'
   },
   ];

map_functions.js包含以下代码:

   var centerLatitude = 62.3908358;
   var centerLongitude = 17.3069157;
   var startZoom = 4;

   var map;

   function addMarker(latitude, longitude, description) {
   var marker = new GMarker(new GLatLng(latitude, longitude));

    GEvent.addListener(marker, 'click',
     function() {
        marker.openInfoWindowHtml(description);
     }
   );

   map.addOverlay(marker);
   }

   function init() {
   if (GBrowserIsCompatible()) {    
    map = new GMap2(document.getElementById("map")); 
    map.addControl(new GSmallMapControl());
    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);

    for(id in markers) {
        addMarker(markers[id].latitude, markers[id].longitude, markers[id].title);


    }
    }
    }

    window.onload = init;
    window.onunload = GUnload;

因为当我使用非动态生成的文件/数组时,此代码运行良好,我怀疑当我尝试从WordPress帖子和标签动态收集数据时,标题中包含的JavaScript没有完成。< / p>

任何建议都会有所帮助: - (

干杯

码头

4 个答案:

答案 0 :(得分:0)

 var markers = [
      {
          'latitude': 62.3908358,
          'longitude': 17.3069157,
          'title': 'it happens in Sundsvall',
          'news': 'che noia5'
      },
   ];

你错过了一个收尾撇号。我不确定这是否是问题,但你可以确定检查它并确保它不是。

除此之外,我不太确定,您也可以查看此链接:http://code.google.com/apis/maps/documentation/javascript/

答案 1 :(得分:0)

以下是一些建议:

  1. 确保markers数组中的最后一个元素之后没有','(就像现在的代码中一样)。
  2. 首选for (id = 0; id < markers.length; ++id) {...}迭代数组。
  3. 请确保在function addMarker(lat, lng, ..)中获得正确的数字。您还可以通过以下方式强制转换为数字:var marker = new GMarker(new GLatLng(lat-0, lng-0));
  4. 使用“显示来源”或类似信息在浏览器中显示和检查页面来源。
  5. 将完整的页面源下载到本地计算机并进行检查。
  6. 使用Javascript调试器(例如Firebug)调试代码。

答案 2 :(得分:0)

使用Google地理编码API传递位置本身,而不是使用其他脚本编码long和lat。 Google API可以很好地与城市名称,国家/地区名称和邮政编码相匹配,即使格式不完美,它也相当无懈可击。

答案 3 :(得分:0)

尤里卡!我确实找到了我的错误的解决方案。我没有指定Javacript代码来处理标题中的指针和GoogleMaps,而是将其移动到index.php的底部。这意味着仅当我的Geocoder完成其工作并且指针列表/数组完成时才会处理地图数据!

PhP和Javascript之间的互动对我来说并不完全清楚: - )

码头