Angular Googlemaps本机在拖动后获取标记位置

Angular Googlemaps本机在拖动后获取标记位置,angular,google-maps,ionic3,ionic-native,Angular,Google Maps,Ionic3,Ionic Native,在设置“on marker drag end”侦听器给出的新坐标时,我面临一个奇怪的问题。尝试设置变量this.lat和this.lng时,甚至尝试在事件侦听器中控制台注销它们时,我会收到以下错误: ERROR Error {rejection: TypeError, promise: t, zone: r, task: e, stack: (...)…} message: "Uncaught (in promise): TypeError: Cannot set property 'lat'

在设置“on marker drag end”侦听器给出的新坐标时,我面临一个奇怪的问题。尝试设置变量this.lat和this.lng时,甚至尝试在事件侦听器中控制台注销它们时,我会收到以下错误:

ERROR 
Error {rejection: TypeError, promise: t, zone: r, task: e, stack: (...)…}
message: "Uncaught (in promise): TypeError: Cannot set property 'lat' of undefined↵TypeError: Cannot set property 'lat' of undefined↵    at file:///android_asset/www/build/28.js:180:27↵    at t.invoke (file:///android_asset/www/build/polyfills.js:3:14976)↵    at Object.zone._inner.zone._inner.fork.onInvoke (file:///android_asset/www/build/vendor.js:4248:33)↵    at t.invoke (file:///android_asset/www/build/polyfills.js:3:14916)↵    at r.run (file:///android_asset/www/build/polyfills.js:3:10143)↵    at file:///android_asset/www/build/polyfills.js:3:20242↵    at t.invokeTask (file:///android_asset/www/build/polyfills.js:3:15660)↵    at Object.zone._inner.zone._inner.fork.onInvokeTask (file:///android_asset/www/build/vendor.js:4239:33)↵    at t.invokeTask (file:///android_asset/www/build/polyfills.js:3:15581)↵    at r.runTask (file:///android_asset/www/build/polyfills.js:3:10834)"
promise: t
rejection: TypeError
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
task: e
zone: r
__proto__: d
这就好像这两个变量不可及或者我不知道。现在试着调试了3个小时,但没有任何运气。也许我只是没有注意到一些事情,所以任何帮助都会非常感激。所讨论的功能是updatePosition,其他功能都在工作

这是我的密码:

import { Component } from '@angular/core';
import {IonicPage, NavController, NavParams, Platform} from 'ionic-angular';
import {HelperProvider} from "../../providers/helper/helper";
import {
  GoogleMaps,
  GoogleMap,
  GoogleMapsEvent,
  GoogleMapOptions,
  Marker, CameraPosition, ILatLng
} from '@ionic-native/google-maps';
import {Geolocation, GeolocationOptions} from "@ionic-native/geolocation";

@IonicPage()
@Component({
  selector: 'page-statement',
  templateUrl: 'statement.html',
})
export class StatementPage {

  map: GoogleMap;
  lat:any;
  lng:any;
  settlementLat:any;
  settlementLng:any;
  geoLocationOption: GeolocationOptions;
  showMap: boolean = false;

  constructor(
    public navCtrl: NavController,
    public platform: Platform,
    private geoLocation: Geolocation,
    private helper: HelperProvider,
    public navParams: NavParams) {

    const data = JSON.parse(localStorage.getItem('deviceData'));

    if(data.settlementLat && data.settlementLng){
      this.settlementLat = data.settlementLat;
      this.settlementLng = data.settlementLng;
    }

  }

  openMap(){
    this.showMap = true;
    setTimeout(()=> {this.loadMap()},500)
  }

  loadMap() {
    this.map = GoogleMaps.create('statementMap', {
      'camera': {
        'target': {
          "lat": parseFloat(this.settlementLat),
          "lng": parseFloat(this.settlementLng)
        },
        'zoom': 18
      }
    });

    this.map.one(GoogleMapsEvent.MAP_READY).then(() => {

      this.geoLocationOption = {
        maximumAge: 0,
        timeout : 15000,
        enableHighAccuracy: true
      };

      this.helper.presentLoading('Searching for GPS position...');
      this.getPosition();
    });
  }

  getPosition(){
    this.geoLocation.getCurrentPosition(this.geoLocationOption).then((resp) => {
      this.helper.closeLoading();
      this.lat = resp.coords.latitude;
      this.lng = resp.coords.longitude;

      let position: CameraPosition<ILatLng> = {
        target: {
          lat: this.lat,
          lng: this.lng
        },
        zoom: 18
      };

      this.map.moveCamera(position);

      this.map.addMarker({
        icon: 'blue',
        animation: 'DROP',
        draggable: true,
        position: {
          lat: this.lat,
          lng: this.lng
        }
      }).then(this.updatePosition)

    }).catch((error) => {
      this.helper.closeLoading();
      this.map.destroy();
      this.helper.presentToast('Please turn on your gps...');
      this.showMap = false;
    });
  }

  updatePosition(marker: Marker){
    marker.one(GoogleMapsEvent.MARKER_DRAG_END).then(() => {
      this.lat = marker.getPosition().lat;
      this.lng = marker.getPosition().lng;

      console.log(this.lat);
      console.log(this.lng);
    });
  }

}
从'@angular/core'导入{Component};
从“离子角度”导入{IonicPage,NavController,NavParams,Platform};
从“./../providers/helper/helper”导入{HelperProvider}”;
进口{
谷歌地图,
谷歌地图,
谷歌地图服务公司,
谷歌地图选项,
标记器,摄像机位置,ILatLng
}来自“@ionic native/google地图”;
从“@ionic native/Geolocation”导入{Geolocation,GeolocationOptions};
@IonicPage()
@组成部分({
选择器:“页面语句”,
templateUrl:'statement.html',
})
导出类语句页面{
地图:谷歌地图;
lat:任何;
液化天然气:任何;
定居地:任何;
结算:任何;
地理定位选项:地理定位选项;
showMap:boolean=false;
建造师(
公共navCtrl:NavController,
公共平台:平台,
私人地理定位:地理定位,
私人佣工:HelperProvider,
公共navParams:navParams){
const data=JSON.parse(localStorage.getItem('deviceData');
if(data.settlementLat&&data.settlementLng){
this.settlementLat=data.settlementLat;
this.settlementLng=data.settlementLng;
}
}
openMap(){
this.showMap=true;
setTimeout(()=>{this.loadMap()},500)
}
loadMap(){
this.map=GoogleMaps.create('statementMap'{
“照相机”:{
“目标”:{
“lat”:parseFloat(本结算表),
“液化天然气”:parseFloat(本结算)
},
“缩放”:18
}
});
这个.map.one(GoogleMapsEvent.map_READY)。然后(()=>{
this.geoLocationOption={
最大值:0,
超时:15000,
EnableHighAccurance:正确
};
this.helper.presentLoading('搜索GPS位置…');
这个.getPosition();
});
}
getPosition(){
this.geoLocation.getCurrentPosition(this.geoLocationOption)。然后((resp)=>{
this.helper.closeLoading();
this.lat=相应的坐标纬度;
this.lng=相应的坐标经度;
let位置:CameraPosition={
目标:{
拉特:这个,拉特,
液化天然气:这是液化天然气
},
缩放:18
};
此.map.moveCamera(位置);
这个是.map.addMarker({
图标:“蓝色”,
动画:“放下”,
真的,
职位:{
拉特:这个,拉特,
液化天然气:这是液化天然气
}
}).然后(此.updatePosition)
}).catch((错误)=>{
this.helper.closeLoading();
this.map.destroy();
this.helper.presentToast('请打开您的gps…');
this.showMap=false;
});
}
更新位置(标记:标记){
marker.one(GoogleMapsEvent.marker)然后(()=>{
this.lat=marker.getPosition().lat;
this.lng=marker.getPosition().lng;
console.log(this.lat);
控制台日志(this.lng);
});
}
}

尝试避免无效使用此运算符,例如

    getPosition(){
      this.geoLocation.getCurrentPosition(this.geoLocationOption).then((resp) => {
        this.helper.closeLoading();
        var my_lat = resp.coords.latitude;
        var my_lng = resp.coords.longitude;

        let position: CameraPosition<ILatLng> = {
          target: {
            lat: this.lat,
            lng: this.lng
          },
          zoom: 18
        };

        this.map.moveCamera(position);

        this.map.addMarker({
          icon: 'blue',
          animation: 'DROP',
          draggable: true,
          position: {
            lat: my_lat,
            lng: my_lng
          }
        }).then(this.updatePosition)

      }).catch((error) => {
        this.helper.closeLoading();
        this.map.destroy();
        this.helper.presentToast('Please turn on your gps...');
        this.showMap = false;
      });
    }
getPosition(){
this.geoLocation.getCurrentPosition(this.geoLocationOption)。然后((resp)=>{
this.helper.closeLoading();
var my_lat=相应的坐标纬度;
var my_lng=相应的坐标经度;
let位置:CameraPosition={
目标:{
拉特:这个,拉特,
液化天然气:这是液化天然气
},
缩放:18
};
此.map.moveCamera(位置);
这个是.map.addMarker({
图标:“蓝色”,
动画:“放下”,
真的,
职位:{
拉特:我的拉特,
液化天然气:我的液化天然气
}
}).然后(此.updatePosition)
}).catch((错误)=>{
this.helper.closeLoading();
this.map.destroy();
this.helper.presentToast('请打开您的gps…');
this.showMap=false;
});
}

代码建议仅针对lat和lng的解决方案可能是您在其他人身上有其他问题不正确地使用此

仍然无法联系到我的this.lat和this.lng,但我删除了this.updatePosition并使用了一个胖箭头函数来代替它,突然这些变量又可以访问了。所以问题解决了,谢谢你的努力:)