JavaScript 信息窗口未显示
JavaScript InfoWindow not Displaying
在使用Google Map API时,我希望将infoWindow应用于我的第一个标记,但是它没有显示。我试图四处寻找帮助,但似乎没有任何效果。有什么想法吗?感谢您的帮助。
function initialize() {
var myLatlng = new google.maps.LatLng(51.843143, -2.643555),
map = new google.maps.Map(document.getElementById('map'), {
zoom: 18,
center: myLatlng
}),
var infowindow = new google.maps.InfoWindow({
content: "We are based here."
});
marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: "We are based here."
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
var accessPoint1 = new google.maps.LatLng(51.840913, -2.638603),
marker1 = new google.maps.Marker({
position: accessPoint1,
map: map,
title: "Access Point 1"
});
var accessPoint2 = new google.maps.LatLng(51.840913, -3.638603),
marker2 = new google.maps.Marker({
position: accessPoint2,
map: map,
title: "Access Point 2"
});
map.controls[google.maps.ControlPosition.TOP_CENTER].push($("#findButton")[0]);
function successCallback(position) {
var latlng = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude),
myOptions = {
zoom: 3,
center: latlng,
mapTypeControl: false,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
},
bounds = new google.maps.LatLngBounds(latlng);
bounds.extend(marker.getPosition());
map.setOptions(myOptions);
map.fitBounds(bounds);
new google.maps.Marker({
position: latlng,
map: map,
title: "You are here!",
icon: 'http://maps.gstatic.com/mapfiles/markers2/boost-marker-mapview.png'
});
}
function errorCallback() {
alert("I'm afraid your browser does not support geolocation.");
}
function findMe() {
$(this).hide();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successCallback, errorCallback, {
timeout: 10000
});
} else {
alert("I'm afraid your browser does not support geolocation.");
}
}
$("#findButton").click(findMe);
}
google.maps.event.addDomListener(window, 'load', initialize);
我收到一个javascript错误,Uncaught SyntaxError: Unexpected token var
发布的代码。不能在逗号分隔的列表中间穿插带有分号的变量声明:
var myLatlng = new google.maps.LatLng(51.843143, -2.643555),
map = new google.maps.Map(document.getElementById('map'), {
zoom: 18,
center: myLatlng
}), //** comma
var infowindow = new google.maps.InfoWindow({
content: "We are based here."
}); // semicolon, terminates the line
工作代码片段:
function initialize() {
var myLatlng = new google.maps.LatLng(51.843143, -2.643555),
map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: myLatlng
}),
infowindow = new google.maps.InfoWindow({
content: "We are based here."
}),
marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: "We are here!"
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
var accessPoint1 = new google.maps.LatLng(51.840913, -2.638603),
marker1 = new google.maps.Marker({
position: accessPoint1,
map: map,
title: "Access Point 1"
});
map.controls[google.maps.ControlPosition.TOP_CENTER].push($("#findButton")[0]);
function successCallback(position) {
var latlng = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude),
myOptions = {
zoom: 3,
center: latlng,
mapTypeControl: false,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
},
bounds = new google.maps.LatLngBounds(latlng);
bounds.extend(marker.getPosition());
map.setOptions(myOptions);
map.fitBounds(bounds);
new google.maps.Marker({
position: latlng,
map: map,
title: "You are here!",
icon: 'http://maps.gstatic.com/mapfiles/markers2/boost-marker-mapview.png'
});
}
function errorCallback() {
alert("I'm afraid your browser does not support geolocation.");
}
function findMe() {
$(this).hide();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successCallback, errorCallback, {
timeout: 10000
});
} else {
alert("I'm afraid your browser does not support geolocation.");
}
}
$("#findButton").click(findMe);
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
height: 500px;
width: 500px;
margin: 0px;
padding: 0px
}
<script src="http://maps.google.com/maps/api/js"></script>
<div id="map" style="border: 2px solid #3872ac;"></div>
相关文章:
- 为什么不显示警报窗口
- 如何在不使用iframe或库的情况下创建剪切显示窗口区域
- 单击角度谷歌地图中的多边形时如何显示窗口
- 单击时显示窗口警报
- 在 EXT JS 中显示窗口
- 如果用户已关闭,则不显示窗口
- JS:这有什么问题?尝试单击链接以显示窗口.提示符
- 如何在谷歌浏览器中使用 JavaScript 显示窗口打印对话框
- 如何在javascript中显示窗口位置,当localStorage但不重新加载页面时
- 自动缩放和自动居中显示窗口中的所有标记
- 不要在页面启动时显示窗口
- 如果响应有效,则显示窗口警报
- Twitter Bootstrap Modal在Rails中不显示窗口
- 暂停在执行拖放时显示窗口的拖放事件的处理;在网格之间下降
- 多次创建和显示窗口时出现问题
- 在标记's上显示窗口点击Angular谷歌地图
- 使用adobeair在最大屏幕上显示窗口
- 在鼠标上显示窗口的时间间隔为2秒
- 如何以最快的方式显示窗口
- 当Iframe改变/加载时显示窗口