2017-08-31 42 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」);返回;}'。 –