【问题标题】:ionic not rendering google maps on first load离子在第一次加载时不渲染谷歌地图
【发布时间】:2015-06-23 13:55:10
【问题描述】:

我有一个应用程序,它是一个欢迎/登录屏幕,在您签名后,您会进入一个包含四个标签的标签屏幕,其中第一个标签显示谷歌地图。

第一次登录页面时不会显示它自己,但在刷新页面时,地图会像预期的那样优雅地显示第一次。每次都会发生这种情况,但我只收到一个 javascript 错误。

错误

Uncaught Error: can't load XRegExp twice in the same frame(anonymous function) @ xregexp-min.js:2

代码

function initialize() {
  var mapOptions = {
    zoom: 16,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(
    document.getElementById("map"),
    mapOptions
  );
  var geocoder = new google.maps.Geocoder();

  $scope.map = map;
  $scope.centerOnMe();
}
google.maps.event.addDomListener(window, 'load', initialize);

$scope.centerOnMe = function() {
  if(!$scope.map) {
    return;
  }

  $scope.loading = $ionicLoading.show({
    content: 'Getting current location...',
    showBackdrop: false
  });

  // get user location
  $cordovaGeolocation
  .getCurrentPosition({
    timeout:10000,
    enableHighAccuracy:true
  })
  .then(function(position) {
    $ionicLoading.hide();
    // we found the position
    $scope.map.setCenter(
      new google.maps.LatLng(
        position.coords.latitude, 
        position.coords.longitude
      )
    );
  }, function(e) {
    $ionicLoading.hide();
    console.log(e);
    alert("Uh oh, we can't find your position! Is your GPS enabled?\n\n" + e);
  });
};

编辑

这是我最新的 javascript 错误,在将 google init 替换为 ionic init 之后。我明白发生了什么,但我不知道应该将该函数声明移到哪里。我把它移到了 init 里面,但它仍然没有用。我应该设置超时吗?

TypeError: $scope.loadLocations is not a function
    at initialize (controllers.js:179)
    at Object.ionic.Platform.ready (ionic.bundle.js:2120)
    at new <anonymous> (controllers.js:182)
    at invoke (ionic.bundle.js:12468)
    at Object.instantiate (ionic.bundle.js:12476)
    at ionic.bundle.js:16745
    at IonicModule.controller.self.appendViewElement (ionic.bundle.js:47716)
    at Object.IonicModule.factory.ionicViewSwitcher.create.switcher.render (ionic.bundle.js:45973)
    at Object.IonicModule.factory.ionicViewSwitcher.create.switcher.init (ionic.bundle.js:45893)
    at IonicModule.controller.self.render (ionic.bundle.js:47590)

【问题讨论】:

    标签: google-maps ionic-framework cordova-plugins ngcordova


    【解决方案1】:

    您不需要 $scope.centerOnMe(); 进行初始化。如果这样做,则编译不一致;参考例子你就会明白。

    另外,在某些情况下,ionic 不适用于 DOM。

    在容器内使用$scope.init 而不是function initialize() 加载地图。

    This 回答详细解释。

    另一种方法是将google.maps.event.addDomListener(window, 'load', initialize); 替换为ionic.Platform.ready(initialize);。示例here

    【讨论】:

    • $scope.init 不起作用,但 ionic.Platform.ready 起作用。现在我发现我的自定义函数在运行 init 时没有声明(在第一次运行时)。对此有何建议?我将js错误放在编辑中。
    • 您可以查看:mcgivery.com/structure-of-an-ionic-app。使用最适合您的要求。
    猜你喜欢
    • 1970-01-01
    • 2020-09-12
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多