gpt4 book ai didi

javascript - map : Expected mapDiv of type Element but was passed undefined - google maps

转载 作者:行者123 更新时间:2023-12-05 00:25:49 24 4
gpt4 key购买 nike

我在 div 中有一张 map ,其引用为 #mapa当我想在 map 中追踪路线时, map 会刷新。我不想刷新 map 我有以下代码:

<div style="height: 500px; width: auto;" #mapa>
<google-map height="500px" width="100%" [zoom]="zoom" [center]="center" [options]="options" (mapClick)="click($event)">
<map-marker #markerElem *ngFor="let marker of markers" [position]="marker.position" [label]="marker.label" [title]="marker.title" [options]="marker.options" (mapClick)="openInfo(markerElem, marker.info)" (mapDragend)="moveMap($event)">
</map-marker>
<map-info-window>{{ infoContent }}</map-info-window>
</google-map>
</div>

如果我删除带有引用 #mapa 的 div我把它作为引用放入 <google-map>标记我得到标题错误并显示没有路线的 map 。
trazarRutaMapa() {
const directionsService = new google.maps.DirectionsService;

const directionsDisplay = new google.maps.DirectionsRenderer;
const map = new google.maps.Map(this.mapa.nativeElement, {
zoom: 7,
center: {
lat: this.markers[0].position.lat,
lng: this.markers[0].position.lng
}
});

directionsDisplay.setMap(map);
directionsDisplay.setOptions({
suppressMarkers: false,
draggable: true,
markerOptions: {
icon: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'
}
});

directionsService.route({
origin: {
lat: this.markers[0].position.lat,
lng: this.markers[0].position.lng
},
destination: {
lat: this.markers[1].position.lat,
lng: this.markers[1].position.lng
},
travelMode: google.maps.TravelMode.DRIVING,

}, (response, status) => {
if (status === google.maps.DirectionsStatus.OK) {
console.log('ESTATUS OKEY');

directionsDisplay.setDirections(response);
} else {
window.alert("Fallo el estatus" + status);
}
});
}

最佳答案

据我所知,您只需使用 @angular/google-maps 即可实现您的目标。官方模块。
你的 HTML 会变成这样

<google-map [options]="options">
<map-marker *ngFor="let some of somearray" [icon]="...", [label]="...", [position]="..."></map-marker>
<map-directions-renderer [directions]="..." [options]="..."></map-directions-renderer>
</google-map>
并且您的 traazarRoutaMapa() 方法将变成这样:
trazarRoutaMapa(): void {
const directionsService = new google.maps.DirectionsService; // better if injected from constructor
const request: google.maps.DirectionsRequest = {
destination: {
lat: this.markers[0].position.lat,
lng: this.markers[0].position.lng
},
origin: {
lat: this.markers[1].position.lat,
lng: this.markers[1].position.lng
},
travelMode: google.maps.TravelMode.DRIVING
};
return directionsService.route(
request,
(response, status) => {
if (status === google.maps.DirectionsStatus.OK) {
console.log('ESTATUS OKEY');
directionsDisplay.setDirections(response);
} else {
window.alert("Fallo el estatus" + status);
}
});
}
免责声明:我没有测试代码,只是在记事本复制/粘贴上创建的 :)

关于javascript - map : Expected mapDiv of type Element but was passed undefined - google maps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61784446/

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