简体   繁体   中英

How to show Google Maps with address in angular2

I could success to show google map using lat and lng. 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. The problem is that if I try to use google.maps.Geocoder, it makes an error that cannot find name '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

It was simple. I solve this that call api url with address parameter

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

It's work for me!

This is a really good tutorial for searching an address and displaying it on google maps:

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

It has a plunkr demo as well at the bottom. Hope this is what you were looking for.

Address to 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'

And... Are there anyone give me some example geocoding with angular2?

to find the name as google in your application you need to declare the var google : any ; at the top component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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