The follow tells me that GMap2 is not defined. But the code that uses GMap2 is in the callback.
$(function() {
$('#sample').click(function() {
$.getScript("http://maps.google.com/maps?file=api&v=2&sensor=true&key=API_KEY_HERE", function() {
var map = new GMap2(document.getElementById("mapTest"));
map.setCenter(new GLatLng(18, -77.4), 13);
map.setUIToDefault();
});
});
});
<a id="sample">Click Me</a>
<div id="mapTest" style="width: 200px; height: 100px;"></div>
You can go two ways with this:
1. Continue to use
$.getScript:It seems that you need both an
async=2parameter as well as a different callback structure for it to work. My answer is adapted to your code from this great walkthrough here.2. Use the Google AJAX Loader
Since you are already using a Google Library, why not use their loader to help you out: