Skip to content

Commit 0c3ca0a

Browse files
committed
fix issue that markers' watcher should take the deep watch mode. and previous markers should be remove after markers changed
1 parent c1f9e2e commit 0c3ca0a

5 files changed

Lines changed: 35 additions & 7 deletions

File tree

baiduMap.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
* <baidu-map options='options' ></baidu-map>
3939
* comments: An improvement that the map should update automatically while coordinates changes
4040
*
41+
* @version 1.2.1
42+
* comments: Accounding to 史魁杰's comments, markers' watcher should have set deep watch equal to true, and previous overlaies should be removed
43+
*
4144
*/
4245
(function(global, factory) {
4346
'use strict';
@@ -126,6 +129,8 @@
126129
}
127130
//create markers
128131

132+
var previousMarkers = [];
133+
129134
var openInfoWindow = function(infoWin) {
130135
return function() {
131136
this.openInfoWindow(infoWin);
@@ -134,7 +139,15 @@
134139

135140
var mark = function() {
136141

137-
for (var i in opts.markers) {
142+
var i = 0;
143+
144+
for (i = 0; i < previousMarkers.length; i++) {
145+
previousMarkers[i].removeEventListener('click', openInfoWindow(infoWindow2));
146+
map.removeOverlay(previousMarkers[i]);
147+
}
148+
previousMarkers.length = 0;
149+
150+
for (i = 0; i < opts.markers.length; i++) {
138151
var marker = opts.markers[i];
139152
var pt = new BMap.Point(marker.longitude, marker.latitude);
140153
var marker2;
@@ -149,6 +162,7 @@
149162

150163
// add marker to the map
151164
map.addOverlay(marker2);
165+
previousMarkers.push(marker2);
152166

153167
if (!marker.title && !marker.content) {
154168
return;
@@ -172,7 +186,7 @@
172186

173187
$scope.$watch('options.markers', function(newValue, oldValue) {
174188
mark();
175-
});
189+
}, true);
176190

177191
},
178192
template: '<div style="width: 100%; height: 100%;"></div>'

baiduMap.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "BaiduMapForAngularJS",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"homepage": "https://github.com/leftstick/BaiduMapForAngularJS",
55
"authors": [
66
"Howard.Zuo"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-baidu-map",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "A baidu-map directive for AngularJS",
55
"main": "./src/baiduMap.js",
66
"repository": {

src/baiduMap.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
* <baidu-map options='options' ></baidu-map>
3939
* comments: An improvement that the map should update automatically while coordinates changes
4040
*
41+
* @version 1.2.1
42+
* comments: Accounding to 史魁杰's comments, markers' watcher should have set deep watch equal to true, and previous overlaies should be removed
43+
*
4144
*/
4245
(function(global, factory) {
4346
'use strict';
@@ -126,6 +129,8 @@
126129
}
127130
//create markers
128131

132+
var previousMarkers = [];
133+
129134
var openInfoWindow = function(infoWin) {
130135
return function() {
131136
this.openInfoWindow(infoWin);
@@ -134,7 +139,15 @@
134139

135140
var mark = function() {
136141

137-
for (var i in opts.markers) {
142+
var i = 0;
143+
144+
for (i = 0; i < previousMarkers.length; i++) {
145+
previousMarkers[i].removeEventListener('click', openInfoWindow(infoWindow2));
146+
map.removeOverlay(previousMarkers[i]);
147+
}
148+
previousMarkers.length = 0;
149+
150+
for (i = 0; i < opts.markers.length; i++) {
138151
var marker = opts.markers[i];
139152
var pt = new BMap.Point(marker.longitude, marker.latitude);
140153
var marker2;
@@ -149,6 +162,7 @@
149162

150163
// add marker to the map
151164
map.addOverlay(marker2);
165+
previousMarkers.push(marker2);
152166

153167
if (!marker.title && !marker.content) {
154168
return;
@@ -172,7 +186,7 @@
172186

173187
$scope.$watch('options.markers', function(newValue, oldValue) {
174188
mark();
175-
});
189+
}, true);
176190

177191
},
178192
template: '<div style="width: 100%; height: 100%;"></div>'

0 commit comments

Comments
 (0)