Newbee
Newbee

Reputation: 817

Google maps marker not showing up

I am new to google maps API and using the code below.

Somehow the marker is not being displayed on the map.

I am using google maps 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 code is as below:

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

Upvotes: 3

Views: 5856

Answers (2)

user2549726
user2549726

Reputation: 361

In fact, it's for AngularJS + Google MAPS V3 + UI-MAP

If markers are not shown when starting map, it's because map is not still loaded. I founded one solution for solve this problem :

In HTML you declare ui-event with 'map-tilesloaded' witch is trigger by maps 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>

Then in your controller you can add markers :

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

Upvotes: 2

vinod_vh
vinod_vh

Reputation: 1061

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>

Upvotes: 5

Related Questions