Javascript-返回变量失败
我正试图做一些应该很简单的事情——但这让我头疼。 我似乎无法从函数中获取返回的变量Javascript-返回变量失败,javascript,variables,geolocation,Javascript,Variables,Geolocation,我正试图做一些应该很简单的事情——但这让我头疼。 我似乎无法从函数中获取返回的变量 var where; if (navigator.geolocation) { where = navigator.geolocation.getCurrentPosition(function (position) { // okay we have a placement - BUT only show it if the accuracy is lower than 500 mtrs
var where;
if (navigator.geolocation) {
where = navigator.geolocation.getCurrentPosition(function (position) {
// okay we have a placement - BUT only show it if the accuracy is lower than 500 mtrs
//if (position.coords.accuracy <= 500 ) {
// put their lat lng into google
var meLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var image = "images/map_blue.png";
var myMarker = new google.maps.Marker({
position: meLatLng,
map: map,
title: "Your location, as provided by your browser",
icon: image,
zIndex: 50
});
//} // end 500 mtrs test
return meLatLng;
});
alert("A: "+where);
}
var-where;
if(导航器.地理位置){
其中=navigator.geolocation.getCurrentPosition(函数(位置){
//好的,我们有一个位置-但只有当精度低于500米时才显示
//如果(position.coords.accurrency简短回答,则为空
回答再长一点,好吧……你看,问题是你的匿名函数似乎会将它的值返回给getCurrentPosition
函数,而这个函数似乎根本不返回任何东西
为了使用返回值,您应该使用回调或在数据所在的位置处理数据
只需尝试将return meLatLng
替换为customCallBack(meLatLng)
,然后在该行的某个位置定义customCallBack
或者,由于您已经在全局范围中定义了where
,您可以尝试用where=melatng
替换return melatng
,然后从where=navigatior.geoloc..中删除where=
如果我的猜测是正确的,那么最后一种方法就行不通了,因为我认为getCurrentPosition
是异步的,这意味着只要调用它,就可以离开应用程序的标准流。这就是为什么首先要为它提供一个函数。简单回答,什么都没有
回答再长一点,好吧……你看,问题是你的匿名函数似乎会将它的值返回给getCurrentPosition
函数,而这个函数似乎根本不返回任何东西
为了使用返回值,您应该使用回调或在数据所在的位置处理数据
只需尝试将return meLatLng
替换为customCallBack(meLatLng)
,然后在该行的某个位置定义customCallBack
或者,由于您已经在全局范围中定义了where
,您可以尝试用where=melatng
替换return melatng
,然后从where=navigatior.geoloc..中删除where=
如果我的猜测是正确的,那么最后一个方法就不起作用了,因为我认为getCurrentPosition
是异步的,这意味着您在调用应用程序的标准流时就离开了它。这就是为什么您首先要为它提供一个函数的原因。是的,它似乎用完了mainflow,所以我必须研究这个回调想法..谢谢:)是的,它似乎用完了mainflow,所以我必须研究这个回调想法..谢谢:)