在typecriptjavascript中使用全局变量在google.maps.Geocoder().geocode()上。

问题描述 投票:0回答:1

我想在google.maps.Geocoder().geocode()里面访问和修改我的全局变量,看起来我不能在里面修改它以返回它的函数全局变量。

  public address: any;

这里我的功能

getCoordsCityName(latlng): any {

new google.maps.Geocoder().geocode({ 'latLng': latlng }, function (results, status) {

  if (status == google.maps.GeocoderStatus.OK) {

    if (results[1]) {
      let country = null, countryCode = null, city = null, cityAlt = null, administrativArea = null;
      let c, lc, component;

      for (let r = 0, rl = results.length; r < rl; r += 1) {
        let result = results[r];
        if (!city && result.types[0] === 'locality') {
          for (c = 0, lc = result.address_components.length; c < lc; c += 1) {
            component = result.address_components[c];
            if (component.types[0] === 'locality') {
              city = component.long_name;
              break;
            }
          }
        } else if (!city && !cityAlt && result.types[0] === 'administrative_area_level_1') {
          for (c = 0, lc = result.address_components.length; c < lc; c += 1) {
            component = result.address_components[c];
            if (component.types[0] === 'administrative_area_level_1') {
              cityAlt = component.long_name;
              break;
            }
          }
        } else if (!country && result.types[0] === 'country') {
          country = result.address_components[0].long_name;
          administrativArea = result.address_components[0].administrative_area_level_1;
          countryCode = result.address_components[0].short_name;
        }
        if (city && country) {
          break;
        }
      }

      this.address = 'Location: ' + country + ', ' + city + ', ' + countryCode;
      console.log('ADDRESS Details: ' + this.address);

    }
  }
});
return this.address;

}

它已经在console.log上显示了位置,但我不能在页面上使用它。

javascript typescript google-maps ionic-framework geolocation
1个回答
0
投票

查看 箭头功能

new google.maps.Geocoder().geocode({ 'latLng': latlng }, function (results, status) {})

改为

new google.maps.Geocoder().geocode({ 'latLng': latlng }, (results, status) => {})
© www.soinside.com 2019 - 2024. All rights reserved.