【问题标题】:Cannot read property 'panTo' of undefined' Angular error无法读取未定义的 Angular 错误的属性“panTo”
【发布时间】:2018-06-04 08:48:19
【问题描述】:

我想在 AGM 地图上打开一个下拉菜单平移到某个城市。我通过在地图组件中创建此方法,然后从视图中调用它来做到这一点:

public move = () => {
  alert("Test");
  const position = new google.maps.LatLng(  52.379189,  4.899431);
  this.map.panTo(position);
}

但是,我收到以下错误消息:

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

谁能指出哪里出了问题?

import {Component, OnInit} from "@angular/core";
import { GoogleMapsAPIWrapper } from '@agm/core';
import { MapsAPILoader } from '@agm/core';
import { Observable, Observer } from 'rxjs';
import {GMapsService} from "./map.service";
import * as data from './inkomen.json';
import * as inbraakdata from '../../assets/output.json';
import * as data_latlon from './inkomen_latlon.json';
import * as inbraak_latlon from './output.json';
import {Http} from "@angular/http";
import 'rxjs/add/operator/map';
import {last} from "@angular/router/src/utils/collection";
import {DropdownModule} from "ngx-dropdown";
import {any} from "codelyzer/util/function";

@Component({
  selector : 'googlemap',
  templateUrl: 'maps.html',
  styleUrls: ['map.component.css']
})
export class GoogleMapsComponent implements OnInit{

arr = [];
arr2 = [];

jsonLatlon
map: any;
jsonData
result
inkomen
count = 0;
results: any;
pages =['abc','bca','pqr'];

constructor(private http: Http, private mapservice: GMapsService) {
  this.jsonData = data;
  this.jsonLatlon = data_latlon
  this.results = inbraak_latlon;
  this.result = inbraak_latlon;
}

ngOnInit(){
console.log(this.jsonData)
console.log(this.jsonLatlon.data)
console.log(this.results)
this.convert()


}

convert(){
  for(var i = 0; i < this.jsonData.length; i++){
  var spl = this.jsonData[i].wijk.split(" ");
  // this.getLatLon(this.jsonData[i].wijk, this.jsonData[i].stad, 
  this.jsonData[i].inkomen)
}
//this.showArr()
}





title: string = 'My first AGM project';
lat: number = 52.1941679;
lng: number = 4.6820146;

public move= () => {
  alert("test");
  const position = new google.maps.LatLng(52.379189,  4.899431);
  this.map.panTo(position);
}
}

【问题讨论】:

  • 你能添加更多代码吗?你在哪里调用move方法?
  • 发布map变量初始化
  • 你没有给data赋值,所以是undefined
  • 完成@SachilaRanawaka
  • 还有@yurzui ....

标签: angular properties undefined panning


【解决方案1】:

您的地图应该被初始化为具有方法 .map,因为您已经在使用 @agm/core,您可以使用 GoogleMapsAPIWrapper

导入

import { AgmCoreModule, GoogleMapsAPIWrapper, AgmInfoWindow, AgmDataLayer, CircleManager, AgmCircle } from '@agm/core';

并参考

 @ViewChild(GoogleMapsAPIWrapper) private gmapWrapper: GoogleMapsAPIWrapper;

你可以做到

this.gmapWrapper.panTo(position);

【讨论】:

  • 感谢您的回复,但还是同样的错误信息
  • 是的,这样称呼它:
  • 我认为 onOpen 在地图被初始化、调试和查看之前被调用
  • 我将调用的标记移到 agm 映射下方,现在我收到此错误消息:“LatLng”类型的参数不可分配给“LatLng”类型的参数 | LatLngLiteral
猜你喜欢
相关资源
最近更新 更多
热门标签