gpt4 book ai didi

angular - 如何通过 Ionic 3 使用 Google map

转载 作者:搜寻专家 更新时间:2023-10-30 21:19:51 25 4
gpt4 key购买 nike

未捕获( promise ):

TypeError: Cannot read property 'firstChild' of null TypeError: Cannot read property 'firstChild' of null at Object._.og (maps.googleapis.com/maps/api/js?key=&callback=initMap:88:391) at new tg (maps.googleapis.com/maps/api/js?key=&callback=initMap:90:76) at ParkMapPage.initializeMap

当我尝试 ionic serve --lab 时出现以上错误

import { Component } from '@angular/core';
import { Platform, NavController } from 'ionic-angular';


@Component({
selector: 'page-park-map',
templateUrl: 'park-map.html'
})

export class ParkMapPage {
map: google.maps.Map;

constructor(public navCtrl: NavController, public platform: Platform) {
this.map = null;
this.platform.ready().then(() => {
this.initializeMap();
});
}

initializeMap() {
let minZoomLevel = 3;

this.map = new google.maps.Map(document.getElementById("map_canvas"), {
zoom: minZoomLevel,
center: {lat: 39.833, lng: -98.583},
mapTypeControl: false,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
}

}

上面是我在 Typescript 文件中的代码。

<ion-content>
<div id="map_canvas"></div>
</ion-content>

以上是我在 HTML 文件中的代码。

这是书中的练习。我无法解决它,任何人都可以帮助我):

最佳答案

强烈建议您使用Ionic Native wrapper for google maps而不是单独行动。

1。安装 Cordova 和 Ionic Native 插件:

$ ionic cordova plugin add cordova-plugin-googlemaps --variable API_KEY_FOR_ANDROID="YOUR_ANDROID_API_KEY_IS_HERE" --variable API_KEY_FOR_IOS="YOUR_IOS_API_KEY_IS_HERE"
$ npm install --save @ionic-native/google-maps

2。安装插件包后,将其添加到应用的 NgModule 中。

...

import { Camera } from '@ionic-native/camera';

...

@NgModule({
...

providers: [
...
GoogleMaps
...
]
...
})
export class AppModule { }

3。用法

import {
GoogleMaps,
GoogleMap,
GoogleMapsEvent,
LatLng,
CameraPosition,
MarkerOptions,
Marker
} from '@ionic-native/google-maps';

export class MapPage {
constructor(private googleMaps: GoogleMaps) {}

// Load map only after view is initialized
ngAfterViewInit() {
this.loadMap();
}

loadMap() {
// make sure to create following structure in your view.html file
// and add a height (for example 100%) to it, else the map won't be visible
// <ion-content>
// <div #map id="map" style="height:100%;"></div>
// </ion-content>

// create a new map by passing HTMLElement
let element: HTMLElement = document.getElementById('map');

let map: GoogleMap = this.googleMaps.create(element);

// listen to MAP_READY event
// You must wait for this event to fire before adding something to the map or modifying it in anyway
map.one(GoogleMapsEvent.MAP_READY).then(
() => {
console.log('Map is ready!');
// Now you can add elements to the map like the marker
}
);

// create LatLng object
let ionic: LatLng = new LatLng(43.0741904,-89.3809802);

// create CameraPosition
let position: CameraPosition = {
target: ionic,
zoom: 18,
tilt: 30
};

// move the map's camera to position
map.moveCamera(position);

// create new marker
let markerOptions: MarkerOptions = {
position: ionic,
title: 'Ionic'
};

const marker: Marker = map.addMarker(markerOptions)
.then((marker: Marker) => {
marker.showInfoWindow();
});
}

}

关于angular - 如何通过 Ionic 3 使用 Google map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44598498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com