gpt4 book ai didi

Javascript "initialize"未定义,但功能在那里

转载 作者:行者123 更新时间:2023-11-30 17:29:33 24 4
gpt4 key购买 nike

我正在尝试加载带有多个标记的谷歌地图。此代码在 onload 中调用:

    <body onload="initialize()">

这是有问题的 javascript,在 <script> 中部分:

    var map; 
var address = String("Chicago, Illinois")+" USA";
var lat = null;
var long = null;
var country = null;
var geocoder = new google.maps.Geocoder();
var oms = new OverlappingMarkerSpiderfier(map,{markersWontMove: true, markersWontHide: true, keepSpiderfied: true});
var iw = new google.maps.InfoWindow();
oms.addListener('click',function(marker) {
iw.setContent(marker.desc);
iw.open(map,marker);
});

var contentString = '+String("Soldier Field")+"<br>"+latlng+'
var infowindow = new google.maps.InfoWindow();
var marker, i, markerAddress;
var facilities = "Soldier Field";
var cities = "Chicago";
var states = "Illinois";
var coords = "41.862338,-87.615855";
var phones = "312-235-7000";
var url = "www.soldierfield.net";
var briefText = "descriptive text";
var lastUpdated = "2014-05-02";
var overallContactName = "John Smith";
var overallContactPhone = "312-235-7000";
var overallContactEmail = "john.smith@email.com";
var latlngStr;
var image = {
url: 'https://www.google.com/mapfiles/kml/paddle/ylw-stars.png',
size: new google.maps.Size(40, 40),
};

var address_components = null;
var tmpResult = null;
var markers = []


function initialize() {

var latlng = new google.maps.LatLng("41.862338", "-87.615855");
//check if address in the US
$.ajax({
url: 'https://maps.googleapis.com/maps/api/geocode/json?latlng='+{{lat}}+','+{{long}}+'&sensor=false',
async: false,
dataType: 'json',
success: function(data) {
$.each(data.results[0].address_components, function(index,comp) {
if (comp.types == 'country,political') {
country = comp.short_name;
return;
}
});
}
});
if (country == "US") {
var mapOptions = {zoom: 8,center: latlng, scrollwheel: false,};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

var circle = new google.maps.Circle({
center: latlng,
radius: {{search_distance}} * 1609.34,
strokeColor: "#0000FF",
strokeOpacity: 0.6,
strokeWeight: 0.5,
fillColor: "#0000FF",
fillOpacity: 0.05,
});
circle.setMap(map);
map.fitBounds(circle.getBounds());
drop(); // drop pins!
}
} // map initialized

function geocode(facility,city,state) {
$.ajax({
url: 'https://maps.googleapis.com/maps/api/geocode/json?address='+encodeURIComponent(facility)+"+"+encodeURIComponent(city)+"+"+encodeURIComponent(state)+'&sensor=false',
async: false,
dataType: 'json',
success: function(data) {
console.log('https://maps.googleapis.com/maps/api/geocode/json?address='+encodeURIComponent(facility)+"+"+encodeURIComponent(city)+"+"+encodeURIComponent(state)+'&sensor=false')
console.log(data.status)
lat = data.results[0].geometry.location.lat;
lng = data.results[0].geometry.location.lng;
address_components = data.results[0].address_components;

}
})
return [lat, lng, address_components];
}
function drop() {

for (j = 0; j < facilities.length; j++) {
setTimeout(function() {
addMarker();
}, j*200);
}
}
function addMarker() {
latlngStr = coords[j].split(",");
tmpResult = geocode(facilities[j],cities[j],states[j]);
lat = tmpResult[0]
lng = tmpResult[1]
address_components = tmpResult[2]

// clever solution to parse address_components: http://stackoverflow.com/a/16429122/3058197
var components = {};
jQuery.each(address_components, function(k,v1) {jQuery.each(v1.types, function(k2, v2){components[v2]=v1.long_name});})

latlng = new google.maps.LatLng(lat,lng);
marker = new google.maps.Marker({
position: latlng,
map: map,
title: facilities[j],
animation: google.maps.Animation.DROP,
}));
oms.addMarker(marker);
markers.push(marker);
}

我在 javascript 控制台中看到的错误是:

ReferenceError: initialize is not defined

有什么想法吗?谢谢

最佳答案

尝试将此添加到您的代码中:

document.getElementsByTagName('body')[0].onload = initialize;

这可能超出了范围。

关于Javascript "initialize"未定义,但功能在那里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23431653/

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