在订阅角度2中使用gecoder

时间:2018-02-21 06:34:22

标签: angular google-maps google-geocoder

嗨我想保留从json数据到地址名称

的经度和经度的地理编码

我的组件ts

     getAllMap() {
    this.mapLocationService.getAll()
      .subscribe(
        ml => {
          this.maplocations = ml;
          let latlng = new google.maps.LatLng(this.maplocations['map_location_latitude'], this.maplocations['map_location_longitude']); // <- problem when i call init google map
          console.log(latlng);
          let request = latlng;
          let geocoder = new google.maps.Geocoder();
          geocoder.geocode(request, (results, status) => {
            if (status === google.maps.GeocoderStatus.OK) {
              console.log('status' + status);
              if (results[0] != null) {
                this.city = results[0].address_components[results[0].address_components.length - 4].short_name;
                console.log('city' + this.city);
              } else {
                this.city = 'No address available';
              }
            }
          });
        },
        error => {
          this.errorMessage = error;
        }
      );
  }

已包含在导入和安装

import { } from '@types/googlemaps';
declare var google: any;

所以问题总是显示错误

  

ERROR ReferenceError:未定义谷歌

1 个答案:

答案 0 :(得分:0)

尝试使用

import { } from 'googlemaps';

而不是

import { } from '@types/googlemaps';
declare var google: any;