Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenLayers 2.13.1 with Google maps

When I switch from OpenLayers 2.12 to 2.13.1, all Google Maps layers stop working. When I use the layer switcher the map area just turns white.

I don't see any error messages in the development console of my browser at any point, and all files are loaded successfully.

I'm creating layers like this:

var layerGoogleMapsNormal    = new OpenLayers.Layer.Google("Google Normal"   , { type : google.maps.MapTypeId.ROADMAP, sphericalMercator : true });
var layerGoogleMapsPhysical  = new OpenLayers.Layer.Google("Google Physical" , { type : google.maps.MapTypeId.TERRAIN, sphericalMercator : true });

Other layers (OSM, WMS) work fine.

While I was writing this, I found out that the example from OpenLayers doesn't work properly either.

http://dev.openlayers.org/examples/google.html

Could it be that Google made a breaking change? Does anyone else have the same problem, and does anyone know a solution?

Remark: I've looked at OpenLayers3, but its API is so different that I don't think we'll ever be able to port all of our code to that. We won't be able to convince our clients to pay for rewriting all of the mapping stuff to make use of OL3, so I guess this project is stuck with the 2.x branch forever...

like image 674
Wouter van Nifterick Avatar asked Jan 07 '23 12:01

Wouter van Nifterick


2 Answers

I had the same problem as you. In the link that includes the js file from Google, you must tell it to send you an older version, before the breaking change from 15 Sep.

Example:

Normal link: <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=false"></script>

Solution link: <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=false&v=3.20"></script>

At the moment, the latest version is 3.22. What works best for me is 3.20 for the same Openlayers version as yours.

More info on the topic: https://developers.google.com/maps/documentation/javascript/versions

Later Edit: As the comments mention, this fix no longer works. In the github issue thread somebody did a patch fix, but I couldn't find the file for direct download so I had to manually apply the fix to my project. To save the others from doing the same, I'll put here a link to the minified version with the fix included.

Download: OpenLayers 2.13.1 with google fix

like image 126
Marius P. Avatar answered Jan 11 '23 17:01

Marius P.


The accepted answer now no longer works since, as @geocodezip mentioned, v3.20 has now been retired by Google. In order to make OpenLayers 2.13.x work with the current version of the Google Maps API you must monkey patch the OpenLayers.js file as specified in this comment on OpenLayers issue #1450.

like image 22
douglasr Avatar answered Jan 11 '23 16:01

douglasr