0

Google マップ アプリのグローバル オブジェクトとは?

https://developers.google.com/maps/documentation/javascript/examples/map-geolocationで js を書き直しました

次のJavaScriptを生成したcoffeescriptで:

// 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を使用するとアプリは機能しますが、coffeescriptによって生成されたjsを使用すると機能しません。私の推測では、マップ変数はコード内のグローバル変数であるため、グローバル オブジェクトにもバインドする必要があります。試してみwindow.mapましたが、それもうまくいきませんでした。何か案は?

4

1 に答える 1

0

handleNoGeolocation関数定義が正しくコンパイルされていません。だったはず

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

それよりも

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

インデントに何か問題があったと思います。

お役に立てれば。

于 2013-07-28T19:11:20.603 に答える