【问题标题】:printing multiple markup on google map using CI使用 CI 在谷歌地图上打印多个标记
【发布时间】:2011-08-22 19:15:21
【问题描述】:

我的数据库中保存了 lat lng。我想在那些特定的地方做标记。但是我的标记只出现在我在谷歌地图文件(保存在库中)中定义的纬度 lng 上。当我在视图页面上回显获取的 lat lng 时,它只显示第一个保存的 lat lng,并且标记不会出现在那个地方。

控制器

function index()
{
    $this->googlemaps->initialize();
    $marker = array();
    $this->main_model->get_map();

    $marker['position '] = $this->main_model->get_map();
    $data['r'] = $marker['position '];
    $this->googlemaps->add_marker($marker);
    $data['map'] = $this->googlemaps->create_map();

    $this->load->view('main_view',$data);
}

型号

public function get_map()
{
    $this->db->select ('lat,lng');
    $sql = $this->db->get('info');
    if ($sql->num_rows () >0)
    {
        foreach($sql->result() as $row) {
        $data[] = $row;
    }
    return $data;
}

查看

<? foreach($r as $row): ?>
    <?echo $r[0]->lat; ?>
    <?php echo $map['html']; ?>
<?php endforeach; ?>

【问题讨论】:

    标签: codeigniter google-maps


    【解决方案1】:

    看起来你正在使用我的CodeIgniter Google Maps Library。您面临的问题是您做事的顺序。尝试这样做:(未经测试)

    控制器:

    function index() {
    
        // initialize the map
        $this->googlemaps->initialize();
    
        // get the markers, loop through the markers and add them to the map
        $infoMarkers = $this->main_model->get_map();
        foreach ($infoMarkers as $infoMarker) {
            $marker = array();
            $marker['position'] = $infoMarker->lat.",".$infoMarker->lng;
            $this->googlemaps->add_marker($marker);
        }
    
        // create the map with added markers
        $data['map'] = $this->googlemaps->create_map();
    
        // load your view
        $this->load->view('main_view',$data);
    
    }
    

    型号:

    public function get_map()
    {
    
        $data = array();
    
        $this->db->select ('lat,lng');
        $sql = $this->db->get('info');
        if ($sql->num_rows () >0)
        {
            foreach($sql->result() as $row) {
                $data[] = $row;
            }
        }
        return $data;
    }
    

    查看:

    <html>
    <head>
        <?php echo $map['js']; ?>
    </head>
    <body>
        <?php echo $map['html']; ?>
    </body>
    </html>
    

    注意 lat/lng 是如何在控制器中循环的。对于每次迭代,我们调用 add_marker 函数来添加每个单独的标记。

    希望对您有所帮助! Check out the demos 也可以帮助解决问题:)

    【讨论】:

    • 还有一点我想说的是,您的 Ci Google 地图库非常棒。它非常易于理解和使用。:)
    • 非常感谢!很高兴它有一些用处
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多