我在google地图网页上看到错误Assertion failed: InvalidValueError: setMap: not an instance of Map; and not an instance of StreetViewPanorama
。然后我在其他地方读到了this question Stack Overflow,它告诉我需要一个google.maps.MAP
对象的实例。我认为通过调用该对象来初始化我将调用该对象的地图。为什么不是google.maps.Map的地图实例? InvalidValueError:setMap:不是Map的一个实例;
以前,我得到了错误i is not defined
所以我将createMarker
函数移到$.getJSON
函数中,它具有局部范围。
我需要在其他地方拨打google.mapsMap
吗?
我在做什么错?
HTML:
<body>
<h1 style="text-align: center;">Hello World</h1>
<div id="map"></div>
<ul id="list"></ul>
</body>
CSS:
#map {
height: 300px;
width: 600px;
border: 1px solid black;
margin: 0 auto;
}
的JavaScript:
function initialize(){
var mapProp = {
center: new google.maps.LatLng(38, -78),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map'), mapProp);
};
$(document).ready(function(){
var url = 'https://opendata.howardcountymd.gov/resource/96q9-qbh7.json';
initialize();
$.getJSON(url, function (data){
$.each(data, function(i, field) {
$('#list').append("<li>" + data[i].location.longitude + " & " + data[i].location.latitude + "</li>");
createMarker(data);
function createMarker (data) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data[i].location.latitude, data[i].location.longitude),
map: map,
title: field.crossroad
});
};
});
});
});
是所有加入'VAR地图;'为全局变量?我正在使用文本比较/脏标记,除了一些空白区域,我没有看到任何其他的东西,但你的工作,我没有。 – adin
,并从地图前面的“初始化”函数中删除了“var”,所以它会初始化该版本的变量,而不是创建一个新的函数本地。 – geocodezip
宾果,谢谢! – adin