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

发布于 2024-12-04 20:51:05 字数 1809 浏览 0 评论 0 原文

我想在 Wordpress 中的 GoogleMap 上创建动态标记。标记是根据帖子标签(都是位置)计算得出的。我在计算坐标和创建 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;

由于当我使用不是动态生成的文件/数组时,此代码运行良好,我怀疑当我尝试执行以下操作时,标头中包含的 JavaScript 未正确完成从 WordPress 帖子中动态收集数据并标签。

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

干杯

码头

I would like to create markers dinamycally on a GoogleMap in Wordpress. The markers are computed from the post tags (which are all locations). I have no problem in computing coordinates, and creating a php array. The problem comes when I have to plot the dynamically generated data stored in the array on the map, because no pointers are displayed

I specified the following instructions in WP header.php:

  <script src="http://maps.google.com/maps?file=api&v=2&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>

The dynamically created array (that I save in mapLocations_cache.php) has the following format:

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

The map_functions.js contains the following code:

   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;

Since when I use a file/array that is NOT dynamically generated, this code works well, my suspicion is that the JavaScript included in the header is not completed appropriate when I try to harvest data dynamically from WordPress posts and tags.

Any suggestion would help :-(

Cheers

Marina

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

や三分注定 2024-12-11 20:51:05
 var markers = [
      {
          'latitude': 62.3908358,
          'longitude': 17.3069157,
          'title': 'it happens in Sundsvall',
          'news': 'che noia5'
      },
   ];

您缺少一个结束撇号。我不确定这是否是问题所在,但您可以检查一下并确保不是问题。

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

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

You were missing a closing apostrophe. I'm not sure if this is the problem but you can sure check it and make sure that it wasn't.

Other than that I'm not really sure, you can check this link out too though: http://code.google.com/apis/maps/documentation/javascript/

蓝咒 2024-12-11 20:51:05

以下是一些建议:

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

Here are some suggestions:

  1. Be sure that there is no ',' after the last element in markers array (as it is in your code now).
  2. Prefer for (id = 0; id < markers.length; ++id) {...} to iterate over an array.
  3. Be sure that you get proper numbers in function addMarker(lat, lng, ..). You can also force casting to numbers by: var marker = new GMarker(new GLatLng(lat-0, lng-0));.
  4. Display and check the source of the page in the browser using 'Show source' or similar.
  5. Download the complete page source to your local computer and check it.
  6. Use a Javascript debugger (e.g. Firebug) to debug your code.
狼性发作 2024-12-11 20:51:05

使用 Google 地理编码 API 传递位置本身,而不是使用另一个脚本编码经度和纬度。 Google API 可以很好地处理城市名称、国家/地区名称和邮政编码,即使格式不完美,也相当安全。

Use the Google Geocoding API to pass in the location itself rather then coding long and lat with another script. Google API plays well with city names, country names, and postal codes, and is fairly bulletproof even if the formatting isn't perfect.

失眠症患者 2024-12-11 20:51:05

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

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

Marina

Eureka! I did find the solution to my error. Instead of specifying the Javacript code that takes care of the pointers and GoogleMaps in the header, I moved it to the bottom of the index.php. This means that the map data will be processed only when my Geocoder has finished its job and the pointer list/array is complete!

The interaction between PhP and Javascript was not completely clear to me :-)

Marina

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文