首页 > 解决方案 > 如何在 google.maps.events.addListener(marker, 'dragend', function() { }) 中使用 storage.set(); 在离子 3

问题描述

google.maps.event.addListener(Marker, 'click', (function(Marker) {
  return function() {
    this.storage.set('mylocation', this.Marker.getPosition()); 
  }
})(Marker));

polyfills.js:3 未捕获的类型错误:无法读取未定义的属性“集”

当我尝试使用来自谷歌地图标记拖动事件的响应在 Ionic 3 中设置存储时出现此错误。

标签: typescripteventsionic-frameworkmapsstorage

解决方案


无法读取未定义的属性“集”

这意味着“存储”是未定义的。如果存储未定义,请务必在构造函数中声明并正确导入,如下所示:Ionic Storage Docs

import { Storage } from '@ionic/storage';

export class MyApp {
  constructor(private storage: Storage) { }

  ...

  // set a key/value
  this.storage.set('name', 'Max');

  // Or to get a key/value pair
  this.storage.get('age').then((val) => {
    console.log('Your age is', val);
  });
}

如果错误因为this.Marker.getPosition()为空而被抛出,您可以尝试 Ahkil J 的解决方案:

google.maps.event.addListener(Marker, 'click', (function(Marker) {
  return function() {
    this.LastLat = Marker.position.lat();
    this.LastLng = Marker.position.lng();

    this.storage.set('mylocation', this.LastLat + this.LastLng);    
  }
})(Marker));

推荐阅读