简体   繁体   English

如何使用带有角度的谷歌地图 2+

[英]How to use google map with angular 2+

I am new to angular and working on an old project with angular 2+.我是 angular 的新手,正在使用 angular 2+ 处理一个旧项目。

As I am new I am having a hard time to find out how the map is working, if anyone please let me know that would be a great help.由于我是新手,我很难弄清楚地图是如何工作的,如果有人请告诉我这将是一个很大的帮助。

          <input  places-auto-complete (place_changed)="placeChange($event)" 
          [types]="['all']"  class="search" 
          [(ngModel)]="place" placeholder="Search Place"/>

I am wondering how places-auto-complete is working i can't find out any class or module that is coming from and i would like to change its parameters like:我想知道 Places places-auto-complete是如何工作的,我找不到来自的任何类或模块,我想更改其参数,例如:

[types]="['all']" -> [types]="['address']"

I tried changing it but it's not working, so I would like to know its whole workflow.我尝试更改它但它不起作用,所以我想知道它的整个工作流程。

Any help will be appreciated.任何帮助将不胜感激。

Thanks谢谢

Instead of relying on 3rd party libraries/modules to implement the map for you, you can load the script directly so that you can use the google map's official documentations .无需依赖第三方库/模块为您实现地图,您可以直接加载脚本,以便您可以使用谷歌地图的官方文档 To do this, simply load the JS map script in the index.html.为此,只需在 index.html 中加载 JS 地图脚本。

Here is a working sample for your reference: https://stackblitz.com/edit/angular-map-with-marker-64255628这是供您参考的工作示例: https : //stackblitz.com/edit/angular-map-with-marker-64255628

index.html索引.html

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
<my-app>loading</my-app>

style.css样式文件

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

.map {
  height:100%;
}

map-with-marker.component.html map-with-marker.component.html

<div #map class="map"></div>

map-with-marker.component.ts map-with-marker.component.ts

import { Component, OnInit, ViewChild } from "@angular/core";
declare const google: any;

@Component({
  selector: "my-maps",
  templateUrl: "./map-with-marker.component.html",
  styleUrls: ["./map-with-marker.component.css"]
})
export class MapComponent implements OnInit {
  @ViewChild("map", { static: true }) mapElement: any;
  map: any;

  constructor() {}

  ngOnInit() {
    var center = { lat: -33.8569, lng: 151.2152 };
    const mapProperties = {
      center: center,
      zoom: 11,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    this.map = new google.maps.Map(this.mapElement.nativeElement,mapProperties);
    var marker = new google.maps.Marker({ position: center, map: this.map });

    //adding markers by click
    this.map.addListener('click', (e) =>{
        //Determine the location where the user has clicked.
        this.addMarker(e.latLng);
    });
  }
  addMarker(latLng) {
    var marker = new google.maps.Marker({ position: latLng, map: this.map });
  }
}

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

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