简体   繁体   English

如何确保我的应用程序可以在没有互联网连接的情况下与外部库(例如Google Maps)一起使用?

[英]How can I ensure that my app works with external libraries like Google Maps without an internet connection?

I am currently working on a mobile app using version 1 of the Ionic framework. 我目前正在使用Ionic框架的版本1开发移动应用程序。 In this app I am using Google Maps to display a map. 在此应用程序中,我正在使用Google Maps显示地图。 The map is being loaded using a script tag and works well while the device is connected to the internet. 该地图正在使用脚本标签加载,并且在设备连接到互联网时效果很好。 But if the app is started without an internet connection, the app won't even open. 但是,如果该应用程序在没有互联网连接的情况下启动,则该应用程序甚至无法打开。 How can I solve this problem? 我怎么解决这个问题?

I am not sure why an Ionic app refuses to open at all when a library cannot be loaded, but luckily this issue can be solved. 我不确定在无法加载库时为何Ionic应用程序根本拒绝打开,但是幸运的是可以解决此问题。

TL;DR: You have to load Google Maps asynchronously, so check whether there is a network connection and if there is load the library and otherwise show an error. TL; DR:您必须异步加载Google Maps,因此请检查是否存在网络连接以及是否加载了库,否则显示错误。


There are many examples of how to load the Google Maps library asynchronously, like this post from Google , or this question which contains an answer to your question or this question in which the differences between loading using the script tag and loading asynchronously are explained . 有许多示例说明如何异步加载Google Maps库,例如Google的帖子 ,或包含您的问题此问题 的答案的 问题,其中说明了使用script标签加载和异步加载之间的区别 There is also this question in which it is shown using plain JavaScript . 还有一个问题,使用纯JavaScript进行显示

Now of course that is a lot of text to go through, so here is the summary. 现在当然要处理很多文本,因此这里是摘要。 Instead of adding a script tag in index.html , you have to load the Google Maps library only when there is an internet connection and load the library using JavaScript. 不必在index.html中添加脚本标签,而仅在互联网连接时加载Google Maps库,然后使用JavaScript加载该库。 In order to check for an active internet connection, you are going to need cordova-plugin-network-information , which you can find here . 为了检查有效的Internet连接,您将需要cordova-plugin-network-information ,您可以在此处找到。 You can also use ngCordova to make it easier to use this plugin, you can find the documentation here . 您还可以使用ngCordova使其更易于使用此插件,您可以在此处找到文档。

In your controller, you can do something like this: 在您的控制器中,您可以执行以下操作:

.controller('MapCtrl', function($state, $scope, $window, $ionicPlatform) {

  // This view event is fired only when the view is loaded for the first time.
  $scope.$on("$ionicView.loaded", function(scopes, states) {
    var isMapLoaded = false;
    var networkState = navigator.connection.type;
    var isOnline = (networkState !== Connection.UNKNOWN && networkState !== Connection.NONE);

    if(isOnline) {
      // If there is a network connection, we load the map.
      var mapsURL = 'https://maps.googleapis.com/maps/api/js?key=[INSERT API KEY HERE]&callback=';

      $window.mapLoaded = function() {
        isMapLoaded = true;
        console.log("The map has been loaded.");
      }

      function loadMapsLibrary(url, callbackFunctionString) {
        var script = document.createElement('script');

        script.type = 'text/javascript';
        script.src = url + callbackFunctionString;

        document.body.appendChild(script);
      }

      $scope.isMapLoaded = isMapLoaded;

      loadMapsLibrary(mapsURL, "mapLoaded");
    } else {
      // It's up to you what you do here, you can show an error for example.
    }

In your view, you may have to use the ng-if directive and the isMapLoaded value to reload the <div> containing the map to ensure that it loads. 在您看来,您可能必须使用ng-if指令和isMapLoaded值来重新加载包含地图的<div>以确保已加载。 This should give you a basic idea of what you need to do to get your app working again. 这应该使您基本了解如何使应用重新运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM