简体   繁体   English

如何在angular2中显示地址为Google Maps

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

I could success to show google map using lat and lng. 我可以使用lat和lng成功显示google map。 However, I have no idea how to show it using address. 但是,我不知道如何使用地址显示它。 I mean I need to generate lat & lng from address. 我的意思是我需要从地址生成经纬度。

I saw some codes here, mostly using google.maps.Geocoder. 我在这里看到了一些代码,主要是使用google.maps.Geocoder。 The problem is that if I try to use google.maps.Geocoder, it makes an error that cannot find name 'google'. 问题是,如果我尝试使用google.maps.Geocoder,则会发生错误,找不到名称“ google”。

If anyone know how to solve this problem, please let me know. 如果有人知道如何解决此问题,请告诉我。 And if you reply with example code, it could big help! 如果您提供示例代码的回复,那将大有帮助!

Thanks for your help and advice! 感谢您的帮助和建议! :D :D

It was simple. 很简单。 I solve this that call api url with address parameter 我解决了用地址参数调用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'; 

to your component.ts file 到您的component.ts文件

It's work for me! 为我工作!

This is a really good tutorial for searching an address and displaying it on google maps: 这是一个非常好的教程,用于搜索地址并将其显示在Google地图上:

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

It has a plunkr demo as well at the bottom. 它的底部也有一个plunkr演示。 Hope this is what you were looking for. 希望这就是您想要的。

Address to Lat & long 地址到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();

I installed again using npm install @types/googlemaps --save-dev So, I could solve the error that cannot find name 'google' 我再次使用npm install @types/googlemaps --save-dev进行npm install @types/googlemaps --save-dev因此,我可以解决cannot find name 'google'的错误

And... Are there anyone give me some example geocoding with angular2? 还有...有没有人给我提供一些使用angular2进行地理编码的示例?

to find the name as google in your application you need to declare the var google : any ; 要在您的应用中找到谷歌的名字,您需要声明var google : any ; at the top component. 在顶部组件。

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

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