S
S
Sergey Zhukov2015-10-15 21:45:33
JavaScript
Sergey Zhukov, 2015-10-15 21:45:33

Strange map behavior: Uncaught TypeError: Cannot read property 'geoObjects' of undefined?

Very strange behavior!
It works with some coordinates, but not with several ;(
Initialize the map:

ymaps.ready(init);
  var myMap;
  function init(){
    myMap = new ymaps.Map("map", {
      center: [56.49540919, 84.95061710],
      zoom: 12,
      controls: ['zoomControl']
    });
  }

We write a function to reload the map (for new coordinates, new coordinates come to responseCoords . This is where the error occurs). Says that there is no myMap (undefined), as it is not, it is declared above ;(:
function reloadYandexMap(responseCoords)
  {
    myMap.geoObjects.removeAll();
    for (var i = 0; i < responseCoords.length; i++) {
      myMap.geoObjects.add(new ymaps.Placemark(responseCoords[i]));
    }
    myMap.setBounds(myMap.geoObjects.getBounds());
  }

We send an AJAX request to get new coordinates, after we get them, we will reload the map:
var responseCoords = [];
  $.ajax({
    type: 'POST',
    url : '/ajax/getCoordsByAddress',
    //async: false,
    data: {
      'arrStreets':  arrStreets
    },
    dataType: 'json',
    success: function(res){
      if(res.success)
      {
        $.each( JSON.parse(res.msg), function( key, val )
        {
          responseCoords.push(val);
        });
        reloadYandexMap(responseCoords);
      }
    }
  });

When something like this comes up:
[56.458592, 84.947361]

And an error occurs (topic title)
But! If it comes like this:
[56.458537, 84.948367]
[56.49848, 84.968858]
[56.459383, 84.958959]
[56.458592, 84.947361]
[56.510298, 85.029584]
[56.461153] [56.461153] [56.461153]
[56.461153]

Then all the marks are put on the map! Everything is great. I draw your attention to the fact that "erroneous" coordinates are present in this array.
Solution :
var myMap;
  function init(){
    myMap = new ymaps.Map("map", {
      center: [56.49540919, 84.95061710],
      zoom: 12,
      controls: ['zoomControl']
    });
  }

  function reloadYandexMap(responseCoords)
  {
    myMap.geoObjects.removeAll();
    for (var i = 0; i < responseCoords.length; i++) {
      myMap.geoObjects.add(new ymaps.Placemark(responseCoords[i]));
    }
    //myMap.setBounds(myMap.geoObjects.getBounds());
  }

  ymaps.ready(function(){
    init();

    var arrStreets = [];
    $('#streets-box span').each(function(ind, val){
      arrStreets.push($(val).text());
    });

    var responseCoords = [];
    $.ajax({
      type: 'POST',
      url : '/ajax/getCoordsByAddress',
      async: false,
      data: {
        'arrStreets':  arrStreets
      },
      dataType: 'json',
      success: function(res){
        if(res.success)
        {
          $.each( JSON.parse(res.msg), function( key, val )
          {
            responseCoords.push(val);
          });
          reloadYandexMap(responseCoords);
        }
      }
    });
  });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Inchin ☢, 2015-10-15
@Adobe

ymaps.ready(init);
var myMap;  //myMap = undefined

function init(){
  //Вот это выполнится только по готовности  ymaps
  myMap = new ymaps.Map("map", {
    center: [56.49540919, 84.95061710],
    zoom: 12,
    controls: ['zoomControl']
  });
  console.log(myMap); //ymaps.Map.......
}

//А то, что тут - сразу
console.log(myMap); //undefined

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question