2013-07-28 17 views
0

什么是Google地图应用中的全局对象?谷歌地图全球对象;由coffeescript生成的js不起作用

我改写了JS在CoffeeScript中

,其产生以下的javascript:

// Generated by CoffeeScript 1.6.3 
(function() { 
    var errorFlag, initialize; 

    google.maps.visualRefresh = true; 

    initialize = function() { 
    var map, mapOptions; 
    mapOptions = { 
     zoom: 15, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
    if (navigator.geolocation) { 
     return navigator.geolocation.getCurrentPosition(function(position) { 
     var infowindow, pos; 
     pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
     infowindow = new google.maps.InfoWindow({ 
      map: map, 
      position: pos, 
      content: 'Location found' 
     }); 
     return map.setCenter(pos); 
     }, function() { 
     return handleNoGeolocation(true); 
     }); 
    } else { 
     return handleNoGeolocation(false); 
    } 
    }; 

    handleNoGeolocation(errorFlag = function() { 
    var content, infowindow, options; 
    if (errorFlag) { 
     content = 'Geolocation failed'; 
    } else { 
     content = 'Your browser does not support Geolocation'; 
    } 
    options = { 
     map: map, 
     position: new google.maps.LatLng(60, 105), 
     content: content 
    }; 
    infowindow = new google.maps.InfoWindow(options); 
    return map.setCenter(options.position); 
    }); 

    google.maps.event.addDomListener(window, 'load', initialize); 

}).call(this); 

该应用程序的工作原理,当我使用JS从他们的网站,而不是当我用所产生的JS由coffeescript。我的猜测是,因为map变量是代码中的全局变量,所以我应该将它绑定到全局对象。我试过window.map,但那也没用。有任何想法吗?

回答

0

handleNoGeolocation函数定义被错误编译。这本来是

var errorFlag, initialize, handleNoGeolocation; 
//.. 
//.. 
handleNoGeolocation = function (errorFlag) { 
    //.. 
    //.. 
}; 

而不是

handleNoGeolocation(errorFlag = function() { 
    //.. 
    //.. 
}); 

我猜出了点问题缩进。

希望这有助于。

+0

有帮助,非常感谢! – tldr

+0

欢迎您:) – shakib