1

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);
});
3
  • We are going to need more of the code. For example we don't see where map is defined and what scope this entire thing is in. Commented Dec 30, 2015 at 19:49
  • 1
    Where are you defining map.mapOptions? Commented Dec 30, 2015 at 19:49
  • added the extra code Commented Dec 30, 2015 at 19:53

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

it works now, and the second alert gives me result. I still don't understand how that happened, also going to fix the code, I copy pasted in a hurry and forgot to clean it up a little bit.
Yeah, and I don't know why
I think it's simply because this is how you set the map center using google maps API
Ok thanks, and thanks to whoever edited my post, i was going to do it myself but ty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.