当鼠标悬停在某个元素上时,Yandex 地图无法完全加载。
以下是 html
<div class="map" data-long="50.6106903" data-lat="36.5612213" data-zoom="13" id="map1"></div>
这是 js
$(".map").on("mouseover", function() {
img = $(this).children("img");
img.remove();
id = $(this).attr("id");
ymaps.ready(function () {
mapZoom = $(this).attr("data-zoom");
lat = $(this).attr("data-lat");
long = $(this).attr("data-long");
console.log(id);
var myMap = new ymaps.Map(id, {
center: [long, lat],
zoom: mapZoom
}, {
searchControlProvider: 'yandex#search'
});
myPlacemark1 = new ymaps.Placemark([long, lat], {
hintContent: ''
});
myMap.geoObjects.add(myPlacemark1);
});
});
Yandex 地图无法完全加载(没有地图,全是灰色)。如何解决这个问题?
当鼠标悬停在地图上时,控制台的网络块中的行https ://api-maps.yandex.ru/services/coverage/v2/? l=map&ll=NaN,NaN&z=NaN... 表示初始化地图时缺少必要的参数 - 坐标和比例。当所有参数直接传给map时,可以正常工作:https://jsbin.com/bajevef/1/edit?html,js ,output 。
每次将鼠标悬停在地图块上时,当前代码也会重复创建地图,这可以在控制台中看到。查看我们按需创建地图的示例。
您还连接了过时的 API 2.0 版本(而不是 2.1 版本),并且没有在代码中指定 API 密钥。没有钥匙的卡也不需要工作,这也违反了服务条款。