【问题标题】:Pass Latitude and Longitude from controller to view with Googlemaps Codeigniter从控制器传递纬度和经度以使用 Googlemaps Codeigniter 进行查看
【发布时间】:2019-11-30 20:40:39
【问题描述】:

我试图从我的控制器的可拖动标记中获取 lat nad lng,

我的控制器功能

public function addnew(){

        // Load the library
        $this->load->library('googlemaps');
        // Load our model
        $this->load->model('Sedes_model', '', TRUE);

        // Initialize the map, passing through any parameters
        $config['center'] = '-17.3718, -66.1615';
        $config['zoom'] = "15";
        $this->googlemaps->initialize($config);
        // Get the co-ordinates from the database using our model
        $sedes = $this->Sedes_model->getCoordanites();
        // Loop through the coordinates we obtained above and add them to the map
        foreach ($sedes as $row) {
            $marker = array();
            $marker['position'] = $row->location;
            $this->googlemaps->add_marker($marker);

        }

        $marker = array();
        $marker['position'] = '-17.3718, -66.1615';
        $marker['draggable'] = true;
        $marker['ondragend'] = 'alert(\'You just dropped me at: \' + event.latLng.lat() + \', \' + event.latLng.lng());';
        $this->googlemaps->add_marker($marker);


        // Create the map
        $data = array();
        $data['map'] = $this->googlemaps->create_map();

        print_r($data['map']['markers']);


        // Load our view, passing through the map data
        $this->load->view('sedes/add_sede_view', $data);


    }

有了这个,我得到了我的地图,上面有我的数据库中的一些标记,还有一个可拖动的标记。

我试图从标记中获取 lat 和 lng 到我视图中的输入值,现在我在警报框中得到坐标。我想我必须为此使用 javascript,但我不太了解它

非常感谢您对此提供的任何帮助。

【问题讨论】:

    标签: javascript php codeigniter


    【解决方案1】:

    您可以使用 $this->input->get('input_name'); 将文本输入(或任何表单元素)中的值传递给控制器或 $this->input->post('input_name');,取决于您在表单中使用的方法。

    在您的控制器中,您应该将 $marker['ondraged'] 更新为:

    $marker['ondragend'] = "document.getElementById('position').value = event.latLng.lat() + \', \' + event.latLng.lng() +';";
    

    所以,例如你有一个表单

    <form method="post" action="<?php echo base_url('update_data'); ?>">
    <input type="hidden" id="position" name="latlng" value="" />
    <input type="submit" value="send" />
    </form>
    

    您可以通过以下方式接收控制器方法中的数据

    public function update_data() {
     // use here the $this->input->post('latlng') as needed
        if($_POST) {
            $data = array(
                "latlng" => $this->input->post('latlng')
            );
            $this->load->view('map', $data);
        }   
    }
    

    代码未经测试,希望它有效;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 2012-09-02
      • 1970-01-01
      相关资源
      最近更新 更多