Jquery 未捕获引用错误:未定义mapCenterAt
我收到错误Uncaught ReferenceError:mapCenterAt未使用以下脚本定义:Jquery 未捕获引用错误:未定义mapCenterAt,jquery,google-maps,Jquery,Google Maps,我收到错误Uncaught ReferenceError:mapCenterAt未使用以下脚本定义: <script> jQuery(document).ready(function ($) { // Map Markers var mapMarkers = [{ address: "217 Summit Boulevard, Birmingham, AL 35243", html: "<strong>Alabama Of
<script>
jQuery(document).ready(function ($) {
// Map Markers
var mapMarkers = [{
address: "217 Summit Boulevard, Birmingham, AL 35243",
html: "<strong>Alabama Office</strong><br>217 Summit Boulevard, Birmingham, AL 35243<br><br>
<a href='#' onclick='mapCenterAt({latitude: 33.44792, longitude: -86.72963, zoom: 16}, event)'>[+] zoom here</a>",
icon: {
image: "img/pin.png",
iconsize: [48, 48],
iconanchor: [48, 48]
}
}];
// Map Initial Location
var initLatitude = 37.09024;
var initLongitude = -95.71289;
// Map Extended Settings
var mapSettings = {
controls: {
panControl: true,
zoomControl: true,
mapTypeControl: true,
scaleControl: true,
streetViewControl: true,
overviewMapControl: true
},
scrollwheel: false,
markers: mapMarkers,
latitude: initLatitude,
longitude: initLongitude,
zoom: 5
};
var map = $("#googlemaps").gMap(mapSettings);
// Map Center At
var mapCenterAt = function(options, e) {
e.preventDefault();
$("#googlemaps").gMap("centerAt", options);
}
});
</script>
jQuery(文档).ready(函数($){
//地图标记
变量映射标记=[{
地址:美国阿拉巴马州伯明翰市顶峰大道217号,邮编:35243,
html:阿拉巴马州办事处
阿拉巴马州伯明翰峰会大道217号,邮编35243
",
图标:{
图片:“img/pin.png”,
iconsize:[48,48],
iconanchor:[48,48]
}
}];
//地图初始位置
初始纬度=37.09024;
var initLongitude=-95.71289;
//映射扩展设置
变量映射设置={
控制:{
泛控制:对,
动物控制:对,
mapTypeControl:true,
scaleControl:对,
街景控制:对,
概览映射控件:true
},
滚轮:错误,
标记:地图标记,
纬度:初始纬度,
经度:经度,
缩放:5
};
VarMap=$(“#谷歌地图”).gMap(地图设置);
//地图中心
var mapCenterAt=函数(选项,e){
e、 预防默认值();
$(“#谷歌地图”).gMap(“centerAt”,选项);
}
});
mapCenterAt在脚本底部定义,但在页面上执行onclick时会引发错误。mapCenterAt是否需要以不同的方式定义才能与onclick一起工作?jQuery的document ready处理程序创建不同的作用域,因此内联onclick处理程序无法使用
mapCenterAt
函数,因为它位于全局作用域中,而全局作用域高于document ready
您必须使用全局作用域:
window.mapCenterAt = function(options, e) {
e.preventDefault();
$("#googlemaps").gMap("centerAt", options);
}
或创建适当的事件处理程序:
<script>
jQuery(document).ready(function ($) {
// Map Markers
var mapMarkers = [{
address: "217 Summit Boulevard, Birmingham, AL 35243",
html: "<strong>Alabama Office</strong><br>217 Summit Boulevard, Birmingham, AL 35243<br><br><a href='#' id='mapButton'>[+] zoom here</a>",
icon: {
image: "img/pin.png",
iconsize: [48, 48],
iconanchor: [48, 48]
}
}];
// Map Initial Location
var initLatitude = 37.09024;
var initLongitude = -95.71289;
// Map Extended Settings
var mapSettings = {
controls: {
panControl: true,
zoomControl: true,
mapTypeControl: true,
scaleControl: true,
streetViewControl: true,
overviewMapControl: true
},
scrollwheel: false,
markers: mapMarkers,
latitude: initLatitude,
longitude: initLongitude,
zoom: 5
};
var map = $("#googlemaps").gMap(mapSettings);
$(document).on('click', '#mapButton', function(e) {
e.preventDefault();
$("#googlemaps").gMap("centerAt", {latitude: 33.44792, longitude: -86.72963, zoom: 16});
});
});
</script>
jQuery(文档).ready(函数($){
//地图标记
变量映射标记=[{
地址:美国阿拉巴马州伯明翰市顶峰大道217号,邮编:35243,
html:“阿拉巴马州办公室
阿拉巴马州伯明翰峰会大道217号,邮编35243
”,
图标:{
图片:“img/pin.png”,
iconsize:[48,48],
iconanchor:[48,48]
}
}];
//地图初始位置
初始纬度=37.09024;
var initLongitude=-95.71289;
//映射扩展设置
变量映射设置={
控制:{
泛控制:对,
动物控制:对,
mapTypeControl:true,
scaleControl:对,
街景控制:对,
概览映射控件:true
},
滚轮:错误,
标记:地图标记,
纬度:初始纬度,
经度:经度,
缩放:5
};
VarMap=$(“#谷歌地图”).gMap(地图设置);
$(文档)。在('单击','映射按钮')上,函数(e){
e、 预防默认值();
$(“#谷歌地图”).gMap(“中心点,{纬度:33.44792,经度:-86.72963,缩放:16});
});
});
非常感谢,这就成功了!将把答案标记为正确。