在协调员的谷歌地图中删除多个引脚的问题

时间:2015-06-01 08:48:53

标签: php codeigniter google-maps google-maps-markers

我想在谷歌地图中删除多个带有动态值的图钉,所以下面是我的代码。

查看

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

控制器

function index(){

 $address ="Gondal Road, Dr. Yagnik Road Corner, Near Malaviya Petrol Pump,      Rajkot, Gujarat 360001";// Google HQ
 $prepAddr = str_replace(' ','+',$address);
 $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
 $output= json_decode($geocode);
 $lat = $output->results[0]->geometry->location->lat;
 $long = $output->results[0]->geometry->location->lng;

 $this->load->library('googlemaps');

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

 $marker = array();
 $marker['position'] = $lat.','.$long;
 $marker['infowindow_content'] = $address;
 $marker['icon'] = 'http://chart.apis.google.com/chart? chst=d_map_pin_letter&chld=A|9999FF|000000';
 $this->googlemaps->add_marker($marker);

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

$marker = array();
$marker['position'] = '37.449, -122.1419';
$marker['onclick'] = 'alert("You just clicked me!!")';
$this->googlemaps->add_marker($marker);
$data['map'] = $this->googlemaps->create_map();



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

}

当我运行上面的代码时,只有单个引脚掉线但我想要多个具有动态值的引脚,那么我怎样才能使它成为可能呢?你的所有建议都会很明显。

1 个答案:

答案 0 :(得分:1)

使用以下代码替换您的代码:

<强>控制器:

    $address[ ]=Your multiple address  //store your multiple address in array
    foreach($address as $add)
    {

        $prepAddr = str_replace(' ','+',$add);
        $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
        $output= json_decode($geocode);
        $lat = $output->results[0]->geometry->location->lat;
        $long = $output->results[0]->geometry->location->lng;

        $marker = array();
        $marker['position'] = $lat.','.$long;
        $marker['infowindow_content'] = '$add';
        $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();
    $this->load->view('test',$data);

您的观点完美无需改变。