Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript Error "Uncaught Reference Error: google is not defined" on Android PhoneGap
    primarykey
    data
    text
    <p>I recently updated the version of PhoneGap/Cordova I was using to the most recent. There is a page in my app that keeps throwing the following error every time it loads.</p> <blockquote> <p>Uncaught Reference Error: google is not defined</p> </blockquote> <p>I tried to use a sample webpage directly from the google developer's site to try and narrow down my options, and still got the same error when trying to load that page on my phone. (found here: <a href="https://developers.google.com/maps/documentation/javascript/examples/map-simple" rel="nofollow noreferrer">https://developers.google.com/maps/documentation/javascript/examples/map-simple</a>). I should note, when running this webpage from google as an .HTML file in Chrome on my desktop it worked fine.</p> <p>I have also tried to use the solution found here: <a href="https://stackoverflow.com/questions/14229695/google-maps-api-throws-uncaught-referenceerror-google-is-not-defined-only-whe">Google Maps API throws &quot;Uncaught ReferenceError: google is not defined&quot; only when using AJAX</a></p> <p>however the initialize method was never even called, which leads me to believe it's a problem with my script tag that I'm still missing.</p> <p>As far as I can tell, I am using proper script calls in my HTML:</p> <pre><code>&lt;script type="text/javascript" src="cordova.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?&lt;KEY&gt;&amp;sensor=false"&gt;&lt;/script&gt; </code></pre> <p>and the javascript code that is crashing is as follows:</p> <pre><code>origins[0] = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); </code></pre> <p>The full code is here:</p> <pre><code>&lt;!DOCTYPE HTML&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;MASH Locations&lt;/title&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" /&gt; &lt;link rel="stylesheet" type="text/css" href="styles/layout.css" /&gt; &lt;script type="text/javascript" src="cordova.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=(myAppKey)&amp;sensor=false"&gt;&lt;/script&gt; &lt;script type="text/javascript" charset="utf-8" src="Util.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" charset="utf-8" src="QuickSort.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" charset="utf-8" src="Location.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; var origins, destinations; var BORDER_ROW = '&lt;tr&gt;&lt;td colspan="3" class="location-result-border"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;'; var RESULT_FORMAT = '&lt;tr&gt;&lt;td id="result_{7}" class="location-result-row" onclick="onLocationResultClick(\'{0}\', \'{1}\', \'{2}\', \'{3}\', \'{4}\', \'http://cloud.intelligentbits.net/mash/images/{5}\', \'{6}\')"&gt;' + '&lt;table class="{8}"&gt;' + '&lt;tr&gt;&lt;td class="location-result-distance" rowspan="3"&gt;&lt;div&gt;{9}&lt;/div&gt;miles&lt;/td&gt;' + '&lt;td class="location-result-logo" rowspan="3"&gt;&lt;img src="http://sqldb.intelligentbits.net/mash/images/{5}" alt="{0}" /&gt;&lt;/td&gt;' + '&lt;td class="location-result-name"&gt;{0}&lt;/td&gt;&lt;/tr&gt;' + '&lt;tr&gt;&lt;td class="location-result-address"&gt;{10}&lt;/td&gt;&lt;/tr&gt;' + '&lt;tr&gt;&lt;td class="location-result-city"&gt;{11}&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;'; function onLoad() { // Wait for PhoneGap to load document.addEventListener("deviceready", onDeviceReady, false); } // PhoneGap is ready function onDeviceReady() { navigator.geolocation.getCurrentPosition(onPositionFound, onPositionError); } function onPositionFound(position) { // get the current location origins = new Array(); origins[0] = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); document.getElementById('finding').innerText = 'Finding our locations...'; readFile('http://sqldb.intelligentbits.net/mash/locations.txt', onLocationsFound); } // onPositionError Callback receives a PositionError object function onPositionError(error) { alert('code: ' + error.code + '\n' + 'message: ' + error.message + '\n'); } function onLocationsFound(text) { if (text == null || text.length == 0) { document.getElementById('finding').innerText = 'An error occurred.'; return; } // split the text into lines (one line per location) var lines = text.split(/\r?\n/g); if (lines.length == 0) { document.getElementById('finding').innerText = 'An error occurred.'; return; } // destinations destinations = new Array(); var locIdx = 0; // iterate over lines/locations for (var i = 0; i &lt; lines.length; ++i) { // split into fields var loc = new Location(); var fields = lines[i].split(';'); for (var j = 0; j &lt; fields.length; ++j) { // split fields into name and value var tokens = fields[j].split('='); if (tokens.length != 2) continue; switch (tokens[0].toUpperCase()) { case 'NAME': loc.Name = tokens[1]; break; case 'ADDRESS': loc.StreetAddress = tokens[1]; break; case 'CITY': loc.City = tokens[1]; break; case 'STATE': loc.Region = tokens[1]; break; case 'POSTAL': loc.PostalCode = tokens[1]; break; case 'PHONE': loc.Phone = tokens[1]; break; case 'HOURS': loc.Hours = tokens[1]; break; case 'LOGO': loc.LogoFileName = tokens[1]; break; case 'EMAIL': loc.Email = tokens[1]; break; } } // save the destination destinations[locIdx++] = loc; } if (destinations.length == 0) document.getElementById('finding').innerText = 'An error occurred.'; else calculateDistances(origins, destinations); } function calculateDistances(orig, dest) { // the passed-in destinations are arrays of Location objects; Google Maps wants strings var destStrings = new Array(); for (var i = 0; i &lt; dest.length; ++i) destStrings[i] = dest[i].ToAddressString(); var service = new google.maps.DistanceMatrixService(); service.getDistanceMatrix( { origins: orig, destinations: destStrings, travelMode: google.maps.TravelMode.DRIVING, unitSystem: google.maps.UnitSystem.IMPERIAL, avoidHighways: false, avoidTolls: false }, onDistancesFound); } function onDistancesFound(response, status) { if (status != google.maps.DistanceMatrixStatus.OK) { alert('Error was: ' + status); } else { // get the place we'll store the list var p = document.getElementById('finding'); var table = document.getElementById('location-results'); var orig = response.originAddresses; var dest = response.destinationAddresses; p.innerText = 'Tap a location for more options.'; // iterate over origins for (var i = 0; i &lt; orig.length; ++i) { var results = response.rows[i].elements; // iterate over destinations for (var j = 0; j &lt; results.length; ++j) { // split the location into the amount and units var distance = results[j].distance.text.Trim().split(' '); // update the locations destinations[j].DistanceFromUser = parseFloat(distance[0]); destinations[j].DistanceUnits = distance[1]; destinations[j].TimeFromUser = results[j].duration.text; } } // sort the distances QuickSort(destinations, 'DistanceFromUser'); // print the results var tablerows = ''; for (var i = 0; i &lt; destinations.length; ++i) { var loc = destinations[i]; tablerows += RESULT_FORMAT.Format(loc.Name, loc.Phone, loc.ToAddressString(), loc.ToTwoLineAddressString(), loc.Hours, loc.LogoFileName, loc.Email, i, 'location-result-' + ((i + 1) % 2 == 0 ? 'even' : 'odd'), loc.DistanceFromUser, loc.StreetAddress, loc.City); } tablerows += '&lt;tr&gt;&lt;td&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;'; // append the rows table.innerHTML += tablerows; } } function onLocationResultClick(name, phone, address, displayAddress, hours, imageUrl, email) { // save the name and address to local storage for the directions window.localStorage.setItem('location-position', address); window.localStorage.setItem('location-address', displayAddress); window.localStorage.setItem('location-title', name); window.localStorage.setItem('location-hours', hours); window.localStorage.setItem('location-phone', phone); window.localStorage.setItem('location-imageUrl', imageUrl); window.localStorage.setItem('location-contact', email); // call link window.location = 'location.html'; } &lt;/script&gt; &lt;/head&gt; &lt;body onload="onLoad()"&gt; &lt;h1&gt; &lt;a href="index.html" class="back"&gt; &lt;div&gt; &lt;span&gt;&lt;/span&gt; &lt;/div&gt; &lt;span class="text"&gt;Back&lt;/span&gt; &lt;/a&gt; Nearest Locations &lt;/h1&gt; &lt;div id="location-results-wrapper"&gt; &lt;h1 style="position: static; width: 94%;"&gt; &lt;a href="#" class="back"&gt; &lt;div&gt; &lt;span&gt;&lt;/span&gt; &lt;/div&gt; &lt;span class="text"&gt;Back&lt;/span&gt; &lt;/a&gt; # &lt;/h1&gt; &lt;table id="location-results"&gt; &lt;tr&gt; &lt;td id="finding" style="vertical-align: top;"&gt;Finding your location...&lt;/td&gt; &lt;tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>and the exact error log:</p> <blockquote> <p>06-27 09:06:00.624: E/Web Console(15020): Uncaught ReferenceError: google is not defined at file:///android_asset/www/locations.html:41</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload