我正在尝试在谷歌地图中添加一个标记但不幸的是,它没有被添加。
地图中只有一个标记。
这是我的HTML:
<div ng-app="myApp" ng-controller="gMap">
<ui-gmap-google-map
center='map.center'
zoom='map.zoom' aria-label="Google map">
<ui-gmap-marker ng-repeat="marker in markers"
coords="marker.coords" options="marker.options" events="marker.events" idkey="marker.id">
<ui-gmap-window>
<div>{{marker.window.title}}</div>
</ui-gmap-window>
</ui-gmap-marker>
</ui-gmap-google-map>
</div>
My Angular代码如下所示:
var myApp = angular.module('myApp', ['uiGmapgoogle-maps']);
myApp.controller("gMap",function($scope){
$scope.map = {
center: { latitude: 39.8282, longitude: -98.5795 },
zoom: 4
};
$scope.markers.id = "1";
$scope.markers.coords.latitude = "40.7903";
$scope.markers.coords.longitude = "-73.9597";
$scope.markers.window.title = "Manhattan New York, NY";
});
答案 0 :(得分:1)
稍微更改了代码并获得了结果。
var myApp = angular.module('myApp', ['uiGmapgoogle-maps']);
myApp.controller("gMap",function($scope){
$scope.map = {
center: { latitude: 39.8282, longitude: -98.5795 },
zoom: 4
};
$scope.marker = {
coords: { latitude: 39.8282, longitude: -98.5795 },
id: 4 ,
window: { title: "Manhattan New York, NY" }
};
});
AND HTML成为:
<div ng-app="myApp" ng-controller="gMap">
<ui-gmap-google-map
center='map.center'
zoom='map.zoom' aria-label="Google map">
<ui-gmap-marker
coords="marker.coords" options="marker.options" events="marker.events" idkey="marker.id">
<ui-gmap-window>
<div>{{marker.window.title}}</div>
</ui-gmap-window>
</ui-gmap-marker>
</ui-gmap-google-map>
</div>
答案 1 :(得分:1)
将控制器更改为:
myApp.controller("gMap",function($scope){
$scope.map = {
center: { latitude: 39.8282, longitude: -98.5795 },
zoom: 4
};
$scope.markers = [];
$scope.marker = {
id: 1,
coords: {
latitude: "40.7903",
longitude: "-73.9597",
},
window: {
title: "Manhattan New York, NY",
}
};
$scope.markers.push($scope.marker);
});
为什么?
markers
是HTML中的数组。在控制器中你没有声明它。
marker
是一个对象。
您现在可以创建更多标记并将其推送到markers
以使用$scope.markers.push($scope.otherMarker);
答案 2 :(得分:1)