I'm trying to display markers on a map using AngularJs and the Google Maps API. But, for some reason, I can't get the markers to display. I'm not sure if I'm pulling the data from the database wrong but if someone could help me out that would be great!
.controller('MapCtrl', ['$scope', '$state', '$cordovaGeolocation', '$ionicSideMenuDelegate', '$ionicModal', 'LocationService', function($scope, $state, $cordovaGeolocation, $ionicSideMenuDelegate, $ionicModal, LocationService) {
$scope.locations = [];
LocationService.getLocations().then(function (result) {
$scope.locations = result.data;
});
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(40.0000, -98.0000),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
$scope.map = new google.maps.Map(document.getElementById('map'), mapOptions);
$scope.markers = [];
var infoWindow = new google.maps.InfoWindow();
var createMarker = function (info){
var marker = new google.maps.Marker({
map: $scope.map,
position: new google.maps.LatLng(info.lat, info.long),
title: info.name
});
marker.content = '<div class="infoWindowContent">' + info.name + '</div>';
google.maps.event.addListener(marker, 'click', function(){
infoWindow.setContent('<h2>' + marker.title + '</h2>' + marker.content);
infoWindow.open($scope.map, marker);
});
$scope.markers.push(marker);
}
for (i = 0; i < $scope.locations.length; i++){
createMarker($scope.locations[i]);
}
$scope.openInfoWindow = function(e, selectedMarker){
e.preventDefault();
google.maps.event.trigger(selectedMarker, 'click');
}
}])
.service('LocationService', function ($http, Backand) {
var baseUrl = '/1/objects/';
var objectName = 'locations/';
function getUrl() {
return Backand.getApiUrl() + baseUrl + objectName;
}
function getUrlForId(id) {
return getUrl() + id;
}
getLocations = function () {
return $http.get(getUrl());
};
return {
getLocations: getLocations
}
})
how the data is stored in the database
{
"id": 1,
"name": "Ballentine Hall",
"lat": 39.165935,
"long": -86.521287
}
console.log(info)in your createMarker function, what does an example for one iteration look like?console.log(info)in the createMarker function but nothing came up. But, when I putconsole.log($scope.locations)outside the function and got an empty list