【问题标题】:Mapbox: How to get an address value in Mapbox input search autocomplete and insert in Mysql table using PHP?Mapbox:如何在 Mapbox 输入搜索自动完成中获取地址值并使用 PHP 在 Mysql 表中插入?
【发布时间】:2019-10-02 04:24:04
【问题描述】:

我在我的项目中使用 Mapbox,我想知道如何从输入搜索自动完成中获取地址并使用 php 在 Mysql 表中插入这个地址?我试图找到与我的疑问类似的东西,但我没有找到。以下是我试图找到答案的一些问题:

  1. Mapbox - Can I use the locationlistener without Mapbox map
  2. Custom Mapbox Geocoder Control
  3. Retrieve data from mapbox geocoder

在这种情况下,我有一个如下表:

用户

id | username | address        | City
==============================================
01 | John     | abc street     | New York
02 | Joseph   | Marie Street   | Washington
03 | Amy      | Barkley street | Houston

在我的表单中,我有 3 个字段将数据发送到用户表。下面的 HTML 代码显示了表单结构:

<form action="" id="register_user">
            <label for="username">Username</label>
            <input type="text" id="Username" name="Username" placeholder="Your Username..">

            <label for="address">address</label>
            <input type="text" id="address" name="address" placeholder="Your address..">

            <input type="submit" value="Submit" >
        </form>

在我的 php 插入脚本中,我有一个函数可以将输入表单中的值插入到 users 表

if(isset($_GET['add_location'])) {
    add_location();
}

function add_location(){
    $con=mysqli_connect ("localhost", 'root', '','system');
    if (!$con) {
        die('Not connected : ' . mysqli_connect_error());
    }
    $username= $_GET['username'];
    $address = $_GET['address'];
    // Inserts new row with place data.
    $query = sprintf("INSERT INTO users " .
        " (id, username, address) " .
        " VALUES (NULL, '%s', '%s');",
        mysqli_real_escape_string($con,$username),
        mysqli_real_escape_string($con,$address));

    $result = mysqli_query($con,$query);
    echo json_encode("Inserted Successfully");
    if (!$result) {
        die('Invalid query: ' . mysqli_error($con));
    }
}

下面的图片正是我需要的,但我不知道该怎么做:

上面的例子可以做吗?谢谢:)

【问题讨论】:

    标签: php mysql mapbox


    【解决方案1】:

    您可以订阅地理编码器事件,尤其是“结果”,并获取您需要的所有信息。

    map.addControl(new MapboxGeocoder({
                  accessToken: accessToken,
                  mapboxgl: mapboxgl,
              }).on('result', function({ result }) { console.log(result.place_name) });

    【讨论】:

      猜你喜欢
      • 2022-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多