In my project I want to move the center of the map to new coordinates. This is the code I have for the map
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(0, 0),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
}
function moveToLocation(lat, lng){
var center = new google.maps.LatLng(lat, lng);
var map = document.getElementById("map_canvas");
map.panTo(center);
}
The moveToLocation function does get called but the map does not re center. Any idea what I'm missing?
Display the Google Maps API using dynamically, fetch the data in database to display the place, lat, long and to show map marker in center using AngularJS. Done by Sureshchan...
Your problem is that in
moveToLocation
, you're usingdocument.getElementById
to try to get theMap
object, but that only gets you anHTMLDivElement
, not thegoogle.maps.Map
element you're expecting. So your variablemap
has nopanTo
function, which is why it doesn't work. What you need to is squirrel themap
variable away somewhere, and it should work as planned. You can just use a global variable like so:See working jsFiddle here: http://jsfiddle.net/fqt7L/1/