angular - ionic 3 : Baidu map working only in browser

标签 angular typescript ionic3 baidu baidu-map

在我使用 Ionic 3 开发的应用程序中,我使用 npm 包 angular2-baidu-map用于显示中国大陆 map 。

我有一个百度 map 的 API key (用于 JS API),并且该 map 在浏览器中工作正常(即 ionicserve -l),但是一旦编译并安装在真实设备, map 未显示。

经过一些调试后,发现无论我在协议(protocol)中设置什么,API都会向file://api.map.baidu.com发送请求> map 初始化选项。

例如,Safari 开发者工具的控制台会记录一些许多消息,例如:

The requested URL was not found on this server. file://api.map.baidu.com/api?v=2.0&ak=...&callback=baidumapinit&s=0 Failed to load resource: The requested URL was not found on this server.

编辑:添加代码

基本上我只是用演示代码尝试了这个插件。但是,为了完整起见,就在这里。

HTML 代码

<ion-header>
  <ion-navbar>
    <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button>
    <ion-title>Baidu map</ion-title>
  </ion-navbar>
</ion-header>

<ion-content>
<baidu-map ak="{{ak}}"
     [options]="opts" [offline]="offlineOpts" 
     (onMapLoaded)="loadMap($event)" 
     (onMarkerClicked)="clickMarker($event)" 
     (onClicked)="clickMap($event)"></baidu-map>
</ion-content>

typescript

map-baidu.module.ts

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { MapBaiduPage } from './map-baidu';
import { TranslateModule } from '@ngx-translate/core';
import { BaiduMapModule } from 'angular2-baidu-map';

@NgModule({
  declarations: [
    MapBaiduPage,
  ],
  imports: [
    IonicPageModule.forChild(MapBaiduPage),
    TranslateModule.forChild(),
    BaiduMapModule
  ],
})
export class MapBaiduPageModule {}

map -baidu.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { OfflineOptions, ControlAnchor, NavigationControlType } from 'angular2-baidu-map';

@IonicPage()
@Component({
  selector: 'page-map-baidu',
  templateUrl: 'map-baidu.html',
})
export class MapBaiduPage {

  public ak:string = '<your Baidu JS API key here>';
  opts: any;
  offlineOpts: OfflineOptions;

  constructor(public navCtrl: NavController, public navParams: NavParams) { }

  ionViewDidLoad() {
    console.log('ionViewDidLoad MapBaiduPage');
  }

  loadMap(map:any){
    console.log('> loadMap:', map);
  }
  clickMap(e:any){
    console.log('> clickMap:', e.point.lat, e.point.lng);
  }

  clickMarker(marker:any){
    console.log('> clickMarker:', marker);
  }

  ngOnInit() {
    this.opts = {
      // protocol:'https:', // changes nothing
      center: {
        longitude: 121.506191,
        latitude: 31.245554
      },
      zoom: 12,
      markers: [{
        longitude: 121.506191,
        latitude: 31.245554,
        title: 'Where',
        content: 'Put description here',
        enableDragging: true
      }],
      geolocationCtrl: {
        anchor: ControlAnchor.BMAP_ANCHOR_BOTTOM_RIGHT
      },
      scaleCtrl: {
        anchor: ControlAnchor.BMAP_ANCHOR_BOTTOM_LEFT
      },
      overviewCtrl: {
        isOpen: true
      },
      navCtrl: {
        type: NavigationControlType.BMAP_NAVIGATION_CONTROL_LARGE
      }
    };

    this.offlineOpts = {
      retryInterval: 5000,
      txt: "Network unavailable"
    };
  }
}

有什么想法吗?

最佳答案

好的,我查看了 sourcecode这就是组件决定使用哪个协议(protocol)的方式:

export var loader = function (ak, offlineOpts, callback, protocol) {
  var realProtocol = protocol || location.protocol;

因此,如果没有传递协议(protocol),它将使用提供页面的协议(protocol)(如果是 ionic,则为 file://,如果是 WKWebview localhost://)。

现在,您在问题中说您尝试传递不同的协议(protocol),但查看您的代码,您将其作为 opts 对象的属性传递。但如果你看一下百度 map 组件,它期望该协议(protocol)作为单独的@Input:

export class BaiduMap implements OnInit, OnChanges {
  @Input() ak: string;
  @Input() protocol: string;
  @Input() options: MapOptions;

因此您需要按如下方式更改组件模板:

<baidu-map ak="{{ak}}"
  [options]="opts" 
  [offline]="offlineOpts" 
  [protocol]="'https'" // not sure about the extra quotation marks
  (onMapLoaded)="loadMap($event)" 
  (onMarkerClicked)="clickMarker($event)" 
  (onClicked)="clickMap($event)">
</baidu-map>

关于angular - ionic 3 : Baidu map working only in browser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45943364/

相关文章:

angular - Ionic 2 - 同时有多个菜单(右 - 左)

javascript - Ionic 构建不会更新 Assets 文件夹的内容 (Ionic 3.5)

Angular2在routerLink中设置当前路由的参数

angular - 简单的 ng-content 在 Angular 2 中不起作用

jquery - Typescript 中的闭包

typescript - 具有模板文字类型的属性路径

Angular 4 - 不要在重新访问时重新加载组件

typescript - 在 TypeScript 中创建墓碑

reactjs - 带有用于自定义 Prop 的样式化组件条件 css 的 typescript

windows - 找不到命令 : ssh OpenSSH not found on your computer