2017-08-15 43 views
1

我有一個變量,我在其上存儲地址。 我想將它傳遞給谷歌地圖API靜態:將變量傳遞給靜態谷歌圖像地圖

這樣

adr = comp.adress; 
console.log(adr);// 28, rue Armand Carrel 
<img src="https://maps.googleapis.com/maps/api/staticmap?center={adr}&amp;size=600x300" alt="exemple" /> 

我不明白的ADR值有什麼不對的代碼?

空間來處理

"addr": "\n    \n    28, rue Armand Carrel \n    ", 

謝謝

回答

1

你在做什麼在你剛讀{adr}作爲字符串的一部分。

假設以上是React/ES6/ES2015

你需要把它串聯到字符串:

<img src={"https://maps.googleapis.com/maps/api/staticmap?center=" + adr + "&amp;size=600x300"} alt="exemple" />` 

或者你可以使用template literals

<img src={`https://maps.googleapis.com/maps/api/staticmap?center=${adr}&amp;size=600x300`} alt="exemple" />` 
1

的問題是不相關的reactjs。

您需要對地址進行編碼(使用encodeURIComponent()),以便它成爲有效的網址。就像這樣:

//var address = '28, rue Armand Carrel'; 
 
var address = "\n    \n    28, rue Armand Carrel \n"; 
 
document.body.innerHTML = '<img src="https://maps.googleapis.com/maps/api/staticmap?center=' + encodeURIComponent(address) + '&amp;size=600x300" alt="exemple" />';

。如果你想重新格式化地址:

var address = "\n    \n    28, rue Armand Carrel \n"; 
 
console.log(address.replace(/\n/g, '').trim());

+0

謝謝你的迴應,我從獲取數據一個JSON,所以它有15個空格... 所以我想圖d出如何刪除它們,現在我需要刪除最後一個空格,以便我可以顯示地圖。 28,+ rue + Armand + Carrel +(需要刪除最後的+) – Taieb

+0

我不確定我明白你的意思。你得到的地址在JSON或圖像的HTML?如果你得到的地址,你只需要使用'encodeURIComponent()',你會得到一個有效的網址。 –

+0

http://i.imgur.com/diK0xsg.png?1 我試圖替換al「」,但我有一個最後的「+」後卡雷爾我想刪除它,以獲得工作地址。 – Taieb