【问题标题】:CakePHP and GoogleMapsHelper: How do I load markers from my DB?CakePHP 和 GoogleMapsHelper:如何从我的数据库中加载标记?
【发布时间】:2013-12-28 17:00:11
【问题描述】:

我正在使用 GoogleMapsHelper 和 CakePHP 在我的应用程序中包含一个 Google 地图。

为了给我的地图添加标记,文档说我应该使用以下语法,其中三个变量是地图 ID、标记 ID 和标记位置。

<?= $this->GoogleMap->addMarker("map_canvas", 1, array('latitude' => 40.69847, 'longitude' => -73.9514)); ?>

考虑到这一点,我一直在尝试遍历我的数据库并显示我的所有观点,但它没有做任何事情:这是代码,每个帖子都有 (lat) 和 (lng)。

<?php 
    foreach ($posts as $post): ?>

        <?php $this->GoogleMap->addMarker("map_canvas", $post['Post']['id'], array('latitude' => $post['Post']['lat'], 'latitude' => $post['Post']['lng'])); ?>

<?php endforeach; ?>

如何将这些条目作为标记加载到我的地图中? 提前致谢!

【问题讨论】:

  • 更新版本可能是this

标签: php google-maps cakephp google-maps-api-3 cakephp-2.4


【解决方案1】:

您只是忘记回显标记。注意示例中的短标签&lt;?=。这是做&lt;?php echo 的简短(而且有些丑陋)的方式。此外,没有必要像您那样打开/关闭 PHP 标记。它可以放在一个块中。最后,您在选项数组中声明了一个重复键 latitude,从 Post 模型的字段名称来看,您可能是指 longitude。综上所述,这应该可以解决问题:

<?php
foreach ($posts as $post):
    echo $this->GoogleMap->addMarker(
        'map_canvas',
        $post['Post']['id'],
        array(
            'latitude' => $post['Post']['lat'],
            'longitude' => $post['Post']['lng']
        )
    );
endforeach;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2015-01-23
    • 2012-07-24
    • 1970-01-01
    相关资源
    最近更新 更多