gpt4 book ai didi

javascript - 未显示/使用 Google map 时出现控制台错误

转载 作者:行者123 更新时间:2023-12-01 03:59:10 25 4
gpt4 key购买 nike

我的联系页面上有一个 Google map ,它工作正常,没有控制台错误。但是在其他页面上我收到以下错误消息,可能是因为它找不到 map :

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)

我看过一些帖子,但其中大多数似乎都记录了如何让 map 工作/显示,但在这种情况下,页面上并不意味着有 map 。

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

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

这是我的 Google map 脚本供引用:

/* 
* 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/map-marker@2x.png", 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
});
}

希望有人能帮忙!

最佳答案

通过简单的 if/else 查询来检测页面上是否存在“ map ”id 标记是否可以解决该错误?

例如:

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

或者:

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

关于javascript - 未显示/使用 Google map 时出现控制台错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42345211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com