javascript - 循环遍历 MVCArray 以将纬度和经度保存在数据库中

标签 javascript php codeigniter google-maps

我正在尝试保存 Google map 上使用绘图管理器绘制的多边形的坐标。但我在循环经度和纬度时遇到问题。我知道插入被触发,因为它正在从父表插入最新的主键,但坐标(纬度和经度)不会同时插入。这些字段只是保持“空”。这是我的尝试,我希望我能很好地解释它。

查看(addgeofence.php)

google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) {
    all_overlays.push(event);

    if (event.type != google.maps.drawing.OverlayType.MARKER) {
        drawingManager.setOptions({
            drawingMode: null,
            drawingControl: false
        });
        polygonArray = event.overlay.getPath().getArray();   
    }
});

function submit()
{
    if(!Array.isArray(polygonArray) || !polygonArray.length){
        $.smallBox({
            title : "Input Alert",
            content : "<i class='fa fa-clock-o'></i> <i>There is no Polygon!</i>",
            color : "#C46A69",
            iconSmall : "fa fa-times fa-2x fadeInRight animated",
            timeout : 8000
        });
    }
    else
    {
        var stringified = JSON.stringify(polygonArray);
        formData.append('stringified', stringified);

        $.ajax({
        contentType: 'multipart/form-data',
        url: base_url+'geofence/save_geofence',
        type: "POST",
        data: formData,
        processData:false,
        contentType:false,
                    ..........
}

Controller (Geofence.php)

public function save_geofence()
{
    $qry = $this->geofence_m->save_new_geofence();
    echo json_encode($qry);
}

模型(Geofence_m.php)

public function save_new_geofence()
{
              ..............

    $gid = $this->db->insert_id();

    if($insQry)
    {
        $array = json_decode($_POST['stringified'], true);

        foreach ($array as $v1) {
            foreach ($v1 as list($a, $b)) {
                $insData = array('geofence_ID' => $gid,
                                'geofence_latitude' => $a,
                                'geofence_longitude' => $b);
                $this->db->insert('x_geofence_coordinates', $insData);    
            }
        }
    }
}

我尝试插入整个 $array 而不是 for 循环,这是插入的数据:

[
    {"lat":22.59725475332327,"lng":113.92748227208108},
    {"lat":22.57316327618248,"lng":114.17330136387795},                
    {"lat":22.436142339776456,"lng":114.20626034825295},        
    {"lat":22.408214041987026,"lng":113.85332455723733}
]

更新:

我对我的错误感到抱歉。这是当我尝试在不进行 for 循环的情况下插入 $array 时插入的 $array 的数据。上面的字符串有误,请忽略。谢谢。

[
    [
        {"lat":22.5642864069,"lng":113.886283542}, 
        {"lat":22.5490675872,"lng":114.206260348}, 
        {"lat":22.4069444405,"lng":114.199393893}, 
        {"lat":22.4171009278,"lng":113.88353696}
    ]
]

最后更新:

第一个数据是正确的。我不知道如何获得第二个数据,但绘图管理器的 event.overlay.getPath().getArray();坐标是第一个。 ZerosAndOnes 的第一个答案是我的案例的解决方案。谢谢。

最佳答案

更新:

[
    [
            {"lat":22.5642864069,"lng":113.886283542}, 
            {"lat":22.5490675872,"lng":114.206260348}, 
            {"lat":22.4069444405,"lng":114.199393893}, 
            {"lat":22.4171009278,"lng":113.88353696}
    ]
]

是一个数组的数组,其中内部数组是一个对象数组。假设第一个数组始终只包含单个数组

[
    {"lat":22.5642864069,"lng":113.886283542}, 
    {"lat":22.5490675872,"lng":114.206260348}, 
    {"lat":22.4069444405,"lng":114.199393893}, 
    {"lat":22.4171009278,"lng":113.88353696}
]

with json_decode($_POST['stringified'], true); 可以通过循环获取值

//Assuming the first array always contains a single array
foreach ($array[0] as $v1)
{
    $insData = array('geofence_ID' => $gid,
                    'geofence_latitude' => $v1["lat"],
                    'geofence_longitude' => $v1["lng"]);
    $this->db->insert('x_geofence_coordinates', $insData);
}
<小时/>

将来使用 print_r($arrayOrObject)var_dump($arrayOrObject) 来更好地了解数组或对象的结构。

<小时/>

原始答案:

list() only works on numerical arrays and assumes the numerical indices start at 0.

$array 是一个对象数组(使用 {} 而不是 []),而不是数组数组。 因此 list($a, $b) 中的 $a$b 将为空。

由于它是一个对象数组,因此您可以如下更改 foreach 以访问对象的 latlng 属性。

foreach ($array as $v1) {
            $insData = array('geofence_ID' => $gid,
                            'geofence_latitude' => $v1->lat,
                            'geofence_longitude' => $v1->lng);
            $this->db->insert('x_geofence_coordinates', $insData);    
    }

关于javascript - 循环遍历 MVCArray 以将纬度和经度保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50027425/

相关文章:

javascript - 如何执行这个数组(jQuery Ajax)?

php - 格式化我的电脑后移动 Xamp,发现我所有的数据库都是空的

php - 如何实现内容处置 : attachment?

php - Codeigniter如何在 Controller 中接收ajax post数据

javascript - 在同一 View 中单击时动态更改页面

javascript - 谷歌地图回归原点

javascript - 使用 jQuery 定位表单中的输入元素,无需手动调用

javascript - 配置 MailDev 将邮件中继到外部服务器

php - Codeigniter 在查询中添加空格,这就是为什么它返回错误的 Total + mysql?

php - 无法访问 codeigniter 中的 bootstrap.css 文件以获取奇怪的 403 错误