2
我目前正在使用下面的代碼獲取使用MVVMCross的位置。我正在使用Command來訪問視圖上的位置更新,以訪問GeoCoder對象&獲取地址數據。我從SO this question獲得了一些代碼,但似乎ViewModel駐留在同一個項目中,而不是在覈心中。我想知道這是否是實現這一目標的正確方法或我應該用另一種方法尋找位置的地址MVVMCross
視圖模型 `
public IMvxCommand LocationUpdate { get; set; }
public LocationViewModel(IMvxLocationWatcher locationWatcher)
{
_locationWatcher = locationWatcher;
FindCurrentLocation();
_timer = new Timer(OnTick, null, 1000, 1000);
}
private void OnLocation(MvxGeoLocation location)
{
Lat = location.Coordinates.Latitude;
Lng = location.Coordinates.Longitude;
if(LocationUpdate != null)
LocationUpdate.Execute();
_locationFound = true;
_locationWatcher.Stop();
}
private void FindCurrentLocation()
{
_locationFound = false;
_locationWatcher.Start(new MvxLocationOptions { Accuracy = MvxLocationAccuracy.Fine }, OnLocation, OnLocationError);
}
`
查看
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.LocationView);
_viewModel = (LocationViewModel) ViewModel;
_viewModel.LocationUpdate = new MvxCommand(LocationUpdated);
/* Initialisation code */
}
private void LocationUpdated()
{
var options = new MarkerOptions();
LatLng latLng = new LatLng(_viewModel.Lat, _viewModel.Lng);
options.SetPosition(latLng);
options.SetTitle(_viewModel.SiteName);
_site = _fragment.Map.AddMarker(options);
_fragment.Map.AnimateCamera(CameraUpdateFactory.NewLatLngZoom(latLng, 12));
Geocoder geocdr = new Geocoder(BaseContext);
IList<Address> addresses = geocdr.GetFromLocation(_viewModel.Lat, _viewModel.Lng, 1);
if (addresses.Any())
{
Address address = addresses.First();
_viewModel.SiteName = address.GetAddressLine(0);
_viewModel.SiteAddress = string.Concat(address.GetAddressLine(1),", ",
address.GetAddressLine(2), ", ", address.GetAddressLine(3));
}
}
@種子Seifer出版了一插件 - 見https://github.com/SeeD-Seifer/Mvx.Geocoder – Stuart