【问题标题】:add button into google maps Angular2将按钮添加到谷歌地图Angular2
【发布时间】:2017-11-03 18:50:29
【问题描述】:

我是 Angular 的新手,并且有一个显示全屏谷歌地图的网络应用程序。 我想在谷歌地图布局中添加一个“注销”按钮(不是作为标题)。

我知道使用 javascript Google Maps Api,可能是这样的:

var Logout = document.getElementById('Logout');
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(Logout);

但是对于AGM - Angular Google Maps,我没有找到方法。

现在我有了:

ma​​p.component.html

<button id="Logout" [(ngModel)]="logoutButton" class="toggle-button controls button" (click)="logout()">Logout</button>

<agm-map [latitude]="lat" [longitude]="lng" [zoom]=12 [mapTypeControl]="true" (boundsChange)="centerChanged($event)" (idle)="loadParkings()" >

    <agm-marker *ngFor="let park of parkings" [latitude]="park.location[0]" [longitude]="park.location[1]"></agm-marker>
</agm-map>

ma​​p.component.ts

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

declare var google: any;

@Component({
  selector: 'app-map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.css']
})

export class MapComponent implements OnInit {
  lat: number;
  lng: number;
  map: any;
  logoutButton: any;

  constructor(
    public mapApi: GoogleMapsAPIWrapper) {}

  ngOnInit(): void {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(position => {
        this.lat = position.coords.latitude;
        this.lng = position.coords.longitude;
      });
    } else {
      this.lat = 47.2275654;
      this.lng = -1.3849729;
    }
  }


  logout() {
    this.authenficationService.logout();
    this.router.navigate(['/login']);
  }
}

【问题讨论】:

    标签: angular google-maps typescript angular2-google-maps


    【解决方案1】:

    几天前我找到了我的解决方案,它看起来像你的答案。 我使用了事件 (mapReady),它提供了原生地图的实例。

    还有我如何设法在地图中显示一个搜索框。

    ma​​p.component.html

    <agm-map [latitude]="lat" [longitude]="lng" [zoom]=12 [mapTypeControl]="true" (boundsChange)="centerChanged($event)" (idle)="loadParkings()"
        (mapReady)="mapReady($event)" (boundsChange)="boundsChanged($event)">
        <button id="Settings" class="toggle-button controls button" [hidden]="hideSettings">
            <mat-icon aria-hidden="true">settings</mat-icon>
        </button>
        <button id="Profile" class="toogle-button controls button" (click)="profileClicked()">
            <mat-icon aria-hidden="true">account_circle</mat-icon>
        </button>
        <button id="Logout" class="toggle-button controls button" (click)="logout()">Logout</button>
        <input id="Map-Search" class="controls" type="text" placeholder="Search Box">
        <agm-marker *ngFor="let park of parkings" [latitude]="park.location[0]" [longitude]="park.location[1]"></agm-marker>
    </agm-map>
    

    ma​​p.component.ts

    declare const google: any;
    
    @Component({
      selector: 'app-map',
      templateUrl: './map.component.html',
      styleUrls: ['./map.component.css'],
    })
    
    export class MapComponent {
        map: any;
        searchBox: any;
    
        [...]
    
        mapReady(event: any) {
            this.map = event;
            const input = document.getElementById('Map-Search');
            this.searchBox = new google.maps.places.SearchBox(input);
            this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(document.getElementById('Settings'));
            this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(document.getElementById('Profile'));
            this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
            this.map.controls[google.maps.ControlPosition.TOP_RIGHT].push(document.getElementById('Logout'));
    
            this.searchBox.addListener('places_changed', () => this.goToSearchedPlace());
        }
    
        goToSearchedPlace() {
            const places = this.searchBox.getPlaces();
            if (places.length === 0) {
            return;
            }
            const bounds = new google.maps.LatLngBounds();
            places.forEach((place) => {
            if (place.geometry.viewport) {
                bounds.union(place.geometry.viewport);
            } else {
                bounds.extend(place.geometry.location);
            }
            });
            this.map.fitBounds(bounds);
        }
    }
    

    【讨论】:

    • 谢谢。奇迹般有效。我想知道是否有办法在动态创建元素时将元素添加到映射中,例如,使用 *ngIf?
    • 如红色代码示例中的here 所述,您可能需要导入“places”库。就像在你的模块中一样:AgmCoreModule.forRoot({ libraries: ['places'] })。这将在窗口对象上添加google.maps.places
    • 我想知道,有什么改变,因为我的 AgmMap 上没有 .controls
    • 好的,所以仅仅删除 AgmMap 的类型仍然可以改变我对 boundsChange 的反应
    • 我试图在我的 agm 地图“设置位置”上添加按钮,但是当我投球或移动地图时它会闪烁.. 有什么帮助吗?
    【解决方案2】:

    几天前我遇到了同样的问题。我不确定这是否是正确的方法。您可以通过 GoogleMapsAPIWrapper 中的 getNativeMap() 获取地图。然后赋值给任意类型的变量,否则找不到控件:

    view-more-button.component.ts

    import { Component, OnInit } from '@angular/core';
    import { GoogleMapsAPIWrapper } from '@agm/core/services/google-maps-api-wrapper';
    declare var google: any;
    
    @Component({
      selector: 'app-view-more-button',
      templateUrl: './view-more-button.component.html',
      styleUrls: ['./view-more-button.component.css']
    })
    export class ViewMoreButtonComponent implements OnInit {
    
      private _isMoreMap: boolean;
    
      constructor(private _wrapper: GoogleMapsAPIWrapper) {
        this._isMoreMap = true;
      }
    
      ngOnInit() {
        this._wrapper.getNativeMap().then((m) => {
          // m type doesnt have controls declared. So need to store it to <any> first then use controls
          var nativeMap: any;
          nativeMap = m;
          nativeMap.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(document.getElementById("view-more-button"));
        });
      }
    
      private _click() {
        ...
      }
    
    }
    

    view-more-button.component.html

    <button class="btn btn-primary" (click)="_click()" id="view-more-button">
      View More Map
    </button>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-19
      • 2019-03-04
      • 1970-01-01
      • 2012-04-19
      • 2012-06-14
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多