I've been building a Xamarin Forms-based app with mapping enabled, using this guide. The map is working without any problems; however, the geocoding functionality is failing silently.
My View contains a SearchBox
, which is wired up to a command that uses a Xamarin Forms Geocoder
instance to find user-defined addresses. The command is firing correctly when an address is entered into the SearchBox
:
However, while I'm debugging, the Geocoder returns instantly, coming back with no results at all. The action happens in the geocodeAddress
function below:
type DashboardViewModel(?host: IScreen, ?platform: IPlatform) as this =
inherit ReactiveViewModel()
let host, platform = LocateIfNone host, LocateIfNone platform
let searchResults = new ObservableCollection<GeodesicLocation>()
let geocodeAddress(vm: DashboardViewModel) =
let vm = match box vm with | null -> this | _ -> vm
searchResults.Clear()
async {
let! results = platform.Geocoder.GetPositionsForAddressAsync(vm.SearchAddress) |> Async.AwaitTask
results |> Seq.map (fun r -> new GeodesicLocation(r.Latitude * 1.0<deg>, r.Longitude * 1.0<deg>)) |> Seq.iter searchResults.Add
} |> Async.StartAsTask
let searchForAddress = ReactiveCommand.CreateFromTask geocodeAddress
let mutable location = new GeodesicLocation()
let mutable searchAddress = String.Empty
member __.Title with get() = LocalisedStrings.AppTitle
member __.SearchForAddress with get() = searchForAddress
member this.SearchAddress with get() = searchAddress and set(value) = this.RaiseAndSetIfChanged(&searchAddress, value, "SearchAddress") |> ignore
member this.LatitudeDegrees
with get() = location.Latitude / 1.0<deg>
and set(value) = location <- new GeodesicLocation(value * 1.0<deg>, location.Longitude); this.RaisePropertyChanged("LatitudeDegrees")
member this.LongitudeDegrees
with get() = location.Longitude / 1.0<deg>
and set(value) = location <- new GeodesicLocation(location.Latitude, value * 1.0<deg>); this.RaisePropertyChanged("LongitudeDegrees")
interface IRoutableViewModel with
member __.HostScreen = host
member __.UrlPathSegment = "Dashboard"
According to the Xamarin docs, the Geocoder
class should work out of the box. Am I missing some kind of setup?
UPDATE: The code for the View is as follows. This isn't built with XAML, but it's an F#-based structure that is reasonably similar to XAML.
type DashboardView(theme: Theme) as this =
inherit ContentPage<DashboardViewModel, DashboardView>(theme)
new() = new DashboardView(Themes.AstridTheme)
override __.CreateContent() =
theme.GenerateGrid([|"Auto"; "*"|], [|"*"|]) |> withColumn(
[|
theme.VerticalLayout() |> withBlocks(
[|
theme.GenerateLabel(fun l -> this.Title <- l)
|> withAlignment LayoutOptions.Center LayoutOptions.Center
|> withOneWayBinding(this.ViewModel, this, <@ fun (vm: DashboardViewModel) -> vm.Title @>, <@ fun (v: DashboardView) -> (v.Title: Label).Text @>)
theme.GenerateSearchBar(fun sb -> this.AddressSearchBar <- sb)
|> withSearchBarPlaceholder LocalisedStrings.SearchForAPlaceOfInterest
|> withTwoWayBinding(this.ViewModel, this, <@ fun (vm: DashboardViewModel) -> vm.SearchAddress @>, <@ fun (v: DashboardView) -> (v.AddressSearchBar: SearchBar).Text @>)
|> withSearchCommand this.ViewModel.SearchForAddress
|])
theme.GenerateMap(new GeodesicLocation(51.4<deg>, 0.0<deg>), 4.0<km>, fun m -> this.Map <- m)
|> withTwoWayBinding(this.ViewModel, this, <@ fun (vm: DashboardViewModel) -> vm.LatitudeDegrees @>, <@ fun (v:DashboardView) -> (v.Map: Map).VisibleRegion.LatitudeDegrees @>)
|> withTwoWayBinding(this.ViewModel, this, <@ fun (vm: DashboardViewModel) -> vm.LongitudeDegrees @>, <@ fun (v:DashboardView) -> (v.Map: Map).VisibleRegion.LongitudeDegrees @>)
|]) |> createFromColumns :> View
member val AddressSearchBar = Unchecked.defaultof<SearchBar> with get, set
member val Title = Unchecked.defaultof<Label> with get, set
member val Map = Unchecked.defaultof<Map> with get, set