angularpropertiesundefinedpanning

Cannot read property 'panTo' of undefined' Angular error


I want as I open a dropdown menu pan to a certain city on the AGM maps. I do this by making this method in the map component, and then calling it from the View:

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

However, I get this error message:

Cannot read property 'panTo' of undefined'

Can someone point out what is going wrong?

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);
}
}

Solution

  • Your map should be initialized to have the method .map, since you are already using @agm/core you cane make use of GoogleMapsAPIWrapper

    import

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

    and refer

     @ViewChild(GoogleMapsAPIWrapper) private gmapWrapper: GoogleMapsAPIWrapper;
    

    and you can do

    this.gmapWrapper.panTo(position);