javascript - "Cannot read property 'fitBounds' of undefined" when plotting markers on Google Maps with AngularJS -
i'm trying have map zoom or expand fit markers when updates plotted off screen , can't see them. code follows...
$scope.loadmap = function () { var chosendestination = new google.maps.latlng($scope.destination.latitude, $scope.destination.longitude); var mapoptions = { zoom: 12, center: chosendestination, scrollwheel: false, maptypeid: google.maps.maptypeid.terrain } $scope.map = new google.maps.map(document.getelementbyid('map'), mapoptions); var marker = new markerwithlabel({ position: chosendestination, map: $scope.map, animation: google.maps.animation.drop, title: $scope.destination.name, labelcontent: " ", labelanchor: new google.maps.point(7, 35), labelclass: "searchlocation" + $scope.destination.name }); } $scope.updatemapmarkers = function (hotel) { var infowindow = new google.maps.infowindow(); var bounds = new google.maps.latlngbounds(); var createmarker = function (hotel) { var index = $scope.hotels.indexof(hotel) + 1; var latlng = new google.maps.latlng(hotel.location.latitude, hotel.location.longitude); var marker = new markerwithlabel({ map: $scope.map, position: latlng, title: hotel.title, labelcontent: index, labelanchor: new google.maps.point(7, 35), labelclass: "maplabels", labelinbackground: false }); bounds.extend(latlng); marker.content = '<div style=\'width:250px\'>' + hotel.address + '</div>'; google.maps.event.addlistener(marker, 'mouseover', function () { infowindow.setcontent('content goes here'); infowindow.open($scope.map, marker); $scope.markerid = $scope.markers.indexof(marker); }); google.maps.event.addlistener(marker, 'mouseout', function () { infowindow.close($scope.map, marker); }); $scope.markers.push(marker); $scope.map.fitbounds(bounds); } (i = $scope.plottingstartpoint; < $scope.hotels.length; i++) { createmarker($scope.hotels[i]) } } $scope.loadmap(); $scope.updatemapmarkers();
i getting error cannot read property 'fitbounds' of undefined. when debug $scope.map @ point undefined although works when plotting markers, fails when comes 'fitbounds'.
if remove code sets bounds works fine.
anyone know may have done wrong here? still learning angularjs appreciated.
perhaps new (different) scope being created in updatemapmarkers()
, 'map' property being called on scope , not on original scope. make sure referencing same scope both function call. try providing proper model maybe(?):
$scope.model = { map: undefined };
Comments
Post a Comment