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>
4

2 回答 2

5

Try the following code

<!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>
于 2013-09-06T12:28:26.513 回答
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)
      }));
};
于 2014-02-04T10:45:19.903 回答