简体   繁体   English

如何在 Angular 中刷新谷歌地图?

[英]How To Refresh Google Map In Angular?

I need to refresh google map every time I press the "show" button that changes the lat and lng.每次按下更改纬度和经度的“显示”按钮时,我都需要刷新谷歌地图。

I'm using AGM for the Google Map.我在谷歌地图上使用 AGM。

Here I'm getting the lat and lng I wanna see on the map:在这里,我得到了我想在地图上看到的纬度和经度:

onShow(_token: string) {
        var find = this.model.find(({ token }) => token === _token);

        this.postsService.setLat(find.lat);
        this.postsService.setLng(find.lng);

        console.log(
            this.postsService.getLat() + '    ' + this.postsService.getLng()
        );
    }

On the web page i have some locations.在网页上我有一些位置。 When I press "Show" button I want to see that location I point to on the map.当我按下“显示”按钮时,我想在地图上看到我指向的那个位置。

That's app.component.ts:那是 app.component.ts:

import { Component } from '@angular/core';
import { PostsService } from './posts.service';

@Component({
    selector: 'app-root',
    templateUrl: 'app.component.html',
    styleUrls: ['app.component.css'],
})
export class AppComponent {
    constructor(public postsService: PostsService) {}

    title = 'Location Project';
    lat: number = this.postsService.getLat();
    lng: number = this.postsService.getLng();
    zoom: number = 12;
}

The problem is that the map is created with lat and lng being undefined (and so the map shows a default point in the ocean).问题是地图是在未定义 lat 和 lng 的情况下创建的(因此地图显示了海洋中的默认点)。 How can i refresh the map every time I change the lat and lng values?每次更改 lat 和 lng 值时如何刷新地图?

app.component.html: app.component.html:

<app-header></app-header> <br /><br />

<agm-map [latitude]="lat" [longitude]="lng" [zoom]="zoom">
    <agm-marker [latitude]="lat" [longitude]="lng"></agm-marker>
</agm-map>

<br /><br />

<app-post-list></app-post-list>

posts.service:邮政服务:

import { Location } from './post.model';
import { Injectable } from '@angular/core';
import { Subject, Observable, Subscription, from } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';

@Injectable({ providedIn: 'root' })
export class PostsService {
    model: Location[] = [];

    constructor() {}

    private lat;
    private lng;

    setLat(lat) {
        this.lat = lat;
    }
    setLng(lng) {
        this.lng = lng;
    }
    getLat() {
        return this.lat;
    }
    getLng() {
        return this.lng;
    }
}

Here's a screenshot of some hardcoded lat and lng values, to show how it should appear.这是一些硬编码的 lat 和 lng 值的屏幕截图,以显示它应该如何显示。

Where are the map markers set?地图标记在哪里设置? You must define markers with the new locations and set them on the map.您必须使用新位置定义标记并将它们设置在地图上。 I think you're showing lat and lng variables, but you're not setting them in your onShow method.我认为您正在显示 lat 和 lng 变量,但您没有在 onShow 方法中设置它们。 You only call setLat, setLng method from the service but not updating the lat and lng variables!您只从服务中调用 setLat、setLng 方法,但不更新 lat 和 lng 变量!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM