javascript - 异步加载诺基亚 map Javascript API

标签 javascript here-api

我正在尝试异步加载诺基亚 map javascript API:

var oScript  = document.createElement('script');
oScript.type = 'text/javascript';
oScript.async = true;
oScript.src  = "http://api.maps.nokia.com/2.2.3/jsl.js?with=maps,positioning,placesdata";
document.body.appendChild(oScript);

正如预期的那样,它不会立即起作用,所以我尝试覆盖 document.write 认为这可能是问题所在,但无济于事(例如,我这样做了 https://stackoverflow.com/a/7884407/1741150 )。

我遇到的错误是基本上没有定义 nokia.maps.map(因此,我无法使用以下方法创建 map :

new nokia.maps.map.Display();

有没有办法做到这一点,或者有人曾经设法做到这一点?我可能遗漏了什么

编辑:我实际上是在尝试在页面中异步编写脚本,而不是异步创建 map (这当然不是问题)

谢谢,

最佳答案

HERE JavaScript map API (3.0)

较新的 3.0 HERE Maps API for JavaScript 是很好的模块化,并且完全支持异步加载。例如,可以使用 requirejs加载一个简单的 map 如下:

  require(['mapsjsService','mapsjsEvents', 'mapsjsUi'], function () {

      var platform = new H.service.Platform({
          app_id: '<YOUR APP ID>',
          app_code: '<YOUR TOKEN>'
      });
      var defaultLayers = platform.createDefaultLayers();
      var map = new H.Map(document.getElementById('map'),
        defaultLayers.normal.map);
      var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
      var ui = H.ui.UI.createDefault(map, defaultLayers);
    });

配置文件需要赋值如下:

 requirejs.config({
    baseUrl: '.',
    waitSeconds: 0,
    map: {
      '*': {
        'css': 'require-css' // or whatever the path to require-css is
      }
    },
    paths: {
        'mapsjsCore' : 'https://js.api.here.com/v3/3.0/mapsjs-core',
        'mapsjsService' : 'https://js.api.here.com/v3/3.0/mapsjs-service',
        'mapsjsEvents' : 'https://js.api.here.com/v3/3.0/mapsjs-mapevents',
        'mapsjsUi' :'https://js.api.here.com/v3/3.0/mapsjs-ui',

        'mapsjsCss' :'https://js.api.here.com/v3/3.0/mapsjs-ui',
      },
      shim: {
        'mapsjsService': {
          deps: ['mapsjsCore']
        },
        'mapsjsEvents': {
          deps: ['mapsjsCore']
        },
        'mapsjsUi': {
          deps: ['mapsjsCore', 'css!mapsjsCss']
        }
      }
    });

可以看出,所有模块都依赖于mapsjsCore,但没有一个子模块相互依赖。 mapsjsUi 是一种特殊情况,因为它具有用于默认外观的关联 CSS 文件。您可以在 header 中保留默认 CSS(或您的覆盖),或者使用 requirejs 插件加载它,例如 require-css .应该注意的是,配置中需要行 waitSeconds:0 以避免在第一次将 HERE Maps for JavaScript 库下载到浏览器时超时,因为它们不会在本地找到,因此您至少有一次依赖互联网连接的速度。

或者使用 Jquery:

$.getScript('https://js.api.here.com/v3/3.0/mapsjs-core.js', function() {
  $.getScript('https://js.api.here.com/v3/3.0/mapsjs-service.js', function() {
    $.getScript('https://js.api.here.com/v3/3.0/mapsjs-mapevents.js', function() {
      $.getScript('https://js.api.here.com/v3/3.0/mapsjs-mapevents.js', function() {
        ////
        //
        // Don't forget to set your API credentials
        //
        var platform = new H.service.Platform({
          app_id: 'DemoAppId01082013GAL',
          app_code: 'AJKnXv84fjrb0KIHawS0Tg',
          useCIT: true
        });
        //
        //
        /////
        var defaultLayers = platform.createDefaultLayers();
        var map = new H.Map(document.getElementById('map'),
          defaultLayers.normal.map, {
            center: {
              lat: 50,
              lng: 5
            },
            zoom: 4
          });
        var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
        var ui = H.ui.UI.createDefault(map, defaultLayers);
      });
    });
  });
});
body {
       margin: 0;
       padding: 0;
     }

     #map {
       width: 100%;
       height: 100%;
       position: absolute;
       overflow: hidden;
       top: 0;
       left: 0;
     }
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
  <link rel="stylesheet" type="text/css"
    href="http://js.api.here.com/v3/3.0/mapsjs-ui.css" />
</head>
<body>
<div id="map"></div>
</body>

 

用于 JavaScript 的诺基亚 map API (2.2.4-2.5.4)

诺基亚 Maps API for JavaScript 的最近弃用版本(2.2.4 及以上版本)支持异步加载,如下所示

详细信息可以在 Feature.load 中找到API Reference 的方法 Feature.load 有两个回调,一个用于成功(您可以在其中继续添加 App ID and token ,显示 map 等),一个用于失败。

// this is our initial script that will load jsl.js
var oScript = document.createElement("script"),
  //once the jsl.js is load, we load all features
  onScriptLoad = function() {
    nokia.Features.load(
      // here we get all features (provide one or many "with" parameters
      nokia.Features.getFeaturesFromMatrix(["all"]),
      // an callback when everything was successfully loaded
      onApiFeaturesLoaded,
      // an error callback
      onApiFeaturesError,
      // a target document (or null if the current document applies)
      null,
      // a flag indicating that loading should be asynchronous
      false
    );
  },
  // once all features we loaded, we can instantiate the map
  onApiFeaturesLoaded = function() {

    /////////////////////////////////////////////////////////////////////////////////////
    // Don't forget to set your API credentials
    //
    // Replace with your appId and token which you can obtain when you 
    // register on http://api.developer.nokia.com/ 
    //
    nokia.Settings.set("appId", "YOUR APP ID");
    nokia.Settings.set("authenticationToken", "YOUR TOKEN");
    //          
    /////////////////////////////////////////////////////////////////////////////////////

    var mapContainer = document.getElementById("mapContainer");
    var map = new nokia.maps.map.Display(mapContainer, {
      center: [40.7127, -74.0059],
      zoomLevel: 13,
      components: [new nokia.maps.map.component.ZoomBar(),
        new nokia.maps.map.component.Behavior(),
      ]
    });
  },
  // if an error occurs during the feature loading
  onApiFeaturesError = function(error) {
    alert("Whoops! " + error);
  };

oScript.type = "text/javascript";
// NOTE: tell jsl.js not to load anything by itself by setting "blank=true"
oScript.src = "http://api.maps.nokia.com/2.2.4/jsl.js?blank=true";
// assign the onload handler
oScript.onload = onScriptLoad;

//finally append the script
document.getElementsByTagName("head")[0].appendChild(oScript);
     body {
       margin: 0;
       padding: 0;
     }

     #mapContainer {
       width: 100%;
       height: 100%;
       position: absolute;
       overflow: hidden;
       top: 0;
       left: 0;
     }
<!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8">
            <title>Asynchronous Loading</title>
        </head>
        <body>
            <div id="mapContainer"></div>

       </body>
    </html>

关于javascript - 异步加载诺基亚 map Javascript API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13922469/

相关文章:

javascript - React Native - React Native Firebase - Google 登录 Android

javascript - 路径更改时 Webpack 从错误的 URL 加载

javascript - 这里的信息气泡 map 不起作用

javascript - HERE map 按 ID 查找标记问题

javascript - 如何过滤和省略 d3.js 的输入数据

javascript - 如何使用子上下文可访问的自定义属性扩展模板绑定(bind)语法?

连接到 MVC Controller 时 JavaScript POST 失败

java - HERE map API 缺少库错误

android - 使用这里的sdk时出现内存不足错误

android - 如何在 HERE Maps Android SDK 中减小 APK 大小