[OpenLayers-Users] Showing additional Google Layers (e.g. TransitLayer)

rbrtmackin robert.mackin at gmail.com
Thu Mar 27 14:36:08 PDT 2014


I use openlayers in my application to allow clients to choose their mapping
backend without affecting the rest of my application. 

One of my customers who uses Google Maps v3 asked if I could show this layer 
https://developers.google.com/maps/documentation/javascript/examples/layer-transit

I took a shot at doing it this way:
     var displayProjection = new OpenLayers.Projection("EPSG:4326");
     map = new OpenLayers.Map('map', {
        projection: 'EPSG:3857',
        displayProjection: displayProjection
    });
    var mapLayer = new OpenLayers.Layer.Google(
                "Google Streets", // the default
                {numZoomLevels: 20}
    );
    map.addLayer(mapLayer);
    var transitLayer = new google.maps.TransitLayer();
    transitLayer.setMap(map.mapObject); 
    map.setCenter(new OpenLayers.LonLat(-71.0636,
42.3581).transform('EPSG:4326', 'EPSG:3857'), 15);

And it didn't work (note: I also tried moving the map.addLayer(mapLayer)
call after transitLayer.setMap(map.mapObject), but it didn't make a
difference.

Then, on a lark, I decided to modify
openlayers\lib\OpenLayers\Layer\Google\v3.js
In loadMapObject(), I added those same 2 lines after the call to new
google.maps.Map as follows

            mapObject = new google.maps.Map(div, {
                center: center ?
                    new google.maps.LatLng(center.lat, center.lon) :
                    new google.maps.LatLng(0, 0),
                zoom: this.map.getZoom() || 0,
                mapTypeId: this.type,
                disableDefaultUI: true,
                keyboardShortcuts: false,
                draggable: false,
                disableDoubleClickZoom: true,
                scrollwheel: false,
                streetViewControl: false
            });
      var transitLayer = new google.maps.TransitLayer();
      transitLayer.setMap(mapObject); 


The transit layer appeared!

I don't like the idea of modifying the openlayers code, especially because I
don't know why this works. Is there a supported way of doing this?



--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Showing-additional-Google-Layers-e-g-TransitLayer-tp5131680.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.


More information about the Users mailing list