Monday, May 5, 2014

Delaying Queries to a Server from Map Move Events



Last week I added BikeShare info to my map. But I also take the bus a lot. I would like the information I need to choose between taking a bus and grabbing a bike. So I need to show buses on the map. In my first iteration of showing bus data, I downloaded the XML for every bus stop in the metro system and loaded that. Unfortunately, this did not work. 

There are over 11,000 bus stops in the DC Metro System and using that may POI in a MapItemView made the map unusably slow. Even if it wasn't slow, when zoomed out on the map, the POI covered the map so it was just a huge blob of POI. Oh well, looks like I need to do something slightly more elegant. 

What I need to do is query the server every time the user moves the map, through a pan or a zoom and ask the web service for the XML for the bus stops centered on the center of the map, and within a certain radius that is slightly bigger than the map. However, if I do that for each and eveyr pan and zoom, I end up hammering the server when ever the user swipes. The web service doesn't like to be called so much so fast, and it's just a waste of resources and bandwidth. So I decided to add a little logic to only load the data after the map is "at rest" for some period of time. I chose to start with a two second rest, but it could be whatever. I did this by first creating a timer.

       Timer
       {
         id: mapRestTimer  
         running: false  
         interval: 2000  
       
       }  

Then I wrote add some event handlers to the Map, for zoom and pan events.
       onZoomLevelChanged:  
       {  
         if(zoomLevel >= minimumBusDataZoomLevel)  
         {  
           timeMapRest()  
         }  
       }  
       onCenterChanged:  
       {  
         if(zoomLevel >= minimumBusDataZoomLevel)  
         {  
           timeMapRest()  
         }  
       }  
Note that I set it up so that bus stops aren't displayed if the user is panned out too far due to the problems I mentioned above. The timeMapRest function just decides whether it needs to restart the timer, or to start it. If the timer is already running, then the user has moved the map before the rest period was up, so we'll reset the timer. Otherwise, we'll go ahead and start the timer.
   function timeMapRest()  
       {  
         if(mapRestTimer.running)  
         {  
           mapRestTimer.restart()  
         }  
         else  
         {  
           mapRestTimer.start()  
         }  
       }  

To finish up, I just had to write an onTriggered handler and set properties on my subclassed XmlListModel, which then goes ahead an updates itself based on the new latlong or zoom level.
         id: mapRestTimer  
         running: false  
         interval: 2000  
         onTriggered:  
         {  
           //stop the timer and set the new latlong  
           running: false  

           //get the info necessary to calculate the correct radius
           busStopListModel.latlong = [getThereMap.center.latitude,getThereMap.center.longitude]  
           var tr = getThereMap.toCoordinate(Qt.point(0,0))  
           var bl = getThereMap.toCoordinate(Qt.point(getThereMap.width,getThereMap.height))  
           var latdiff = bl.latitude - tr.latitude  
           var londiff = bl.longitude - tr.longitude  
           //TODO: fix this silly calculation for the radius  
           var rad = (latdiff > londiff) ? latdiff * (34.67 * 5280 / 3) : londiff * (34.67 * 5280 / 3 )  
           busStopListModel.mapRadius = rad  
         }  
       }  


The code for the whole project is here.

26 comments:

  1. Wouldn't it be better to get the XML data for all bus stops and do the filtering locally? Doing 11,000 radius checks shouldn't be too difficult.

    ReplyDelete
  2. So nice and great blog post about map it's also helpful information all of us so thanks a lot for sharing this tips with us
    clipping path service

    ReplyDelete

  3. That was a great and comprehensive article…all the tips enumerated and explained will be helpful for those who are wise enough to tap from it. Any business nowadays without social media signals and presence may not make it to the outermost, and investment too is part of the key to success in business.

    ReplyDelete
  4. All the articles here are informative. It is beneficial to the newcomers. Thanks for posting such a
    nice post.
    Clipping Path
    Image Masking Service

    ReplyDelete
  5. Quickbooks Tool Hub is a multipurpose tool that is used to diagnose common errors in Quickbooks. It is the combination of all tools in one application. You can fix login issues, installation issues, printing issues, and network connectivity issues.
    Download quickbooks tool hub

    ReplyDelete
  6. This is too cool to watch even, Likewise if you want to know about the tool that rectifies accounting error of your Quickbooks and various network errors, then check out Quickbooks File Doctor

    ReplyDelete
  7. Great blog here with all of the valuable information you have. Keep up the good work you are doing here. write my dissertation

    ReplyDelete
  8. I really like your articles, it is easy to understand. You are a good blogger. Pls keep it up. See also: University of Tennessee Enrollment 2021

    ReplyDelete
  9. It’s really a nice and useful piece of information. I’m glad that you shared this helpful info with us. Please keep us up to date like this. Thanks for sharing. 야한동영상

    ReplyDelete
  10. 카지노사이트
    바카라사이트
    온라인카지노사이트




    Hello There. I found your blog using msn. This is a really well
    written article. I’ll make sure to bookmark it and come back to read more of your
    useful info. Thanks for the post. I’ll definitely return.

    ReplyDelete
  11. 카지노사이트
    카지노사이트홈
    카지노

    You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and extremely broad for me. I’m looking forward for your next post, I’ll try to get the hang of it

    ReplyDelete
  12. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further post thanks once again.


    토토사이트
    토토
    안전놀이터

    ReplyDelete
  13. 토토사이트 Pretty section of content. I simply stumbled upon your weblog and in accession capital to say that I
    acquire actually loved account your blog posts.
    Any way I’ll be subscribing in your feeds or even I success you get right of
    entry to constantly rapidly.

    ReplyDelete
  14. Sage Intacct Login Error can occur when the login information submitted on Bill.com’s Sync Login Info page is invalid.

    Sage Intacct Login
    Sage 50
    Sage 100

    ReplyDelete
  15. Góð síða : Resep
    Góð síða : Sinopsis Film
    Góð síða : Paling Sedikit
    Góð síða : Logo
    Góð síða : Biodata
    Góð síða : Resep Masakan
    Góð síða : Sinopsis Film Terbaru
    Góð síða : Paling Dalam

    ReplyDelete
  16. How do Troubleshoot Canon Printer not printing color?

    To fix issues related to Canon printer not printing in color, you are supposed to follow and apply few important instructions. To proceed, you must check your ink cartridge, now update your printer driver and then check color printing settings and then restart your printer. Once you do follow and apply the steps and so you will be able to resolve the issues without any fuss. Have a look at the steps to resolve all your problems without any fuss.

    How to Block Yahoo emails?

    If you want to clear out some unwanted emails and block them, this guide will benefit you. Yahoo allows you to add any email addresses to the blocked list anonymously, and you can also remove them if you change your mind sometime in the future. To block yahoo mails, click on the Settings icon and select More Settings Image. Then tap on Security and Privacy and choose Add button under the section on Blocked Addresses. Then enter the email address and click on Save.

    How to make Yahoo my Homepage Firefox?

    If you often use Yahoo, you should set Yahoo as your default homepage on your computer browser. This way, each time you open your browser, you'll be able to access different Yahoo services. Here are the steps to make yahoo my homepage Firefox- Click the Menu button on the Mozilla Firefox web browser on your computer (three horizontal lines). This will open a new tab with the Firefox settings page. After that, go to the "Home Page" section and type in the Yahoo homepage's URL. Make sure the "Show my home page" option in the "When Firefox begins" section is chosen. On the Firefox browser, the Yahoo website will be set as your default home page.

    Steps to stop outlook calendar email notifications

    Remembering important events in the Outlook calendar can be done. The reminders appear on the screen, prompting you of its date. But if you wish to turn off the Outlook calendar notifications, you can do that as well. For that, open Outlook and click on “Calendar.” Then click on File and select Options. Then tap on Calendar and uncheck the “Default reminders” checkbox to stop outlook calendar email notifications.Lastly, click on Ok to complete.

    Reasons why icloud email is not working properly

    There could be many reasons for iCloud not working correctly. And you must know about these reasons because that will help you avoid them in future. And if you know what caused an issue, it’ll be easier for you to solve it. One primary reason for iCloud not working is that the total size of an email message( text and attachments) cannot exceed 20MB. And this rule is as per Apple’s documentation which you should know in case icloud email is not working properly. So you can activate Mail Drop as it utilizes iCloud to store temporary files up to 5GB.

    ReplyDelete
  17. Are you getting QuickBooks Payroll Error 15243 when trying to update the QuickBooks Premier? Don’t worry this post will help you to overcome from this error.

    ReplyDelete
  18. I truly appreciate the amazing work and focus that you have on this site.Quite creative and knowldegeable information. It’s a nice time visiting your blog. Many thanks for sharing. ondo city polytechnic admission requirements

    ReplyDelete
  19. In this article we have explained the uses and benefits of QuickBooks gopayment support
    App. knows everything about its benefits and uses GoPayment App for easy and hassle free transactions.

    ReplyDelete
  20. Your use of real-world examples, especially the one involving a map-based application, made it so much easier for a non-tech-savvy person like me to grasp the concept. It's like you have this magic ability to demystify complex tech issues.
    best zoos in the world

    ReplyDelete