js: the environment was captured during function creation. How can I overcome this?
I'm running the following script that does geocoding:
function find_callback(response){
var map = g_waze_map.map;
var first_result = response[0];
var lonlat = new OpenLayers.LonLat(first_result.location.lon,first_result.location.lat);
g_waze_map.map.setCenter(lonlat);
var size = new OpenLayers.Size(36,47);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var icon = new OpenLayers.Icon('http://www.waze.co.il/images/home.png',size,offset);
var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
markers.addMarker(new OpenLayers.Marker(lonlat,icon));
map.addPopup(new OpenLayers.Popup.Framed开发者_JAVA技巧Cloud("test",lonlat,null,
"<div style='font-family:Arial,sans-serif;font-size:0.8em;'>"+first_result.name+"<div>",
anchor=null,true,null));
};
When I invoke the 3 calls to "find" in a batch (as part of my JS) I get a map with only the 3rd piont on it. When I invoke them via chrome console (one by one) I get a map with all 3 point on it. Is it the call_back that holds its environment? If so, how can I overcome this?
function onInit(){
g_waze_map.find('<%#Locations[2]%>','find_callback');
g_waze_map.find('<%#Locations[3]%>','find_callback');
g_waze_map.find('<%#Locations[5]%>','find_callback');
}
This script looks very strange to me. I'm assuming you only have one map
object (g_waze_map.map
) but for each callback you do g_waze_map.map.setCenter(lonlat);
where lonlat
is the latitude longitude of the first result. That doesn't make any sense.
If you calls to g_waze_map.find
are asynchronous then you don't know what order they will finish in so I have no idea why you'd want to setCenter
.
Also, why not just:
g_waze_map.find('<%#Locations[2]%>', find_callback);
g_waze_map.find('<%#Locations[3]%>', find_callback);
g_waze_map.find('<%#Locations[5]%>', find_callback);
// without the quotes, find_callback is a function afterall
My work-around was: sending an array of location to the g_waze_map.find(); That achived my goal, but I yet don't understand why it didn't work in the original way.
精彩评论