2012-12-17 6 views
9

をロードするJavaScriptの非同期API:予想を1としては、非同期Iは、Nokiaの地図をロードしようとしているNokiaの地図の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ので、(定義されていないということです。

new nokia.maps.map.Display(); 

があり、これを行うための方法を、または誰もがこれまで管理しています?私は実際に(もちろんの問題ではありません)非同期

おかげでマップを作成していない、ページで非同期に書き込みにスクリプトをしようとしている。そうするために、私は何か

EDITを欠落している可能性があります、

答えて

2

jHEREライブラリを見てください。滑らかな非同期ロードと、Nokia Maps APIで遊びたい場合は、他のクールな機能があります。 JavaScriptのための

+0

おかげ@lorisを、私は持っていますそれを見てください – tchap

+0

ここに行く:http://jsfiddle.net/3ZA7x/ –

+0

こんにちは、私はiOSとAndroidでノキアマップを統合するためにMH5で1つのアプリを開発しています。 mh5またはjHEREプラグインで可能 – user869123

8

HERE Maps APIを(3.0)

のJavaScriptの新しいHERE 3.0 Maps APIでは、きれいにモジュール化され、かつ完全に非同期読み込みをサポートしています。例えば、次のように簡単なマップをロードするためにrequirejsを使用することが可能である:

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ファイルを持っているため、特別なケースです。ヘッダーにデフォルトCSS(またはオーバーライド)を保持するか、またはrequire-cssのようなrequirejsプラグインを使用してロードすることができます。 ここをクリックしてJavaScriptライブラリをブラウザに初めてダウンロードするとタイムアウトしないようにするには、コンフィグレーションに行があることに注意する必要があります。あなたのインターネット接続を少なくとも1回。

jQueryを使って

または代わり:JavaScriptのための

$.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> 
 

 

Nokiaの地図API(2.2.4-2.5.4)

最近廃止予定バージョンNokia Maps API for JavaScript(バージョン2.2.4以降)は、次のように非同期ロードをサポートします

詳細は フィーチャーのFeature.loadメソッドにあります。負荷は、あなたが、あなたのApp ID and tokenに追加マップなど、失敗のための1つを表示し続けることができる場所(成功のための1つを2つのコールバックを持っている。

// 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>

+0

** Feature.load **のAPIリファレンスが明らかに[ここ]に移動しました(http:// developer .here.com/javascript-apis/documentation/maps/topics_api_pub/nokia.Features.html)。 – Veksi

+0

また、バージョン3の初めからロード機能が変更されていることに注目してください。詳細はhttp://developer.here.com/documentation/versionsをご覧ください。 – Veksi

+1

@Veski - 新しい** 3.x **バージョンが非同期に読み込む方がはるかに簡単であることを明確にするために答えを更新しました。私は新しいプロジェクトで廃止された** 2.x **を引き続き使用することを奨励しません –

関連する問題