admin 管理员组文章数量: 1086019
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>
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>
Share
Improve this question
edited May 28, 2019 at 12:25
squaleLis
6,4942 gold badges25 silver badges31 bronze badges
asked Sep 6, 2013 at 11:53
NewbeeNewbee
8373 gold badges14 silver badges37 bronze badges
4
-
Where is
$scope.myMap
ing from? Where is thegoogle.maps.Map
object instantiated and assigned to$scope.myMap
? – André Dion Commented Sep 6, 2013 at 12:11 - Andre, what changes do you suggest in my code ?. – Newbee Commented Sep 6, 2013 at 13:48
-
1
André is asking where do you have something like this in your code:
$scope.myMap = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
Seems like you aren't initiating the map. – winkerVSbecks Commented Sep 6, 2013 at 13:58 - I changed that statement and its works fine now. Thanks winkerV. – Newbee Commented Sep 6, 2013 at 14:00
2 Answers
Reset to default 5Try 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./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>
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)
}));
};
本文标签: javascriptGoogle maps marker not showing upStack Overflow
版权声明:本文标题:javascript - Google maps marker not showing up - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744099611a2533437.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论