Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Layers 3 get Google Maps baselayer?

I've read some posts regarding this matter with links to examples

However I find it hard to follow how they've done it in the example:

var gmap = new google.maps.Map(document.getElementById('gmap'), {
  disableDefaultUI: true,
  keyboardShortcuts: false,
  draggable: false,
  disableDoubleClickZoom: true,
  scrollwheel: false,
  streetViewControl: false
});

var view = new ol.View({
  // make sure the view doesn't go beyond the 22 zoom levels of Google Maps
  maxZoom: 21
});
view.on('change:center', function() {
  var center = ol.proj.transform(view.getCenter(), 'EPSG:3857', 'EPSG:4326');
  gmap.setCenter(new google.maps.LatLng(center[1], center[0]));
});
view.on('change:resolution', function() {
  gmap.setZoom(view.getZoom());
});

var vector = new ol.layer.Vector({
  source: new ol.source.GeoJSON({
    url: 'data/geojson/countries.geojson',
    projection: 'EPSG:3857'
  }),
  style: new ol.style.Style({
    fill: new ol.style.Fill({
      color: 'rgba(255, 255, 255, 0.6)'
    }),
    stroke: new ol.style.Stroke({
      color: '#319FD3',
      width: 1
    })
  })
});

var olMapDiv = document.getElementById('olmap');
var map = new ol.Map({
  layers: [vector],
  interactions: ol.interaction.defaults({
    altShiftDragRotate: false,
    dragPan: false,
    rotate: false
  }).extend([new ol.interaction.DragPan({kinetic: null})]),
  target: olMapDiv,
  view: view
});
view.setCenter([0, 0]);
view.setZoom(1);

olMapDiv.parentNode.removeChild(olMapDiv);
gmap.controls[google.maps.ControlPosition.TOP_LEFT].push(olMapDiv);

How many DOM elements do I have to create? Is this correct?

<div id="map">
   <div id="gmap"></div>
   <div id="olmap"></div>
</div>

I'm getting an error by structuring like this. Here is the error I get:

Cannot read property 'parentNode' of null

Any clues on how to implement Google Maps in OL3?

like image 869
Nicolas T Avatar asked Dec 04 '15 11:12

Nicolas T


1 Answers

That attempt was deprecated, better to look at: https://github.com/mapgears/ol3-google-maps

like image 120
bartvde Avatar answered Oct 05 '22 11:10

bartvde