如何在谷歌地图中添加多标记

时间:2017-09-07 10:00:41

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

我需要在谷歌地图中添加多个标记(使用googlemap库添加谷歌地图)。我还在此地图中添加了一个标记。  对于单个标记代码:

控制器:

    $data['tunnels'] = $this->functional->getTunnelData();
    $config['center'] = '37.4419, -122.1419';
    $config['zoom'] = 'auto';
    $this->googlemaps->initialize($config);
    $marker = array();
    $marker['position'] = 'center';
    $this->googlemaps->add_marker($marker);
    $data['map'] = $this->googlemaps->create_map();

型号:

    $this->db->select('ult_tunnel.*,ult_country.name as country_name,ult_state.name as state_name,ult_city.name as city_name ')
                    ->join('ult_country','ult_country.id = ult_tunnel.country_id')
                    ->join('ult_state','ult_state.id = ult_tunnel.state_id')
                    ->join('ult_city','ult_city.id = ult_tunnel.city_id')
                    ->from('ult_tunnel')
                    ->where('ult_tunnel.status','Active')
                    ->get()->result();

查看:

    <div class="col-md-12 col-xs-12 col-sm-12 col-lg-12" id="map">
        <?php echo $map['js']; ?>
        <?php echo $map['html']; ?>
    </div>

请帮我在这个googlemap中添加多个标记。 任何帮助将不胜感激。

提前致谢。

编辑:

表格结构: 它包含:

Id(int主键)

名(VARCHAR)

国(int)的

状态(int)的

市(int)的

状态

我想在谷歌地图中标记名称

1 个答案:

答案 0 :(得分:1)

您可以将代码更改为以下解决方案。

  

控制器中的更改:

$data['tunnels'] = $this->functional->getTunnelData();
$config['center'] = '37.4419, -122.1419';
$config['zoom'] = 'auto';
$this->googlemaps->initialize($config);

// First Marker
$marker = array();
$marker['position'] = '37.429, -122.1519';
$marker['infowindow_content'] = 'Welcome Googel Map';
$marker['icon'] = 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|9999FF|000000';
$this->googlemaps->add_marker($marker);

// Second Marker
$marker = array();
$marker['position'] = '37.409, -122.1319';
$marker['draggable'] = TRUE;
$marker['animation'] = 'DROP';
$this->googlemaps->add_marker($marker);

// third Marker
$marker = array();
$marker['position'] = '37.449, -122.1419';
$marker['onclick'] = 'alert("You just clicked on Maker!!")';
$this->googlemaps->add_marker($marker);

// Add Dyanamic Place name In Infowindow content
if(!empty($data['tunnels'])){
    foreach ($data['tunnels'] as $value) {
        $marker = array();
        $marker['position'] = $value->name.",".$value->city_name.",".$value->state_name.",".$value->country_name;
        $marker['infowindow_content'] = $value->name.",".$value->city_name.",".$value->state_name.",".$value->country_name;
        $marker['icon'] = 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|9999FF|000000';
        $this->googlemaps->add_marker($marker);
    }
}

$data['map'] = $this->googlemaps->create_map();

我希望这会对你有所帮助。谢谢!