如何使用多个参数正确生成URL

时间:2016-02-22 12:09:34

标签: codeigniter google-maps

如何在我当前的代码中生成URL,因此我可以将值传递给我的路线功能。

这是我在每个公司的地图上显示标记的代码:

$n = 0;
   foreach ($firm as $f) {
     if ($f->active == 1 && in_array($f->skd_n, $dejavnost) && $f->lat != 0) {
          $lat_f = $f->lat;
          $lng_f = $f->lng;
          $distance = (($this->distance($lat, $lng, $lat_f, $lng_f)) * 1000);
             if ($distance <= $rad && $n <= 199) {
                $marker = array();
                $marker['position'] = "$lat_f, $lng_f";
                  if ($f->phone != 0) {
                     $marker['infowindow_content'] = '<div class="info_window">' . "$f->title" . '<br/>' .
                     '<div class="pin_icon"></div>' .
                     "$f->address" . '<br/>' .
                     '<div class="phone_icon"></div>' .
                     "$f->phone" . '<br/>' .
                     //generate URL with 4 arguments
                     '<a href="?????">DIRECTION</a>' .
                     '</div><br/>';      
                  }else{
                     $marker['infowindow_content'] = '<div class="info_window">' . "$f->title" . '<br/>' .
                     '<div class="pin_icon"></div>' .
                     "$f->address" . '</div>';
                  }
                 $marker['animation'] = 'DROP';
                 $marker['zIndex'] = '0';
                 $marker['icon'] = '../images/pin-map-red.png';
                 $this->googlemaps->add_marker($marker);
                 $n++;
              }
       }
   }

我想将锚添加到下一个函数,其中问号是并且使用变量(坐标)作为参数:

function direction($lat, $lng, $lat_f, $lng_f){
   $config['places'] = TRUE;
   $config['placesAutocompleteInputID'] = 'event_location';
   $config['placesAutocompleteBoundsMap'] = TRUE;
   $config['zoom'] = 'auto';
   $config['center'] = 'auto';
   $config['directions'] = TRUE;
   $config['directionsStart'] = "$lat, $lng";
   $config['directionsEnd'] = "$lat_f, $lng_f";
   $config['directionsDivID'] = 'directions';
   $this->googlemaps->initialize($config);
   $data['map'] = $this->googlemaps->create_map();
   $this->load->view('header', $data);
   $this->load->view('domov_view', $data);
   $this->load->view('footer'); 

}

如何从第一个函数(方向)开始使用值? 谢谢!

2 个答案:

答案 0 :(得分:1)

您可以在url中传递参数,并通过$ _GET方法

在方向方法中获取它们

实施例: 您可以生成类似的链接

<a href="site_url()."direction"."?lat=".$lat."&lng=".$lng."&lat_f=".$lat_f."&lng_f=".$lng_f">DIRECTION</a>

所以你的功能看起来像这样:

$n = 0;
foreach ($firm as $f) {
    if ($f->active == 1 && in_array($f->skd_n, $dejavnost) && $f->lat != 0) {
        $lat_f = $f->lat;
        $lng_f = $f->lng;
        $distance = (($this->distance($lat, $lng, $lat_f, $lng_f)) * 1000);
        if ($distance <= $rad && $n <= 199) {
            $marker = array();
            $marker['position'] = "$lat_f, $lng_f";
            if ($f->phone != 0) {
                $marker['infowindow_content'] = '<div class="info_window">' . "$f->title" . '<br/>' .
                '<div class="pin_icon"></div>' .
                "$f->address" . '<br/>' .
                '<div class="phone_icon"></div>' .
                "$f->phone" . '<br/>' .
                '<a href="'.site_url()."direction"."?lat=".$lat."&lng=".$lng."&lat_f=".$lat_f."&lng_f=".$lng_f.'">DIRECTION</a>' .
                '</div><br/>';
            }else{
                $marker['infowindow_content'] = '<div class="info_window">' . "$f->title" . '<br/>' .
                '<div class="pin_icon"></div>' .
                "$f->address" . '</div>';
            }
            $marker['animation'] = 'DROP';
            $marker['zIndex'] = '0';
            $marker['icon'] = '../images/pin-map-red.png';
            $this->googlemaps->add_marker($marker);
            $n++;
        }
    }
}

在方向函数

中以这种方式获取它们
function direction(){
   $lat = $this->input->get('lat');
   $lng = $this->input->get('lng');
   $lat_f = $this->input->get('lat_f');
   $lng_f = $this->input->get('lng_f');
   $config['places'] = TRUE;
   $config['placesAutocompleteInputID'] = 'event_location';
   $config['placesAutocompleteBoundsMap'] = TRUE;
   $config['zoom'] = 'auto';
   $config['center'] = 'auto';
   $config['directions'] = TRUE;
   $config['directionsStart'] = "$lat, $lng";
   $config['directionsEnd'] = "$lat_f, $lng_f";
   $config['directionsDivID'] = 'directions';
   $this->googlemaps->initialize($config);
   $data['map'] = $this->googlemaps->create_map();
   $this->load->view('header', $data);
   $this->load->view('domov_view', $data);
   $this->load->view('footer');
}

P.S。:如果您通过网址发送经度和经度,您可能会遇到通过网址(点(。))发送不允许的字符的问题。 您可以在配置文件

中配置允许的URL字符
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_=+-' 

答案 1 :(得分:0)

您可以扩展网址助手。添加一些功能:

create_url('controller/action', [
    'getParam1' => 'value1',
    'getParam2' => 'value2',
    'getParam3' => 'value3',
]);

它会生成 http://base.url/controller/action?getParam1=value&getParam2=value2&getParam3=value3

或使用

http://www.codeigniter.com/userguide2/libraries/uri.html

创建链接$ this-&gt; uri-&gt; assoc_to_uri()

解析链接$ this-&gt; uri-&gt; uri_to_assoc()