开发者

printing multiple markup on google map using CI

开发者 https://www.devze.com 2023-03-30 11:58 出处:网络
i have lat lng saved in my DB. I want to place markup on those particular places. But my markup only appears on the lat lng which i defined in the google map file (saved in the library). When i echo t

i have lat lng saved in my DB. I want to place markup on those particular places. But my markup only appears on the lat lng which i defined in the google map file (saved in the library). When i echo the fetched lat lng on view page it only shows the 1st saved lat lng a开发者_运维问答nd the markup doesn't appear on that place.

Controller:

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);
}

Model:

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;
}

View:

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


Looks like you're using my CodeIgniter Google Maps Library. The problem you face is the order that you are doing things in. Try doing something like so: (untested)

Controller:

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);

}

Model:

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;
}

View:

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

Notice how the lat/lng's are looped through in the controller. Foreach iteration we call the add_marker function to add each individual marker.

Hope that helps! Check out the demos too as they may help clear things up :)

0

精彩评论

暂无评论...
验证码 换一张
取 消