【问题标题】:Google Maps API results variables into phpGoogle Maps API 结果变量转换为 php
【发布时间】:2018-02-09 18:24:23
【问题描述】:

我想从 JS 中导出搜索结果变量(纬度和经度)作为 php 变量。 我知道必须有另一个 php 文件,例如 test.php。

以下变量:

        marker.setPlace({
          placeId: place.place_id,
          location: results[0].geometry.location,

        });
        marker.setVisible(true);

        //infowindowContent.children['place-name'].textContent = place.name;
        //infowindowContent.children['place-id'].textContent = place.place_id;
        infowindowContent.children['place-address'].textContent =
            //results[0].formatted_address;
            results[0].geometry.location.lat(), 
            results[0].geometry.location.lng(),
            infowindow.open(map, marker); 
        });
    });
}

【问题讨论】:

  • 您需要在 PHP 中创建 HTTP 端点,然后您可以将 lat lon 传递给 url 或者可以设置为 cookie 并从 PHP 中读取它

标签: javascript php ajax google-maps-api-3


【解决方案1】:

您可以简单地将XMLHttpRequestajax 和JSON(用于jsphp)一起使用,以便将数据传递到您的php 文件:

var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      console.log (this.responseText);
    }
  };
  xhttp.open("GET", "demo_get.asp?marker=" + JSON.stringify (marker) + "&infoWindow=" + JSON.stringify (infowindow), true);
  xhttp.send();

然后,在php中:

$marker = json_decode ($_GET["marker"]);
$infoWindow = json_decode ($_GET["infoWindow"]);
// Do stuff...

如果您有任何问题,请告诉我。

【讨论】:

  • 嗯,地图文件没有错误,但是在 index.php 中有两个错误: 注意:未定义索引:第 20 行 C:\xampp\htdocs\licncjat\index.php 中的标记注意:未定义索引:第 21 行 C:\xampp\htdocs\licncjat\index.php 中的 infoWindow。我在文件开头开始了会话,当然 xampp 已打开。
  • @jackson77, 你在复制例子的时候可能会出错:你可以在你的代码中把marke放在marker的地方,系统会崩溃。另外一点,您的 js 代码中的标记可能未定义(如您的信息窗口),您可以添加这行代码以检查您的 js 变量是否已定义:if (!infowindow||!marker) {alert ("ERROR"); return;}
猜你喜欢
  • 2013-11-05
  • 2011-06-01
  • 2014-02-18
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多