Java脚本回调修改变量 - Google api geocode
我正在使用 Google 的地理编码/googlemaps api。我可以成功地对位置进行地理编码,并放置一个标记,但是我无法将纬度/经度放入回调函数之外的变量中
function init() {
var latLng = new Array();//Setup the cities lat + long in array
this.foo = 3;
function addGeo(me){//call back function
me.foo = 2;
return function (results, code){
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
me.foo = 7;
this.foo = 8;
}
}
var geo = new google.maps.Geocoder();
geo.geocode( {address: 'London', region: 'UK'}, addGeo(this));
var map = new google.maps.Map(document.getElementById("full_map"));
var latLngBounds = new google.maps.LatLngBounds( );
//For each city, add its location to the boundry and add a marker in that location
for ( var i = 0; i < latLng.length; i++ ) {
latLngBounds.extend( latLng[ i ] );
}
alert(this.foo);
map.setCenter(latLngBounds.getCenter( ));
map.fitBounds(latLngBounds);
map.setMapTypeId(google.maps.MapTypeId.TERRAIN);
}
这是我的完整代码,我可以使用 addGeo (回调函数)来编辑 foo 变量,但不在返回的函数内,这将警告 2(this.foo 的值),我这样做是为了将纬度和经度存储在 latLong 数组中,这样我就可以利用谷歌的 latLngBounds。
任何帮助将不胜感激。
编辑:
对于我感兴趣的人 me.latLngBounds.extend(结果[0].geometry.location); me.map.fitBounds(latLngBounds); (在主部分设置相关变量后) 在未命名的函数中,它的工作方式就像一种享受。
Im using Google's geocode/googlemaps api. I can succesfully geo-code a location, and place a marker, however im having trouble putting the lat/long in a varible outside the call back function
function init() {
var latLng = new Array();//Setup the cities lat + long in array
this.foo = 3;
function addGeo(me){//call back function
me.foo = 2;
return function (results, code){
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
me.foo = 7;
this.foo = 8;
}
}
var geo = new google.maps.Geocoder();
geo.geocode( {address: 'London', region: 'UK'}, addGeo(this));
var map = new google.maps.Map(document.getElementById("full_map"));
var latLngBounds = new google.maps.LatLngBounds( );
//For each city, add its location to the boundry and add a marker in that location
for ( var i = 0; i < latLng.length; i++ ) {
latLngBounds.extend( latLng[ i ] );
}
alert(this.foo);
map.setCenter(latLngBounds.getCenter( ));
map.fitBounds(latLngBounds);
map.setMapTypeId(google.maps.MapTypeId.TERRAIN);
}
Thats my full code, I can get addGeo (the callback function) to edit the foo varible, but not within the returned function, this will alert 2 (value of this.foo), I to do this to store the lat and long in latLong array so I can make use of googles latLngBounds.
Any help will be greatly appreciated.
EDIT:
For anyone intrested i put
me.latLngBounds.extend(results[0].geometry.location);
me.map.fitBounds(latLngBounds);
(after setting the relevent varibles in the main section)
within the unamed function, and it works like a treat.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
geocode
方法是异步(AJAX 中的A),这意味着它将立即返回,无需等待服务器的回复。因此,地理编码回调仅在代码的其余部分(当服务器回复时)之后执行。
在设置
foo
之前,您的alert(this.foo)
会在您收到回复之前运行。您需要将使用响应的所有代码移至回调中。
The
geocode
method is asynchronous (the A in AJAX), meaning that it will return immediately, without waiting for a reply from the server.Therefore, the geocode callback only executes after the rest of your code (when the server replies).
Your
alert(this.foo)
runs before you receive a reply, beforefoo
is set.You need to move all of the code that uses the response into the callback.