首页 > 解决方案 > 此处地图未显示在 Ionic 选项卡中

问题描述

我在 Ionic 水龙头中有一个非常基本的 Here Map,它使用这个 JavaScript 加载到 Ionic 选项卡中。

var platform = new H.service.Platform({
    useCIT: true,
    'app_id': $(component).data('appid'),
    'app_code': $(component).data('appcode'),
    useHTTPS: true
    });

    // Obtain the default map types from the platform object
    var maptypes = platform.createDefaultLayers();

    // Instantiate (and display) a map object:
    var map = new H.Map(
    document.getElementById('mapContainer'),
    maptypes.normal.map,
    {
      zoom: 10,
      center: { lng: $(component).data('long'),
                lat: $(component).data('lat')
              }
    });

// Enable the event system on the map instance:
var mapEvents = new H.mapevents.MapEvents(map);

// Add event listeners:
map.addEventListener('tap', function(evt) {
  // Log 'tap' and 'mouse' events:
  console.log(evt.type, evt.currentPointer.type); 

当不在选项卡 1 中添加此地图时,地图未显示。我尝试并搜索了几件事,但它们仅适用于谷歌地图。我怎样才能让它在 Ionic 选项卡中工作?

标签: ionic-frameworktabshere-api

解决方案


请尝试以下代码片段:

function moveMapToBerlin(map){
  map.setCenter({lat:52.5159, lng:13.3777});
  map.setZoom(14);
}

/**
 * Boilerplate map initialization code starts below:
 */

//Step 1: initialize communication with the platform
// In your own code, replace variable window.apikey with your own apikey
var platform = new H.service.Platform({
  apikey: window.apikey
});
var defaultLayers = platform.createDefaultLayers();

//Step 2: initialize a map - this map is centered over Europe
var map = new H.Map(document.getElementById('map'),
  defaultLayers.vector.normal.map,{
  center: {lat:50, lng:5},
  zoom: 4,
  pixelRatio: window.devicePixelRatio || 1
});
// add a resize listener to make sure that the map occupies the whole container
window.addEventListener('resize', () => map.getViewPort().resize());

//Step 3: make the map interactive
// MapEvents enables the event system
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));

// Create the default UI components
var ui = H.ui.UI.createDefault(map, defaultLayers);

// Now use the map as required...
window.onload = function () {
  moveMapToBerlin(map);
}                  


推荐阅读