googlemaps codeigniter未在视图中显示

时间:2018-04-26 04:46:55

标签: php codeigniter google-maps-api-3

我在codeigniter网站上显示地图时遇到问题。我使用的是googlemaps.php库,我的控制器和视图如下所示:

控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Maps extends My_Controller {

public function __construct(){
    parent::__construct();
    $this->load->model('Maps');

}

public function maps(){
    $this->load->library('googlemaps');
    $mapmarkers = $this->Maps->find(); //From here get lan. lat. 

    foreach($mapmarkers as $m){
        $marker = array();
        $marker['position'] = $m['location'];
        $content = "My Information";
        $marker['infowindow_content'] = $content ;
        $this->googlemaps->add_marker($marker);
    }

    $data['map'] = $this->googlemaps->create_map();
    //var_dump($data);
    $this->load_theme('maps', $data);
}
}

查看:

<div class="map-content content-area container-fluid">
<div class="col-lg-12">
    <div class="row">
        <?php echo $map['js']; ?>
        <?php echo $map['html']; ?>
    </div>
</div>

结果是一个空白页面,但是当我转储变量map时,它会显示正确的数据。 我在控制台中也没有任何错误。

var_dump log:

array(1) { ["map"]=> array(3) { ["js"]=> string(1439) " " ["html"]=> string(61) "
 " ["markers"]=> array(1) { ["marker_0"]=> array(2) { ["latitude"]=> 
 string(17) "48.28866326125372" ["longitude"]=> string(17) 
 "16.37960056416341" } } } } 

有人可以告诉我我的代码有什么问题,或者我如何解决它

1 个答案:

答案 0 :(得分:0)

你不是initialize库,更新你的功能如下

public function maps(){
    $this->load->library('googlemaps');
    $this->googlemaps->initialize();

    $mapmarkers = $this->Maps->find(); //From here get lan. lat. 
    foreach($mapmarkers as $m){
        $marker = array();
        $marker['position'] = $m['location'];
        $marker['infowindow_content'] = 'My Information';
        $this->googlemaps->add_marker($marker);
    }

    $data['map'] = $this->googlemaps->create_map();
    //var_dump($data);
    $this->load_theme('maps', $data);
}

<强> HTML

<head>
<?=$map['js'];?>
</head>

<body>
<?=$map['html'];?>
</body>