2017-08-31 100 views
0

我想从JS中导出搜索结果变量(纬度和经度)作为php变量。 我知道有必要有另一个PHP文件,例如test.php。Google Maps API结果变量到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); 
     }); 
    }); 
} 
+1

您需要在PHP中创建HTTP端点,然后您可以将经纬度传递给url或者可以设置为cookie并从PHP中读取 –

回答

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... 

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

+0

那么,映射文件中没有错误,但是在index.php中有两个错误:注意:未定义的索引:在第20行的C:\ xampp \ htdocs \ licencjat \ index.php中的标记 注意:未定义的索引:infoWindow位于第21行的C:\ xampp \ htdocs \ licencjat \ index.php 。我在文件的开头开始会话,当然xampp已经开启。 – jackson77

+0

@ jackson77,当你复制这个例子时,你可能会犯错:你可以在你的代码中放置标记,系统会崩溃。另一点,你的js代码中的标记可能没有被定义(比如你的infowindow),你可以添加这行代码来检查你的js变量是否被定义:if(!infowindow ||!marker) {alert(“ERROR”);返回;}'。 –