簡體   English   中英

如何在angular2中顯示地址為Google Maps

[英]How to show Google Maps with address in angular2

我可以使用lat和lng成功顯示google map。 但是,我不知道如何使用地址顯示它。 我的意思是我需要從地址生成經緯度。

我在這里看到了一些代碼,主要是使用google.maps.Geocoder。 問題是,如果我嘗試使用google.maps.Geocoder,則會發生錯誤,找不到名稱“ google”。

如果有人知道如何解決此問題,請告訴我。 如果您提供示例代碼的回復,那將大有幫助!

感謝您的幫助和建議! :D

很簡單。 我解決了用地址參數調用api url的問題

constructor(private http: Http) { }
  getlatlng(address){
    return this.http.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + address)
      .catch(handleError);
  }
npm install --save @types/googlemaps;

import {} from '@types/googlemaps'; 

到您的component.ts文件

為我工作!

這是一個非常好的教程,用於搜索地址並將其顯示在Google地圖上:

http://brianflove.com/2016/10/18/angular-2-google-maps-places-autocomplete/

它的底部也有一個plunkr演示。 希望這就是您想要的。

地址到Lat&Long

let place: google.maps.places.PlaceResult = autocomplete.getPlace();

//verify result
if (place.geometry === undefined || place.geometry === null) {
    return
}

//set latitude, longitude
this.latitude = place.geometry.location.lat();
this.longitude = place.geometry.location.lng();

我再次使用npm install @types/googlemaps --save-dev進行npm install @types/googlemaps --save-dev因此,我可以解決cannot find name 'google'的錯誤

還有...有沒有人給我提供一些使用angular2進行地理編碼的示例?

要在您的應用中找到谷歌的名字,您需要聲明var google : any ; 在頂部組件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM