在函数外使用变量是行不通的
using variable outside function wont work
我想在其他函数中提醒curlat变量,以便我可以在Google地图中使用它特性。但是我不能让它工作。
var getLocation = {
init: function () {
var curlat = "";
function onSuccess(position) {
curLat = position.coords.latitude;
};
function onError(error) {
alert('code: ' + error.code + ''n' + 'message: ' + error.message + ''n');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
}
var map = {
init: function () {
alert(curLat); // alert the cordinate here
//Google maps API initialisation
var element = document.getElementById("map");
//Define the properties of the OSM map to be displayed
var map = new google.maps.Map(element, {
center: new google.maps.LatLng(57, 21),
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true,
mapTypeControl: false,
streetViewControl: false
});
//Define OSM map type pointing at the OpenStreetMap tile server
map.mapTypes.set("OSM", new google.maps.ImageMapType({
getTileUrl: function (coord, zoom) {
return "http://tile.openstreetmap.org/" + zoom + "/" + coord.x + "/" + coord.y + ".png";
},
tileSize: new google.maps.Size(256, 256),
name: "OpenStreetMap",
maxZoom: 18
}));
}
}
首先,javascript是区分大小写的,所以curlat
应该是curLat
。
var curLat = "" ;
var getLocation = { ...
同样,正如其他人正确指出的那样。您应该延迟map
的初始化,直到curLat
被设置。也许您可以从success
处理程序调用.map.init
。
相关文章:
- 将函数的上下文应用于javascript变量
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 将PHP变量传递给jQuery时遇到问题
- 如何通过ajax刷新JSF填充的javascript变量
- 参数变量出现ngTable指令问题
- 通过javascript重定向html传递php变量
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 全局变量和全局对象的属性之间有什么区别吗
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 值对象在某个变量发生更改后发生更改
- 在Javascript中使用PHP变量是行不通的
- 将变量传递到 jQuery 中的函数中是行不通的
- 尝试创建大量变量和 for 循环是行不通的
- 通过 ajax 将 javascript 变量传递给 php 变量是行不通的
- 使用变量供以后在另一个函数中使用是行不通的
- 将jQuery选择器存储到变量中是行不通的
- 在函数外使用变量是行不通的
- 向Angular方法中添加动态变量似乎行不通
- 在JavaScript中增加Razor变量是行不通的