0

I want to load the maps.googleapis if the user clicks on a view that contains a map, so I have code to load the api with a callback parameter:

<script type="text/javascript">
$('div[data-role="page"]').bind('pagecreate', function () {
    var script = document.createElement("script");
    script.id = "addedformaps";
    script.type = "text/javascript";
    script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=initMap";
    document.body.appendChild(script);
});

The initMap function references item in the google.maps object, such as:

<script type="text/javascript">
function initMap() {
    var mapType = google.maps.MapTypeId.ROADMAP;

The first time the view is loaded, I get an error because google.maps.MapTypeId is undefined. Subsequent loads work, until the browser cach is cleared, and then the error occurs again. I am able to get around this by adding a SetTimeout to the initMap function:

<script type="text/javascript">
setTimeout(function initMap() {
  ...
}, 500)
</script>

but I am not comfortable with slowing down every load. Why is the callback function executing before the google.maps object is fully loaded?

tereško
  • 58,060
  • 25
  • 98
  • 150
Christine
  • 562
  • 3
  • 19
  • Is this proper way for map initialization? Most examples are using something like ` var center = new google.maps.LatLng()`. See also [“google.maps.MapTypeId is undefined” when using GMAP3 in FF 14](http://stackoverflow.com/questions/11972331/google-maps-maptypeid-is-undefined-when-using-gmap3-in-ff-14) – Anto Jurković Nov 19 '13 at 15:45
  • Can you take a look at [this fiddle](http://jsfiddle.net/WTPxL/1/)? I cannot reproduce the issue you're having. If we can reproduce it, then we can try to find a solution. – Taylan Aydinli Nov 19 '13 at 16:36
  • Anto, At that point I'm not initializing the map, I'm trying to determine which map type to use when I create the map. I default it to Roadmap, then fall through a bunch of if stmts that may or may not change it to one of the other values, then I send that as the mapTypeId. – Christine Nov 19 '13 at 19:01
  • I can no longer recreate this, and neither can the other person who reported the issue to me. I don't know if google changed something, we didn't. – Christine Nov 20 '13 at 17:12

0 Answers0