gpt4 book ai didi

angular - setCenter 在 angular2-google-maps 中不起作用

转载 作者:太空狗 更新时间:2023-10-29 17:00:23 25 4
gpt4 key购买 nike

import { GoogleMapsAPIWrapper } from '@agm/core';
import { Component, Input } from '@angular/core';

@Component({
selector: 'core-map',
styleUrls: [ './map.component.scss' ],
templateUrl: './map.component.html',
})
export class MapComponent {
constructor(
public gMaps: GoogleMapsAPIWrapper
) {}

public markerClicked = (markerObj) => {
this.gMaps.setCenter({ lat: markerObj.latitude, lng: markerObj.longitude });
console.log('clicked', markerObj, { lat: markerObj.latitude, lng: markerObj.longitude });
}
}
console output: Object {lat: 42.31277, lng: -91.24892}

也尝试过 panTo,结果相同。

最佳答案

终于搞定了。必须创建 agm-map 的子组件并创建加载时的输出,获取 native google maps api 包装器并传递到我的父 map 组件。我希望他们做到了,这样您就可以在常规 agm-map 组件中获取 gmaps api 包装器。也适用于 panTo。

父组件标记

<agm-map [latitude]='lat' [longitude]='lng'
[usePanning]='true'>
<agm-marker *ngFor='let location of locations'
[latitude]='location.latitude'
[longitude]='location.longitude'
[iconUrl]='location.icon'
(markerClick)='markerClicked(location)'></agm-marker>
<core-map-content (onMapLoad)='loadAPIWrapper($event)'></core-map-content>
</agm-map>

父组件

/**
* Map Component
* API Docs: https://angular-maps.com/docs/api/latest/ts/
*/
import { GoogleMapsAPIWrapper } from '@agm/core';
import { Component, Input } from '@angular/core';

declare var google:any;

@Component({
selector: 'core-map',
styleUrls: [ './map.component.scss' ],
templateUrl: './map.component.html',
})
export class MapComponent {
@Input() lat: number;
@Input() lng: number;
@Input() locations: {};
map: any;

constructor(
public gMaps: GoogleMapsAPIWrapper,
) {}

public loadAPIWrapper(map) {
this.map = map;
}

public markerClicked = (markerObj) => {
const position = new google.maps.LatLng(markerObj.latitude, markerObj.longitude);
this.map.panTo(position);
}
}

子组件

import { Component, EventEmitter, OnInit, Output } from '@angular/core';

import { GoogleMapsAPIWrapper } from '@agm/core';

@Component({
selector: 'core-map-content',
template: '',
})
export class MapContentComponent implements OnInit {
@Output() onMapLoad: EventEmitter<{}> = new EventEmitter<{}>();

constructor(public gMaps: GoogleMapsAPIWrapper) {}

ngOnInit() {
this.gMaps.getNativeMap().then((map) => {
this.onMapLoad.emit(map);
});
}
}

关于angular - setCenter 在 angular2-google-maps 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44315771/

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