2017-02-20 72 views
0

我的联系人页面上有Google地图,并且工作正常,没有控制台错误。然而在其他页面上,我得到以下错误消息,大概是因为它无法找到地图:未显示/使用Google地图时的控制台错误

Uncaught TypeError: Cannot read property 'firstChild' of null 
    at Object._.$f (js?key=MY-KEY:81) 
    at new tg (js?key=MY-KEY:87) 
    at init (main.js:292) 

我见过几个职位,但大多数人似乎越来越记录在地图的工作/显示器,但在这种情况下,并不意味着在页面上显示地图。

我想这可以通过在联系页面上调用javascript来解决,但脚本在我的全局.js文件中,并且它被加载到每个页面上。

有没有办法解决这个错误,将脚本保存在全局的外部文件中?

这里是我的谷歌地图的脚本供参考:

/* 
* When the window has finished loading create our google map below. 
*/ 

google.maps.event.addDomListener(window, 'load', init); 

/* 
* Basic options for a simple Google Map. For more options see: 
* https://developers.google.com/maps/documentation/javascript/reference#MapOptions 
* 
* 1. How zoomed in you want the map to start at (always required) 
* 2. The latitude and longitude to center the map (always required). 
* 3. How you would like to style the map. This is where you would paste any 
* style found on Snazzy Maps. 
* 4. Prevent map from being draggable. 
* 5. Hide map/satellite toggle. 
* 6. Hide "Street View" button. 
*/ 

function init() { 

    var mapOptions = { 
     zoom: 15, /* [1] */ 
     center: new google.maps.LatLng(LAT-VALUE, LNG-VALUE), /* [2] */ 
     draggable: false, /* [4] */ 
     mapTypeControl: false, /* [5] */ 
     streetViewControl: false, /* [6] */ 
     styles: /* [3] */ 
      [{"stylers":[{"saturation":-100},{"gamma":1}]},{"elementType":"labels.text.stroke","stylers":[{"visibility":"off"}]},{"featureType":"all","elementType":"geometry.fill","stylers":[{"weight":"2.00"}]},{"featureType":"all","elementType":"geometry.stroke","stylers":[{"color":"#9c9c9c"}]},{"featureType":"all","elementType":"labels.text","stylers":[{"visibility":"on"}]},{"featureType":"landscape","elementType":"all","stylers":[{"color":"#f2f2f2"}]},{"featureType":"landscape","elementType":"geometry.fill","stylers":[{"color":"#ffffff"}]},{"featureType":"landscape.man_made","elementType":"geometry.fill","stylers":[{"color":"#ffffff"}]},{"featureType":"poi","elementType":"all","stylers":[{"visibility":"off"}]},{"featureType":"road","elementType":"all","stylers":[{"saturation":-100},{"lightness":45}]},{"featureType":"road","elementType":"geometry.fill","stylers":[{"color":"#eeeeee"}]},{"featureType":"road","elementType":"labels.text.fill","stylers":[{"color":"#7b7b7b"}]},{"featureType":"road","elementType":"labels.text.stroke","stylers":[{"color":"#ffffff"}]},{"featureType":"road.highway","elementType":"all","stylers":[{"visibility":"simplified"}]},{"featureType":"road.arterial","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"transit","elementType":"fill","stylers":[{"weight":"1.00"}]},{"featureType":"transit","elementType":"labels.icon","stylers":[{"visibility":"on"},{"hue":"#a3cd39"},{"gamma":1},{"saturation":"50"}]},{"featureType":"water","elementType":"all","stylers":[{"color":"#46bcec"},{"visibility":"on"}]},{"featureType":"water","elementType":"geometry.fill","stylers":[{"color":"#c8d7d4"}]},{"featureType":"water","elementType":"labels.text.fill","stylers":[{"color":"#070707"}]},{"featureType":"water","elementType":"labels.text.stroke","stylers":[{"color":"#ffffff"}]}] 
    }; 

    /* 
    * Get the HTML DOM element that will contain your map. We are using a div with 
    * id="map" seen below in the <body> 
    */ 

    var mapElement = document.getElementById('map'); 

    /* 
    * Create the Google Map using our element and options defined above. 
    * 
    * 1. Map varible. 
    * 2. Marker variable so we can specify a retina image and resize. 
    */ 

    var map = new google.maps.Map(mapElement, mapOptions); /* [1] */ 
    var mapIcon = new google.maps.MarkerImage("img/interface/[email protected]", null, null, null, new google.maps.Size(100,78)); /* [2] */ 

    /* 
    * Let's also add a marker while we're at it. 
    */ 

    var marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(LAT-VALUE, LNG-VALUE), 
     map: map, 
     flat: true, 
     title: 'TITLE-HERE', 
     icon: mapIcon 
    }); 
} 

希望有人能帮助!

+0

行'google.maps.event.addDomListener(window,'load',init)''不会失败吗?我认为不应该定义Google对象(或'google.maps')。 –

回答

2

将简单的if/else查询检测页面上是否存在'map'id标记可解决错误?

例如:

var mapElement = document.getElementById('map'); 
if (mapElement !== null) { 
    // draw the map 
} 

或者:

if (document.getElementById('map')) { 
    // draw the map 
} 
+0

这两种方法有没有优先选择?我想我只需要包装'google.maps.event.addDomListener(window,'load',init);'在里面? – user1406440

+0

两者大致相同;在我看来,顶层方法更具可读性。如果地图ID不存在,底部也会返回'null'(参见[这里](https://developer.mozilla.org/en-US/docs/Glossary/Falsy)了解更多关于Falsy值的信息)。 – CodeMacabre

+0

感谢的人,似乎做的工作! :) – user1406440

0

变种mapElement =的document.getElementById( '映射');

if(mapElement != undefined || mapElement != '') 
{ 
    var map = new google.maps.Map(mapElement, mapOptions); /* [1] */ 
    var mapIcon = new google.maps.MarkerImage("img/interface/map-   [email protected]", null, null, null, new google.maps.Size(100,78)); 

    var marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(LAT-VALUE, LNG-VALUE), 
    map: map, 
    flat: true, 
    title: 'TITLE-HERE', 
    icon: mapIcon 
    }); 
} 
+0

请给我的答案添加描述。 –

相关问题