简体   繁体   English

如何在GeocoderService中存储来自geocoder.geocode()的地址并将其返回给app.component?

[英]How to store address from geocoder.geocode() in GeocoderService and return it to app.component?

I'm really new to angular and TypeScript, I am working on application which uses google maps to find address and I can't store address from geocoder.geocode(). 我对angular和TypeScript真的很陌生,我正在开发使用google地图查找地址的应用程序,而我无法从geocoder.geocode()存储地址。

I tried solution from here: How do I return a variable from Google Maps JavaScript geocoder callback? 我从这里尝试了解决方案: 如何从Google Maps JavaScript geocoder回调返回变量?

and it is ok, the alert() is working but what I want to do is to pass address from GeocoderService to a parent component and store it there. 没关系,alert()可以正常工作,但是我想要做的是将地址从GeocoderService传递到父组件并将其存储在其中。 I tried to store it in service itself but I can't! 我试图将其存储在服务本身中,但是我不能!

GeocoderService: GeocoderService:

initialize() {
    this.geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(40.730885,-73.997383);
    this.codeLatLng(function(addr){
      this.loc = addr; // <= here i can't store address it is undefined
      alert(addr);
      return this.loc;
    });

  }

  codeLatLng(callback) {
    var latlng = new google.maps.LatLng(40.730885,-73.997383);
    if (this.geocoder) {
      this.geocoder.geocode({'location': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (results[1]) {
            callback(results[1].formatted_address);
          } else {
            alert("No results found");
          }
        } else {
          alert("Geocoder failed due to: " + status);
        }
      });
    }
  }

app.component: app.component:

 constructor( private geolocationService: GeolocationService,
    private geocoderService: GeocoderService){
    this.position = {latitude: 0, longitude: 0}
    this.LatLng = new google.maps.LatLng( this.position.latitude, this.position.longitude);
  }

findMe(){
    this.geolocationService.getPosition().then((position: any) => {
      this.position = {latitude:position.coords.latitude,longitude: position.coords.longitude}
      this.LatLng = new google.maps.LatLng( this.position.latitude, this.position.longitude);
      this.map.setCenter(this.LatLng);
      this.loc = this.geocoderService.initialize(); //<= here i need this address to be returned and stored
      console.log(this.loc) // <= undefined
    })
 }

app.component.html: app.component.html:

<div #map style= "width:100%;height:400px"></div>
<button (click) = "findMe()">Find me</button>

Please help and sorry if question is dumb 如果问题愚蠢,请帮忙,抱歉

1) 1)

   function(addr){
          this.loc = addr; // <= here i can't store address it is undefined
          alert(addr);
          return this.loc;
        }

this function is a callback function so you cannot return data from a callback function The reason why you have undefined loc variable, is because "This" of app component class has been shadowed by the "This" of the function so to resolve this issue you have to use the arrow function (but it does not resolve your problem) 此函数是一个回调函数,因此您无法从回调函数返回数据之所以拥有未定义的loc变量,是因为该应用程序组件类的“ This”已被该函数的“ This”遮盖了,因此您可以解决此问题必须使用箭头功能(但不能解决您的问题)

this.codeLatLng(addr=> this.loc = addr);

2) 2)

  this.loc = this.geocoderService.initialize(); 

you will never get a value because initialize does not return a value 您将永远不会获得值,因为初始化不会返回值

3) So what you have to do 3)所以你要做

Since the result of codeLatLng is asynchronous try to use Subject Or Observable object and in the app component subsribe to this object 由于codeLatLng的结果是异步的,请尝试使用Subject或Observable对象,并在应用程序组件中将此对象替换为

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

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