I created a very simple test app to try and reverse geocode my current lat/long into an address.
Here is the code for my ViewModel:
namespace LoginProductsMVVM.Core.ViewModels
{
public class ProductDetailViewModel
: MvxViewModel
{
public void Init(Product product)
{
Product = product;
}
private Product _product;
public Product Product
{
get { return _product; }
set { _product = value;
RaisePropertyChanged (() => Product); }
}
private string _latitude;
public string Latitude{
get { return _latitude; }
set { _latitude = value; RaisePropertyChanged(() => Latitude); }
}
private string _longitude;
public string Longitude{
get { return _longitude; }
set { _longitude = value; RaisePropertyChanged(() => Longitude); }
}
private string _address;
public string Address{
get { return _address; }
set { _address = value; RaisePropertyChanged(() => Address); }
}
private IMvxGeoLocationWatcher _watcher;
public IMvxGeoLocationWatcher Watcher
{
get
{
_watcher = Mvx.Resolve<IMvxGeoLocationWatcher> ();
return _watcher;
}
}
public ProductDetailViewModel(IMvxGeoLocationWatcher watcher)
{
_watcher = watcher;
_watcher.Start (new MvxGeoLocationOptions (), OnLocation, OnError);
}
void OnLocation (MvxGeoLocation location)
{
Latitude = location.Coordinates.Latitude.ToString();
Longitude = location.Coordinates.Longitude.ToString();
// Android Location specific stuff
var activity = Mvx.Resolve<IMvxAndroidCurrentTopActivity> ().Activity;
Geocoder geocdr = new Geocoder (activity.BaseContext);
IList<Address> addresses = geocdr.GetFromLocation (double.Parse(Latitude), double.Parse(Longitude), 1);
addresses.ToList().ForEach ((addr) => Address += addr.ToString() + "\r\n\r\n");
}
void OnError (MvxLocationError error)
{
Mvx.Error ("Seen location error {0}", error);
}
}
}
I have a break point in my OnLocation method but it never gets in there. Am I missing something for this to work properly on Android? It appears to work just fine for iOS...
Per Odahan here:
Well... I investigated a bit more : The problem is known and can be seen on many devices. This is not a MvvmCross problem. Short answer : the device needs to be rebooted and all is working like a charm... It seems Google sent some updates that are causing the problem. Here is a thread speaking about this problem and a similar one concerning the GeoCode class :
https://code.google.com/p/android/issues/detail?id=38009
So : can be closed, MvvmCross is ok but others can face this bug so my explanations and the link here.
When you say it never fires 'success', does the success actually ever occur?
There could be lots of things going wrong in the GPS code - eg your app might not have privilege, your phone might have A-GPS or GPS disabled, or you might even be running on a location-less emulator - all of this is possible from your description.
It'a also worth noting that Xamarin.Android has long standing issues with hitting breakpoints - so it's better to add trace than to rely on breakpoint hitting :/
Perhaps try running though N=8 from http://mvvmcross.wordpress.com/ - does that help at all? (N=9 is also worth watching as it shows one way to allow multiple viewmodels to use the geowatcher)
来源:https://stackoverflow.com/questions/17703427/mvvmcross-imvxgeolocationwatcher-success-action-never-fires-on-android