Shadid
Shadid

Reputation: 4252

how to pass iframe in react component

I am trying to make a simple iframe in my react component that will show a google map location. I am currently hard-coding the src like this

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>
    )
})

which seems to work. However I want to pass the scr using a react variable so I am doing something like this

const MY_API = AIzaSyCc3zoz5TZaG3w2oF7IeR-fhxNXi8uywNk

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

and then passing the src dynamically like this

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

but now I get the following error

The Google Maps API server rejected your request. Invalid request. Unexpected parameter 'amp%3Bkey'

Not sure what's the error message is about. help please.

Upvotes: 4

Views: 4632

Answers (3)

Rodrigo Camarano
Rodrigo Camarano

Reputation: 1

I don't know if it's the best option:

<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>` }} />

Upvotes: 0

Mohit Sharma
Mohit Sharma

Reputation: 677

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>
  )
};

Upvotes: 0

CraigRodrigues
CraigRodrigues

Reputation: 86

Try this (I am unable to comment on questions yet)

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

The above is using ES6 template literals

Upvotes: 4

Related Questions