0

I'm trying to get javascript to render an embedded google map once the user's coordinates are obtained.

The code that gets the lat & long works, so I wont show it, but here's the gist of it

<script type="text/javascript">
  $(document).ready(function() {
    //previous code calculating lat & lng, etc
    showMap(lat, lng);
  function showMap(lat,lng) {
    var apikey = "asdfasdfasdfasdf";
    var code = "<iframe width='480' height='320' frameborder='0' style='border:0' src='https://www.google.com/maps/embed/v1/view?zoom=14&center=" + lat + "," + lng + "&key=" + apikey + "></iframe>";
    console.log(code) //outputs good code
    $("#map").html(code)
  }
 }
</script>

Note that this does work....

$("#map").html("<h2>FML</h2>");

That works.

2
  • you are missing some brackets and semicolons. at the end of $(document).ready(.......) that closing bracket is missing. after console log ; missing Commented May 18, 2015 at 1:14
  • ); missing at the end of $(document).ready(function(){.....} Commented May 18, 2015 at 1:23

2 Answers 2

2

Missing the closing ' for the src attribute.

..."&key=" + apikey + "></iframe>";

needs to be

..."&key=" + apikey + "'></iframe>";
                    ^
Sign up to request clarification or add additional context in comments.

Comments

2

test if it is working with all the typo's corrected:

<script type="text/javascript">
  $(document).ready(function() {
    //previous code calculating lat & lng, etc
    var lat = 1234;
    var lng = 5678;
    showMap(lat, lng);
  });
  function showMap(lat,lng) {
    var apikey = "asdfasdfasdfasdf";
    var code = "<iframe width='480' height='320' frameborder='0' style='border:0' src='https://www.google.com/maps/embed/v1/view?zoom=14&center=" + lat + "," + lng + "&key=" + apikey + "'></iframe>";
    console.log(code); //outputs good code
    $("#map").html(code);
  }
</script>

(credit to @epascarello for the missing "'")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.