如何在反应组件中传递iframe

时间:2017-05-07 18:14:08

标签: javascript google-maps reactjs

我正在尝试在我的反应组件中创建一个简单的iframe,它将显示一个谷歌地图位置。我目前正在对src进行硬编码,如此

this.state.features.map(function(school){
    return(
        <div>
            {school.name}
            <div>
            <iframe frameBorder="0" style={{ width: "100%", height: "450"}} 
                src="https://www.google.com/maps/embed/v1/place?q=40.7127837,-74.0059413&amp;key=AIzaSyCc3zoz5TZaG3w2oF7IeR-fhxNXi8uywNk">
            </iframe>
            </div>
        </div>
    )
})

似乎有用。但是我想使用react变量传递scr 所以我正在做这样的事情

const MY_API = AIzaSyCc3zoz5TZaG3w2oF7IeR-fhxNXi8uywNk

let _url = "https://www.google.com/maps/embed/v1/place?q=40.7127837,-74.0059413&amp;key="+MY_API;

然后像这样动态传递src

<iframe frameBorder="0" style={{ width: "100%", height: "450"}} 
        src={_url}>
</iframe>

但现在我收到以下错误

  

Google Maps API服务器拒绝了您的请求。非法请求。   意外的参数&amp; amp; 3Bkey&#39;

不确定错误消息是什么。请帮忙。

3 个答案:

答案 0 :(得分:3)

试试这个(我无法对问题发表评论)

const MY_API = 'AIzaSyCc3zoz5TZaG3w2oF7IeR-fhxNXi8uywNk'
let _url = `https://www.google.com/maps/embed/v1/place?key=${MY_API}&q=40.7127837,-74.0059413`

以上是使用ES6 template literals

答案 1 :(得分:0)

export const maps = (profile) => { 
  var latitude = coordinates.latitude;
  var longitude = coordinates.longitude;
  const MY_API = 'AIzaSyCc3zoz5TZaG3w2oF7IeR-fhxNXi8uywNk';
  var querystring = 'q='+latitude+','+longitude;
  let _url = `https://www.google.com/maps/embed/v1/place?key=${MY_API}&`+querystring;
  return (
    <div className="col-lg-5 col-md-12">
      <iframe frameBorder="0" width="100%" height="100%"  src={_url}>
      </iframe>
    </div>
  )
};

答案 2 :(得分:0)

我不知道这是不是最好的选择:

<div dangerouslySetInnerHTML={{ __html: `<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d30019.337398962354!2d-42.98605759032091!3d-19.864598844970093!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0xa51a4bd62a16a7%3A0x1ba11c1e287c95a4!2sS%C3%A3o%20Domingos%20do%20Prata%2C%20MG%2C%2035995-000!5e0!3m2!1spt-BR!2sbr!4v1593640995514!5m2!1spt-BR!2sbr" width="600" height="450" frameborder="0" style="border:0;" allowfullscreen="" aria-hidden="false" tabindex="0"></iframe>` }} />