0

I'm attempting to get MKMapKit annotations updating in real time in a Xamarin iOS project.

I'm using MvvmCross and have based the implementation on @slodge code and it's working great.

https://gist.github.com/slodge/6070386

What I'd like to be able to do now is what Stuart alludes to in one of his comments.

public class HouseAnnotation : MKAnnotation
{
    public HouseAnnotation(House house)
    {
        // use house here... 
        // in theory you could also data-bind to the house too (e.g. if it's location were to move...)
    }

    public override CLLocationCoordinate2D Coordinate { get; set; }
}

How would I go about binding the House coordinates to the HouseAnnotation.Coordinate?

So far I have been doing bindings like:

var bindingSet = this.CreateBindingSet<View, ViewModel>();

which works straight forward as you do this in viewDidLoad and have access to everything you need.

I feel like I'd naturally want to do

var bindingSet = myView.CreateBindingSet<HouseAnnotation, House>();

But this means passing a reference to myView down to the HouseAnnotation so it can be used to call CreateBindingSet on it and I doubt this would even work because House and HouseAnnotation aren't subclasses of any Mvx base class.

I feel I'm missing a bit of the puzzle here. Is anyone able to help me out here?

I understand that houses are unlikely to move but I'm preparing for all eventualities!

Justyn
  • 1,442
  • 12
  • 27
  • 1
    There's an n+1 video that covers this -' look for zombies (although I think the map update code needs some changes for IOS8) – Stuart Nov 05 '14 at 20:47
  • @slodge Thanks for that, exactly what I was looking for. I can't see any issues on iOS 8 as of yet. The map pins seem to be updating and animating into place as expected. – Justyn Nov 06 '14 at 12:07

1 Answers1

1

You can subscribe to changes on the house.Location property by using WeakSubscribe

The answer was in n+38 at around 24mins.

https://www.youtube.com/watch?v=JtXXmS3oHHY

public class HouseAnnotation : MKAnnotation
{
    private House _house;

    public HouseAnnotation(House house)
    {
        // Create a local reference
        _house = house;
        // We update now so the annotation Coordinate is set first time round
        UpdateLocation()
        // Subscribe to be notified of changes to the Location property to trigger the UpdateLocation method
        _house.WeakSubscribe<House>("Location", (s, e) => UpdateLocation());
    }

    private void UpdateLocation()
    {
        // Convert our house.Location to a CLLocationCoordinate2D and set it on the MKAnnotation.Coordinate property
        Coordinate = new CLLocationCoordinate2D(_house.Location.Lat, _house.Location.Lng);
    }

    public override CLLocationCoordinate2D Coordinate {
        get {
            return coord;
        }
        set {
            // call WillChangeValue and DidChangeValue to use KVO with
            // an MKAnnotation so that setting the coordinate on the
            // annotation instance causes the associated annotation
            // view to move to the new location.

            // We animate it as well for a smooth transition
            UIView.Animate(0.25, () => 
                {
                        WillChangeValue ("coordinate");
                        coord = value;
                        DidChangeValue ("coordinate");
                });
        }
    }
}
Justyn
  • 1,442
  • 12
  • 27