gpt4 book ai didi

javascript - '未捕获无效值错误 : setContent: not a string; and [object Console]' error in Google Map

转载 作者:行者123 更新时间:2023-11-30 12:34:18 24 4
gpt4 key购买 nike

我有以下页面使用 Google Maps API 显示 Google map

<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
#map{
width:400px;
height:400px;
border:2px solid black;
margin-top:20px;
}
</style>
</head>
<body>
<div id="location">
<p>Your location will be displayed here</p>
</div>
<div id="map"></div>
<script>
var map;
var div = document.getElementById("location");
window.onload = getMyLocation;
function getMyLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(displayLocation,displayError);
}else{
alert("Sorry, your browser does not support geolocation");
}
}

function displayLocation(position){
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
div.innerHTML = "You are at Latitude: "+latitude+", Longitude: "+longitude;
showMap(position.coords);
}

function displayError(error){
if(error.code==0){
div.innerHTML="An unknown error occurred.";
}else if(error.code==1){
div.innerHTML="You denied the request for Geolocation.";
}else if(error.code==2){
div.innerHTML="Location information is unavailable.";
}else if(error.code==3){
div.innerHTML="The request to get user location timed out.";
}
}

function showMap(coords){
var googleLatAndLong = new google.maps.LatLng(coords.latitude,coords.longitude);
var mapOptions = {
zoom:10,
center:googleLatAndLong,
maptypeId:google.maps.MapTypeId.ROADMAP
};
var mapDiv=document.getElementById("map");
map = new google.maps.Map(mapDiv,mapOptions);
var title="Your location";
var content = "You are here: "+coords.latitude+", "+coords.longitude;
addMarker(map,googleLatAndLong,title,console);
}

function addMarker(map,latlong,title,content){
var markerOptions = {
position:latlong,
map:map,
title:title,
clickable:true
};
var marker = new google.maps.Marker(markerOptions);

var infoWindowOptions = {
content:content,
position:latlong
};
var infoWindow = new google.maps.InfoWindow(infoWindowOptions);
google.maps.event.addListener(marker,"click",function(){
infoWindow.open(map);
});
}

</script>
<script src="https://maps.google.com/maps/api/js?sensor=true"></script>
</body>
</html>

注意:代码片段可能无法运行,因为 SO 阻止了 Geolocation API 的请求。

当我加载页面时, map 与标记正确显示,但是当我单击标记时没有任何反应(它应该显示一个窗口)并且 JS 控制台抛出以下错误

未捕获 InvalidValueError:setContent:不是字符串;和[对象控制台]

我读过类似的帖子,问题似乎是无论 Content 有什么值,它都不是字符串。唯一的内容变量在这里声明

var content = "You are here: "+coords.latitude+", "+coords.longitude;
addMarker(map,googleLatAndLong,title,console);
}

function addMarker(map,latlong,title,content){
var markerOptions = {
position:latlong,
map:map,
title:title,
clickable:true
};
var marker = new google.maps.Marker(markerOptions);

var infoWindowOptions = {
content:content,

我尝试将 coords.latitude 和 coords.longitude 转换为字符串(不是在这个版本的代码中),但没有任何区别。

关于造成这种情况的原因有什么想法吗?代码来自 Head First HTML5 编程,他们根本没有提到这个问题。

最佳答案

语法错误

我有

addMarker( map 、googleLatAndLong、标题、控制台);

应该是

addMarker( map 、googleLatAndLong、标题、内容);

不确定我从哪里得到“控制台”。

关于javascript - '未捕获无效值错误 : setContent: not a string; and [object Console]' error in Google Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26603083/

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