3

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:

SearchBox Demo

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
2
  • 1
    Can you show the xaml? Jan 14, 2017 at 0:47
  • I'll edit the question to show you the View (not built with XAML).
    – Rob Lyndon
    Jan 14, 2017 at 1:03

2 Answers 2

2

It looks like this is an issue with the Visual Studio emulator. When I run the debugger on my device via a USB, it has no problem finding the correct results.

1

From looking at your code, I suspect the issue is that you need to make a public property for the searchResults, and bind it from the map:

member this.SearchResults with get() = searchResults

This would allow you to bind the search results.

You also will likely need to switch to the UI threading context before adding to searchResults within the async workflow. Adding to an ObservableCollection<'T> in a background thread typically results in nothing being rendered, or a crash.

1
  • I'm afraid it doesn't get as far as this. The results collection is always empty. I'm using ReactiveUI, so I should probably do something sophisticated like use a ReactiveCollection and observe it on the UI thread. The trouble is, if I don't find any results, there will be nothing for the UI thread to do.
    – Rob Lyndon
    Jan 14, 2017 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.