Marco Filippozzi
Marco Filippozzi

Reputation: 191

Variable goes undefined after assignement AngularJS

I'm using $http.get method to get objects from a json file. It puts them in the data variable as an array of objects. The first time I try to use the variables contained in the objects to initialize a marker it works, but then the variables disappear, and the console gives me "TypeError: Cannot set property 'center' of undefined" on the line where I try to overwrite the .center value, and even the alert gives nothing.

var mapOptions =
{
    center: new google.maps.LatLng(0, 0),
    zoom: 16,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("map"), mapOptions);

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(0, 0),
    map: map,
    title: 'Uluru (Ayers Rock)'
});

$http.get("buses/buses.json").success(function(data){
    console.log(angular.isArray(data));
    marker.position = new google.maps.LatLng(data[1].lat, data[1].lon);
    alert(data[1].lon);
    map.mapOptions.center = marker.position;
    alert(data[1].lon);
});

Upvotes: 1

Views: 36

Answers (1)

Alon Eitan
Alon Eitan

Reputation: 12025

Shouldn't if be map.setCenter(marker.position);?

Upvotes: 1

Related Questions