Google Maps Intersection Detector

by HoffZ

HTML

<script src="https://cdn.rawgit.com/bjornharrtell/jsts/gh-pages/1.4.0/jsts.min.js"></script>
<script src="https://maps.google.com/maps/api/js?sensor=false&amp;libraries=drawing"></script>
<div id="map">

</div>

CSS

#map {
  width: 500px;
  height: 400px;
}

JavaScript

var tiledLayer = new google.maps.ImageMapType({
  getTileUrl: function(tileCoord, zoom) {
  	var baseUrl = "https://opencache.statkart.no/gatekeeper/gk/gk.open_wmts?";
    var params = 
      "layer=topo4graatone" +
      "&format=image/png&service=wmts&version=1.0.0" +
      "&request=GetTile&style=default&tilematrixset=EPSG:3857" +
      "&tilematrix=EPSG:3857:" + zoom + "&tilerow=" + tileCoord.y +
      "&tilecol=" + tileCoord.x;
      return baseUrl + params;
  },
  tileSize: new google.maps.Size(256, 256),
  name: "Kartverket",
  maxZoom: 20
});

var mapOptions = {
  zoom: 16,
  center: new google.maps.LatLng(59.618702, 11.079686),  
};

//var map = new google.maps.Map(document.getElementById("map"), mapOptions);


/*
map.mapTypes.set('topo4g',tiledLayer);
map.setMapTypeId('topo4g');
*/
var tileWidth = 512,
    tileHeight = 512;

var map = new google.maps.Map(document.getElementById('map'), mapOptions);

var imageMapType = new google.maps.ImageMapType({
    getTileUrl: function (coord, zoom) {
//        if (zoom >= 18) {
    
            // The call to our earlier function
            var bbox = tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight);
    
            // The server endpoint for getting the images, where we pass bbox.join(',') through
            var url = "https://geminiwms.onpowel.com/SpydebergWMS/gemini-va.wms";
            return url;
  //      }
    },
    tileSize: new google.maps.Size(tileWidth, tileHeight),
    opacity: 0.4
});

map.overlayMapTypes.push(imageMapType);

function tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight) {
    var proj = map.getProjection();

    // scale is because the number of tiles shown at each zoom level double.
    var scale = Math.pow(2, zoom);

    // A point is created for the north-east and south-west corners, calculated
    // by taking the tile coord and multiplying it by the tile's width and the map's scale.
    var ne = proj.fromPointToLatLng(new google.maps.Point( (coord.x+1) *...