none
How I can make use of Microsoft Rx Framework to implement Bing Map effectively in WinRT / Windows 8.

    Question

  • In my e-commerce application I need to plot my nearby stores in Bing map, And my another requirement is during the time of zooming and paning the map I need to update my stores based on the map centre. So for implementing this I primarily choose the traditional way of coding. The steps are given below.

    1. Initial launch I will send the api request for location and will plot the stores on Map.
    2. In Maps ViewChanged event I will send the subsequent requests for the nearby stores based on the maps current store.

    So during this implementation, I am getting around 400 stores in single api request. And I will plot this on map. But when I zoom or pan the map it sends several requests simultaneously and try to update the pushpins on UI , eventually it will block the UI and Map behaves horribly in my app.

    During the Google search I found many suggestions regarding using Microsoft Rx framework to implement similar functionality. But didn’t get any proper code samples to achieve my goal. Can anyone please help me or guide me to solve my issue.

    Remember I need to plot on an average of 400 stores in map on a single request.

    Regards,

    Stez.   

    Tuesday, June 18, 2013 8:31 AM

All replies

  • Hi Stez,

    Throttle can be used to ignore ViewChanged events that occur within a certain proximity.  This covers the case whereby the user is panning or zooming and the map is raising lots of ViewChanged events.  Throttle only reacts to the last event in a close sequence of events.

    You'll also probably want to use TakeUntil or Switch in your query to ignore requests that haven't returned yet while simultaneously receiving ViewChanged events.  This covers the case whereby the user finishes panning or zooming, waits for a bit, then begins panning or zooming again.  If the original request is still in flight, then TakeUntil/Switch will ignore its response since it's already outdated.

    For example: (Untested)

    IObservable<Foo> viewChanges = GetViewChangedAsObservable();
    
    viewChanges
    	.Throttle(TimeSpan.FromSeconds(1))
    	.Select(foo => SendRequestAsObservable(foo))
    	.Switch()
    	.ObserveOnDispatcher()
    	.Subscribe(UpdatePins);

    You can play around with the Throttle period until you get the behavior you like.  Making the period smaller will result in a more real-time view, though of course it depends on how fast the service responds while the user is panning/zooming.  Making the period larger will prevent overlapping requests.  It's probably reasonable to use 1 second, assuming that the service request generally takes under 1 second.  Though if you want more accuracy, then test your application in a production environment to determine the most appropriate value.

    SendRequestAsObservable should simply send the request.  If you're using an async API, then simply convert it using ToObservable.

    For example:

    IObservable<PinCollection> SendRequestAsObservable(Foo foo)
    {
    	var service = new PinService();
    	return service.GetPinsAsync(foo).ToObservable().Finally(service.Dispose);
    }

    - Dave


    http://davesexton.com/blog

    • Proposed as answer by Rakesh R Nair Wednesday, June 19, 2013 6:55 AM
    Tuesday, June 18, 2013 7:42 PM