admin管理员组文章数量:1023273
I'm building an ASP.NET MVC site where I want to have a Google Maps Javascript API map that shows markers loaded from my backend through AJAX.
As I don't want the client to run into memory issues, I want to lazy-load the markers and apply them to Fluster2 to put them into clusters. I think the best way to lazy-load the markers is to add an event listener to the idle
event of the Map, which occurs after the map is panned or zoomed.
Here's my current strategy:
- Add event listener to
idle
event. - When
idle
event is thrown, use jQuery to make a AJAXHTTP POST
call to my backend, supplying the current viewport/bounds of the map. - The backend returns all the points inside the viewport.
- The points are created into markers and added to Fluster2, which adds them to the map. Old points are NOT discarded.
- Repeat
However, this can create issues, as the old points aren't discarded. I don't want to discard them, as I don't want to load them again, but with my current strategy, I would be loading them a second time and creating markers for them a second time.
While I think it's not a good idea to somehow tell the backend in the AJAX call that I already have some of the markers, it'd be nice to implement some sort of a hashtable that doesn't allow duplicates.
That way, when I load the points, I can try adding them to the hashtable first: if that succeeds, I haven't displayed them yet, so I can add them as markers; if it fails, they're already on the map.
Is this a good strategy? If so, how can I implement a hashtable that doesn't allow duplicates?
I'm building an ASP.NET MVC site where I want to have a Google Maps Javascript API map that shows markers loaded from my backend through AJAX.
As I don't want the client to run into memory issues, I want to lazy-load the markers and apply them to Fluster2 to put them into clusters. I think the best way to lazy-load the markers is to add an event listener to the idle
event of the Map, which occurs after the map is panned or zoomed.
Here's my current strategy:
- Add event listener to
idle
event. - When
idle
event is thrown, use jQuery to make a AJAXHTTP POST
call to my backend, supplying the current viewport/bounds of the map. - The backend returns all the points inside the viewport.
- The points are created into markers and added to Fluster2, which adds them to the map. Old points are NOT discarded.
- Repeat
However, this can create issues, as the old points aren't discarded. I don't want to discard them, as I don't want to load them again, but with my current strategy, I would be loading them a second time and creating markers for them a second time.
While I think it's not a good idea to somehow tell the backend in the AJAX call that I already have some of the markers, it'd be nice to implement some sort of a hashtable that doesn't allow duplicates.
That way, when I load the points, I can try adding them to the hashtable first: if that succeeds, I haven't displayed them yet, so I can add them as markers; if it fails, they're already on the map.
Is this a good strategy? If so, how can I implement a hashtable that doesn't allow duplicates?
Share Improve this question edited May 14, 2012 at 13:31 mmmmmm 32.7k28 gold badges91 silver badges122 bronze badges asked Jun 30, 2010 at 20:16 Maxim ZaslavskyMaxim Zaslavsky 18.1k30 gold badges111 silver badges174 bronze badges1 Answer
Reset to default 4I've recently implemented something quite similar; while I personally opted to remove all markers and pletely refill the maps, I actually like your overall strategy and think it might also make the user experience more fluid, as the short flicker between removing all markers and adding the new ones disappears. So yea, I'd say it's a good strategy.
Idle is an interesting event; I've found that onZoom etc didn't work particularly well and settled for TileLoaded (or similar). Works fine for me but does seem a bit like a hack.
Concerning your hashtable question - there's two possible places where you could store such a map.
a) Server If you can keep track of which page you're sending coordinates to, you could keep a dotnet Hashtable of the markers in your session state. This might keep you from sending unnecessary coordinates to the client, but is more errorprone imo
b) Client This is probably what you want to go for anyway. Here, a normal Javascript associative array should be your friend.
var x = {"some key": "some value"};
document.write ("some key" in x); // True
document.write ("some other key" in x); // False
Unless you find a good way of mapping lat and lng into a single hashable value, you might have to keep an array of arrays:
var markers = {lat1: {lng11: 1, lng12: 1, lng13: 1}, lat2: ...}
and then check like
...
if (checklat in markers)
if (checklng in markers[checklat])
return True
return False
I just see that this is an older question. Well too late, maybe it still helps.
I'm building an ASP.NET MVC site where I want to have a Google Maps Javascript API map that shows markers loaded from my backend through AJAX.
As I don't want the client to run into memory issues, I want to lazy-load the markers and apply them to Fluster2 to put them into clusters. I think the best way to lazy-load the markers is to add an event listener to the idle
event of the Map, which occurs after the map is panned or zoomed.
Here's my current strategy:
- Add event listener to
idle
event. - When
idle
event is thrown, use jQuery to make a AJAXHTTP POST
call to my backend, supplying the current viewport/bounds of the map. - The backend returns all the points inside the viewport.
- The points are created into markers and added to Fluster2, which adds them to the map. Old points are NOT discarded.
- Repeat
However, this can create issues, as the old points aren't discarded. I don't want to discard them, as I don't want to load them again, but with my current strategy, I would be loading them a second time and creating markers for them a second time.
While I think it's not a good idea to somehow tell the backend in the AJAX call that I already have some of the markers, it'd be nice to implement some sort of a hashtable that doesn't allow duplicates.
That way, when I load the points, I can try adding them to the hashtable first: if that succeeds, I haven't displayed them yet, so I can add them as markers; if it fails, they're already on the map.
Is this a good strategy? If so, how can I implement a hashtable that doesn't allow duplicates?
I'm building an ASP.NET MVC site where I want to have a Google Maps Javascript API map that shows markers loaded from my backend through AJAX.
As I don't want the client to run into memory issues, I want to lazy-load the markers and apply them to Fluster2 to put them into clusters. I think the best way to lazy-load the markers is to add an event listener to the idle
event of the Map, which occurs after the map is panned or zoomed.
Here's my current strategy:
- Add event listener to
idle
event. - When
idle
event is thrown, use jQuery to make a AJAXHTTP POST
call to my backend, supplying the current viewport/bounds of the map. - The backend returns all the points inside the viewport.
- The points are created into markers and added to Fluster2, which adds them to the map. Old points are NOT discarded.
- Repeat
However, this can create issues, as the old points aren't discarded. I don't want to discard them, as I don't want to load them again, but with my current strategy, I would be loading them a second time and creating markers for them a second time.
While I think it's not a good idea to somehow tell the backend in the AJAX call that I already have some of the markers, it'd be nice to implement some sort of a hashtable that doesn't allow duplicates.
That way, when I load the points, I can try adding them to the hashtable first: if that succeeds, I haven't displayed them yet, so I can add them as markers; if it fails, they're already on the map.
Is this a good strategy? If so, how can I implement a hashtable that doesn't allow duplicates?
Share Improve this question edited May 14, 2012 at 13:31 mmmmmm 32.7k28 gold badges91 silver badges122 bronze badges asked Jun 30, 2010 at 20:16 Maxim ZaslavskyMaxim Zaslavsky 18.1k30 gold badges111 silver badges174 bronze badges1 Answer
Reset to default 4I've recently implemented something quite similar; while I personally opted to remove all markers and pletely refill the maps, I actually like your overall strategy and think it might also make the user experience more fluid, as the short flicker between removing all markers and adding the new ones disappears. So yea, I'd say it's a good strategy.
Idle is an interesting event; I've found that onZoom etc didn't work particularly well and settled for TileLoaded (or similar). Works fine for me but does seem a bit like a hack.
Concerning your hashtable question - there's two possible places where you could store such a map.
a) Server If you can keep track of which page you're sending coordinates to, you could keep a dotnet Hashtable of the markers in your session state. This might keep you from sending unnecessary coordinates to the client, but is more errorprone imo
b) Client This is probably what you want to go for anyway. Here, a normal Javascript associative array should be your friend.
var x = {"some key": "some value"};
document.write ("some key" in x); // True
document.write ("some other key" in x); // False
Unless you find a good way of mapping lat and lng into a single hashable value, you might have to keep an array of arrays:
var markers = {lat1: {lng11: 1, lng12: 1, lng13: 1}, lat2: ...}
and then check like
...
if (checklat in markers)
if (checklng in markers[checklat])
return True
return False
I just see that this is an older question. Well too late, maybe it still helps.
本文标签: Strategy for lazyloading markers into a Google Maps Javascript API mapStack Overflow
版权声明:本文标题:Strategy for lazy-loading markers into a Google Maps Javascript API map - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745597868a2158278.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论