Skip to content

Commit eebb6ac

Browse files
committed
changed map pool to find by id. if id not given find ununsed one. Also changed getMap parameter to be string
1 parent b6087a3 commit eebb6ac

File tree

4 files changed

+80
-10
lines changed

4 files changed

+80
-10
lines changed

services/ng-map-pool.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,27 @@
2323
return map;
2424
};
2525

26-
var find = function(el) { //jshint ignore:line
26+
var findById = function(el, id) {
2727
var notInUseMap;
2828
for (var i=0; i<mapInstances.length; i++) {
2929
var map = mapInstances[i];
30+
if (map.id == id && !map.inUse) {
31+
var mapDiv = map.getDiv();
32+
el.appendChild(mapDiv);
33+
notInUseMap = map;
34+
break;
35+
}
36+
}
37+
return notInUseMap;
38+
};
39+
40+
var findUnused = function(el) { //jshint ignore:line
41+
var notInUseMap;
42+
for (var i=0; i<mapInstances.length; i++) {
43+
var map = mapInstances[i];
44+
if (map.id) {
45+
continue;
46+
}
3047
if (!map.inUse) {
3148
var mapDiv = map.getDiv();
3249
el.appendChild(mapDiv);
@@ -44,7 +61,7 @@
4461
* @return map instance for the given element
4562
*/
4663
var getMapInstance = function(el) {
47-
var map = find(el);
64+
var map = findById(el, el.id) || findUnused(el);
4865
if (!map) {
4966
map = add(el);
5067
} else {

services/ng-map.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,14 @@
4141
/**
4242
* @memberof NgMap
4343
* @function getMap
44-
* @param {Hash} options optional, e.g., {id: 'foo, timeout: 5000}
44+
* @param {String} optional, id e.g., 'foo'
4545
* @returns promise
4646
*/
47-
var getMap = function(options) {
48-
options = options || {};
49-
var deferred = $q.defer();
47+
var getMap = function(id) {
48+
id = typeof id === 'object' ? id.id : id;
5049

51-
var id = options.id || 0;
52-
var timeout = options.timeout || 2000;
50+
var deferred = $q.defer();
51+
var timeout = 2000;
5352

5453
function waitForMap(timeElapsed){
5554
if(mapControllers[id]){

testapp/ui-router.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
.state('baz', { url: "/baz", templateUrl: "baz.html" })
1818
});
1919
myapp.controller('fooCtrl', function($scope, NgMap){
20-
NgMap.getMap({id:'foomap'}).then(function(map) {
20+
NgMap.getMap('foomap').then(function(map) {
2121
console.log('NgMap.getMap in fooCtrl', map);
2222
});
2323
$scope.onClick = function() {
2424
alert('map clicked');
2525
}
2626
});
2727
myapp.controller('barCtrl', function(NgMap){
28-
NgMap.getMap({id:'barmap'}).then(function(map) {
28+
NgMap.getMap('barmap').then(function(map) {
2929
console.log('NgMap.getMap in barCtrl', map);
3030
});
3131
});

testapp/ui-router2.html

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>AngularJS: UI-Router Quick Start</title>
5+
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
6+
<script src="script-tags-for-development.js"></script>
7+
<link href="lib/bootstrap.min.css" rel="stylesheet">
8+
<script src="lib/angular-ui-router.js"></script>
9+
<link href="custom-marker.css" rel="stylesheet" />
10+
<script>
11+
var myapp = angular.module('myapp', ['ngMap',"ui.router"])
12+
myapp.controller('fooCtrl', function(NgMap) {
13+
NgMap.getMap('map1').then(function(map) {
14+
var latLng = new google.maps.LatLng(40.74, -74.18);
15+
var marker = new google.maps.Marker({
16+
position:latLng, draggable: true
17+
});
18+
marker.setMap(map);
19+
})
20+
});
21+
myapp.config(function($stateProvider, $urlRouterProvider){
22+
$urlRouterProvider.otherwise("/foo")
23+
$stateProvider
24+
.state('foo', { url: "/foo", templateUrl: "foo.html", controller: 'fooCtrl', controllerAs: 'vm' })
25+
.state('bar', { url: "/bar", templateUrl: "bar.html"})
26+
})
27+
</script>
28+
</head>
29+
30+
<body ng-app="myapp">
31+
This is to test not to share map instance if `id` is given
32+
<script type="text/ng-template" id="foo.html">
33+
<h1>Map 1</h1>
34+
<ng-map id="map1" center="[40.74, -74.18]"></ng-map>
35+
</script>
36+
37+
<script type="text/ng-template" id="bar.html">
38+
<h1>Map 2 - Should not have any markers </h1>
39+
<ng-map center="[40.74, -74.18]"></ng-map>
40+
</script>
41+
42+
<div class="navbar">
43+
<div class="navbar-inner">
44+
<ul class="nav">
45+
<li><a ui-sref="foo">Route 1</a></li>
46+
<li><a ui-sref="bar">Route 2</a></li>
47+
</ul>
48+
</div>
49+
</div>
50+
51+
<div class="well" ui-view></div>
52+
</body>
53+
54+
</html>

0 commit comments

Comments
 (0)