Answer the question
In order to leave comments, you need to log in
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']
});
}
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());
}
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);
}
}
});
[56.458592, 84.947361]
[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]
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
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 questionAsk a Question
731 491 924 answers to any question