0

I've looked at many posts on this subject, but I just can't seem to find a fix for a Google map embedded on my website. It's an iframe that refers back to a separately hosted html file created with the maptiler program. The embed produces the dreaded "Oops! Something went wrong" grey box ... although the desired map does show for about half a second. The java console complains about "no api keys" ... I've created all kinds of Google Maps Javascript API keys, sometimes using the http rule, sometimes not. Nothing works.

The html file appears below. (I've left the key out on purpose.)

<!DOCTYPE html>
<html>
<head>
<title>3rd</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { overflow: hidden; }
body { overflow: hidden; padding: 0; margin: 0;
width: 100%; height: 100%; font-family: Trebuchet MS, Trebuchet, Arial, sans-serif; }
#map { position: absolute; top: 10px; left: 10px; right: 10px; bottom: 15px; overflow: auto; }
#footer { position: absolute; bottom: 0px; left: 0px; width:100%; height: 12px; overflow: hidden; }
@media screen and (max-width: 600px) {
  #map { top:0px; left:0px; width:100%; height:100%;}
}
body { background: #f4f4f4;}
#header { background: #fff; box-shadow: 0 1px 3px #CCC; border: 1px solid #ccc; }
#header h1 { padding:7px 10px; margin:0; font-size: 28px; }
#map { border: 1px solid #ccc; box-shadow: 0 1px 3px #CCC; background-color: #DEDCD7;}
#footer { text-align:center; font-size:9px; color:#606060; }
</style>
<!--[if lte IE 6]>
<style type="text/css">
#map {
    height:expression(document.body.clientHeight-35); /* 10+10+15=35 */
    width:expression(document.body.clientWidth-20); /* 10+10=20 */
}
</style>
<![endif]-->
<script type="text/javascript" src="https://maps.google.com/maps/api/js?MY_KEY_HERE&ampsensor=true"></script>
<!-- Get your Google Maps API Key: https://developers.google.com/maps/documentation/javascript/tutorials/adding-a-google-map#introduction-->
<!--<script type="text/javascript" src="https://maps.google.com/maps/api/js?key=MY_Key_HERE"></script>-->
<script type="text/javascript">
var map;
var mapBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(44.890044, -75.192903),
    new google.maps.LatLng(44.909499, -75.165437));
var mapMinZoom = 13;
var mapMaxZoom = 17;
var maptiler = new google.maps.ImageMapType({
    getTileUrl: function(coord, zoom) { 
        var proj = map.getProjection();
        var z2 = Math.pow(2, zoom);
        var tileXSize = 256 / z2;
        var tileYSize = 256 / z2;
        var tileBounds = new google.maps.LatLngBounds(
            proj.fromPointToLatLng(new google.maps.Point(coord.x * tileXSize, (coord.y + 1) * tileYSize)),
            proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileXSize, coord.y * tileYSize))
        );
        var y = coord.y;
        var x = coord.x >= 0 ? coord.x : z2 + coord.x
        if (mapBounds.intersects(tileBounds) && (mapMinZoom <= zoom) && (zoom <= mapMaxZoom))
            return zoom + "/" + x + "/" + y + ".png";
        else
            return "https://www.maptiler.com/img/none.png";
    },
    tileSize: new google.maps.Size(256, 256),
    isPng: true,
    opacity: 1.0
});
function init() {
    var opts = {
        tilt:0,
        streetViewControl: false,
        center: new google.maps.LatLng(44.899771, -75.179170),
        zoom: 13
    };
    map = new google.maps.Map(document.getElementById("map"), opts);
    map.setMapTypeId('satellite');
    map.overlayMapTypes.insertAt(0, maptiler);
}
</script>
</head>
<body onload="init()">
<div id="footer">Generated with <a href="https://www.maptiler.com/">MapTiler</a></div>
<div id="map"></div>
</body>
</html>

2 Answers2

0

Latest Google Maps API requires personal API key for each published website - this is a decision made by Google Maps team.

You have to generate your own API key and use it in the template to get your maps working. More information is available on Google Maps API pages:

https://developers.google.com/maps/documentation/javascript/adding-a-google-map#step_3_get_an_api_key

To get your key from the Google Developers Console follow this link:

https://console.developers.google.com/flows/enableapi?apiid=maps_backend,geocoding_backend,directions_backend,distance_matrix_backend,elevation_backend&keyType=CLIENT_SIDE&reusekey=true

The key is used when pasting Google Maps Javascript Library into a page header of the HTML as a parameter to query (replace YOUR_API_KEY with your own generated key):

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"
  async defer>
</script>

From MapTiler 7.0 on, we added a field for this key to the MapTiler’s Settings. MapTiler will automatically add this key to pre-generated templates with Google Maps API to save your time and effort.

This answer is a repost from MapTiler HowTo: http://www.maptiler.com/how-to/google-maps-api/

MapTiler
  • 1,754
  • 14
  • 22
  • Thanks. Yeah, I've been all over those links, created multiple keys, inserted the keys into the code. No dice. I get the "Oops" message and the "No Api Keys" claim in the java console, over and over again. My Google Maps Java Api is definitely enabled, with a big green "enabled" check mark inside the Google Cloud Console. Totally stymied. The MapTiler Howto was one of the first things I looked at before I ended up seeking help in this forum. – N. Zandbergen Jan 23 '18 at 15:35
  • Have you set the URL of your website in your Google Cloud Project? – MapTiler Jan 24 '18 at 16:29
  • I'm getting closer, after purchasing your product now, but — maddeningly — I still get the error message. Now it says it's an invalid key map. I do have the URL of my website set in the Google Cloud Project. There are two places to do it, which is especially confusing. There is "Domain Verification" and then there is the "Key restriction" where I can plug in URL's, too. It's an https website. I wish somebody could tell me EXACTLY what URL's to plug in and where. Does it need the URL where the Maptiler files are hosted? Does it need only the URL where the iframe is embedded? Thanks again. – N. Zandbergen Jan 24 '18 at 20:41
  • This message appears in the Java console: "Can't Load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings." – N. Zandbergen Jan 24 '18 at 20:50
0

So, the solution was to create yet another project in Google Cloud Console and another API key, which I was careful to verify against my website. I specified the https:// version of the site, among others, and it worked. I had verified the site before but possibly not the https:// version. It may be a coincidence.