*NOTE - This code is from a third party extension. I had no part of it's creation and several years ago when used it was the only extension available at the time. So while I appreciate your opinions, I do hope all comments can be just for suggestions on a resolution. Thanks!
We have many sites running a Google Maps component for a CMS that allows for clients to add markers and outlines (polygons) to their Google Maps.
This has been working for years. To note, it uses Google Maps JS API 2, which has been discontinued rather than API 3. However, Google has noted API 2 will still work well into 2013 so that is not the issue. However, they must have changed something because as of the other day, on all our sites though the Maps appear the markers and polygons do not. They are on different servers.
Before there was no errors but now in Chrome it says:
"Uncaught SyntaxError: Unexpected token )" for line 1669 in a JS File. You can see the file in the following Gist:
https://gist.github.com/2238148
As you can see there is no missing ")" and the code has work unmodified for years on nearly 100 sites, so assume something on Google's end must have changed. But is there something we can adjust to this code to help counter there change? -Update on March 25th when all of this broke Google made an update to their Google Maps API 2.
Searched the web and here is an example site using the same component with the same error: http://goo.gl/GMgOs
This issue appears to be near:
// extract current digraph from overlay function var digraph = GMap.addOverlay.toString().replace(/\s/g,'').replace(/.push\([^{]+\);a.initialize\([^{]+\);a.redraw\([^{]+\).+$/,'').replace(/^.+\./,''); // add multiple overlays at once (api hack to improve loading speed) GMap2.prototype.addOverlays = function(a) { var b = eval('this.' + digraph); var i = a.length; while (i--) { b.push(a[i]); a[i].initialize(this); a[i].redraw(true); } }
If that code needs to be alterted could someone post the modified version on a gist or pastie?
Another Update - That code in the pre above I commented out since it supposively is not needed by was a hack to speed things up. Still gets an error however noted in the comments. I did notice however here: https://developers.google.com/maps/documentation/javascript/v2/reference#GMap2.Methods.Overlays that it calls the code, "addOverlay" rather than "addOverlays" so wondering if maybe the s was taken off in the most recent API Google update. Removing the s in all three locations just shoots out a new error which repeats [object] many times.
The code is an abomination, in fact I've not seen a worse add-on in five years of working with and helping users of Version 2. It overwrites GMap (part of the Version 2 API to provide compatibility with Version 1) with no redeclaration. The error you're getting is a direct result of a hack to minified code: this was bound to fail at some point and should never have been implemented.
The best thing you can do is to remove the var digraph
line and then redefine the new method GMap2.prototype.addOverlays
which follows it. That will allow the code to use the API's addOverlay()
function and should eliminate the problem.
GMap2.prototype.addOverlays = function(a) {
var i = a.length;
while (i--) {
this.addOverlay(a[i]);
}
It appears that addOverlays()
takes an array of overlays. The existing method attempts to add them directly to the internal array of overlays, which has moved. The suggested method simply uses GMap2's own addOverlay()
method to add each member of the array of objects. Thus we use an exposed method and don't try and hijack the minified code of the API — if we did that again, it would almost certainly break again.
GMarker.prototype.openInfoWindowFX
and GMarker.prototype.updateInfoWindow
are additions to GMarker and unlikely to cause problems (especially if they currently work), although even they use properties of GMarker -- which isn't really recommnded.