Google Maps v3 - prevent API from loading Roboto font

后端 未结 3 1777
忘掉有多难
忘掉有多难 2020-12-01 02:41

Google adds styles to the maps container that override my styles.
I know how to fix this. But the API (v3.8/9/exp) also loads the webfont \"Roboto\" which I don\'t reall

3条回答
  •  时光取名叫无心
    2020-12-01 03:34

    You can replace the insertBefore method before the Google script invokes it:

    http://jsfiddle.net/coma/7st6d9p2/

    var head = document.getElementsByTagName('head')[0];
    
    // Save the original method
    var insertBefore = head.insertBefore;
    
    // Replace it!
    head.insertBefore = function (newElement, referenceElement) {
    
        if (newElement.href && newElement.href.indexOf('//fonts.googleapis.com/css?family=Roboto') > -1) {
    
            console.info('Prevented Roboto from loading!');
            return;
        }
    
        insertBefore.call(head, newElement, referenceElement);
    };
    
    // Check it!
    new google.maps.Map(document.getElementById('map'), {
        center           : new google.maps.LatLng(51.508742,-0.120850),
        zoom             : 16,
        mapTypeId        : google.maps.MapTypeId.ROADMAP,
        streetViewControl: false,
        zoomControl      : false,
        panControl       : false,
        mapTypeControl   : false
    });
    

提交回复
热议问题