我正在尝试使用AngularJs和Google Maps API在地图上显示标记。但是,由于某种原因,我无法显示标记。我不确定我是否错误地从数据库中提取数据,但是如果有人可以帮我解决这个问题会很棒!
.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
}
})
数据如何存储在数据库中
{
"id": 1,
"name": "Ballentine Hall",
"lat": 39.165935,
"long": -86.521287
}
答案 0 :(得分:1)
嗯,我得到了它的工作。很确定这不是正确/最有效的方法,但至少它的工作。
所以,我发现createMarker函数无法读取位置变量中存储的数据。为了解决这个问题,我只是将地图的所有代码放在从数据库获取位置数据的函数中。这解决了获取数据的问题。
第二个问题是它没有从这些位置获取正确的数据。因此,在getList
函数中,我将代码更改为:$scope.locations = response.data.data;
然后一切都开始工作了。
.controller('MapCtrl', ['$scope', '$state', '$cordovaGeolocation', '$ionicSideMenuDelegate', '$ionicModal', 'dataService', '$http', 'Backand', function($scope, $state, $cordovaGeolocation, $ionicSideMenuDelegate, $ionicModal, dataService, $http, Backand) {
$scope.locations = [];
dataService.getList('locations').then(function(response) {
$scope.locations = response.data.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');
}
});
答案 1 :(得分:0)
您是否在调用createMarker函数时检查是否将数据输入$ scope.locations。 使用console.log查看
for (i = 0; i < $scope.locations.length; i++){
console.log($scope.locations);
createMarker($scope.locations[i]);
}
由于javascript异步性质,它将转到下一行并执行程序,然后一旦你的promise解析它将绑定数据。