Google地图标记未显示

时间:2013-09-06 11:53:13

标签: javascript angularjs google-maps google-maps-api-3

我是谷歌地图API的新手,并使用下面的代码。

不知何故,地图上没有显示标记。

我正在使用谷歌地图API v3。

.controller('MapCtrl', ['$scope', 
           function MapCtrl($scope) {
               var ll = new google.maps.LatLng(38.895112, -77.036366);
               $scope.mapOptions = {
               center: ll,
               zoom: 12,
               mapTypeId: google.maps.MapTypeId.ROADMAP
             };
               $scope.onMapIdle = function() {
                   if ($scope.myMarkers === undefined){    
               var marker = new google.maps.Marker({
               map: $scope.myMap,
               position: ll
                });
               $scope.myMarkers = [marker, ];
            }
        };
              $scope.markerClicked = function(m) {
              window.alert("clicked");
        };

    }   
])

HTML代码如下:

<div ng-app='localAdventuresApp'>
        <div ng-controller="MapCtrl">
            <div id="map_canvas" ui-map="myMap" 
            style="height:300px;width:400px;border:2px solid #777777;margin:3px; border:1px solid" 
            ui-options="mapOptions" 
            ui-event="{'map-idle' : 'onMapIdle()'}"
            >
            </div>
                    <div ng-repeat="marker in myMarkers" ui-map-marker="myMarkers[$index]"
                            ui-event="{'map-click': 'markerClicked(marker)'}">
            </div>
        </div>
</div>

2 个答案:

答案 0 :(得分:5)

尝试以下代码

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple markers</title>
<link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
var myLatlng = new google.maps.LatLng(38.895112,-77.036366);
var mapOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

var marker = new google.maps.Marker({
  position: myLatlng,
  map: map,
  title: 'Hello World!'
 });
}

google.maps.event.addDomListener(window, 'load', initialize);

</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>

答案 1 :(得分:2)

事实上,它适用于AngularJS + Google MAPS V3 + UI-MAP

如果在启动地图时没有显示标记,那是因为地图尚未加载。 我找到了一个解决这个问题的解决方案:

在HTML中你用'map-tilesloaded'来声明ui-event女巫是由地图事件'tilesloaded'触发的:

<div ui-map="myMap" class="map-canvas" 
       ui-event="{'map-tilesloaded': 'maploaded()'}"
       ui-options="mapOptions">
</div>
<div ng-repeat="marker in myMarkers" ui-map-marker="myMarkers[$index]"
         ui-event="{'map-click': 'openMarkerInfo(marker)'}">
 </div>

然后在你的控制器中你可以添加标记:

$scope.maploaded = function() {
    $scope.myMarkers.push(new google.maps.Marker({
        map: $scope.myMap,
        position: new google.maps.LatLng(mylat, mylon)
      }));
};