Javascript Google将API结果变量映射到php中

Javascript Google将API结果变量映射到php中,javascript,php,ajax,google-maps-api-3,Javascript,Php,Ajax,Google Maps Api 3,我想从JS中导出搜索结果变量(纬度和长度)作为php变量。 我知道有必要有另一个php文件,例如test.php 变量如下: marker.setPlace({ placeId: place.place_id, location: results[0].geometry.location, }); marker.setVisible(true); //infowindowContent.c

我想从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); 
        });
    });
}
您可以简单地使用with和JSON(for和)将数据传递到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...

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

您需要在PHP中创建HTTP端点,然后您可以将lat lon传递到url,或者可以将其设置为cookie并从PHPWell读取,映射文件中没有错误,但是在index.php中有两个错误:注意:第20行的C:\xampp\htdocs\licencjat\index.php中的未定义索引:marker注意:第21行的C:\xampp\htdocs\licencjat\index.php中的未定义索引:infoWindow。我在文件开头启动了会话,当然xampp是打开的。@jackson77,复制示例时可能会出错:您可以在代码中的标记处添加标记,系统将崩溃。还有一点,js代码中的标记可能没有定义(比如infowindow),您可以添加这行代码以检查js变量是否已定义:
if(!infowindow | |!marker){alert(“ERROR”);return;}