Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not call other Google Map service #17

Open
twuttiwat opened this issue May 2, 2021 · 1 comment
Open

Could not call other Google Map service #17

twuttiwat opened this issue May 2, 2021 · 1 comment

Comments

@twuttiwat
Copy link

Hi there,

I am trying to incorporate Fable.ReactGoogleMaps into my Safe Simplifed stack (Felize). I could show the map and add markers. But I could not do the following

  1. Call GeoCode service
  2. Call distance matrix service
  3. Call direction renderer

I think the problem is from Google Map js script is dynamically loaded which cause Fable compilation error. I fix that by follow original readme from Fable.Import google map which is:

    externals: {
        'google.maps': 'window.google.maps'
    },

Now the error is gone and I could sometime call other Google API service. But most of the time it show error that

can't access property "maps", window.google is undefined

I guess that my code trying to invoke actual google map library but the library is not loaded. Are there any ways to wait until the Google Map js library finished loading before I call service api. I did try to call function in OnIdle as follow:


let route() =
    printfn "route GetBounds %A" (mapRef.GetBounds())
    //()
    
    if (mapRef.GetBounds().IsSome) then
        let directionService = GoogleMaps.maps.DirectionsService.Create()
        let directionRenderer = GoogleMaps.maps.DirectionsRenderer.Create()
        let map:GoogleMaps.Map option = Some (GoogleMaps.maps :?> GoogleMaps.Map)
        directionRenderer.setMap(map)
        
        let origin = GoogleMaps.maps.LatLng.Create(10.11111, 10.22222)
        let destination = GoogleMaps.maps.LatLng.Create(11.11111, 11.22222)
     
         
        let req:GoogleMaps.DirectionsRequest =
            { new GoogleMaps.DirectionsRequest with
                 member x.avoidFerries with get() = None and set v = () 
                 member x.avoidHighways with get() = None and set v = () 
                 member x.avoidTolls with get() = None and set v = () 
                 member x.destination with get() = Some !^destination and set v = ()
                 member x.durationInTraffic with get() = None and set v = () 
                 member x.drivingOptions with get() = None and set v = () 
                 member x.optimizeWaypoints with get() = None and set v = () 
                 member x.origin with get() = Some !^original and set v = () 
                 member x.provideRouteAlternatives with get() = None and set v = () 
                 member x.region with get() = None and set v = ()
                 member x.transitOptions with get() = None and set v = () 
                 member x.travelMode with get() = Some ("DRIVING" :> obj) and set v = () 
                 member x.unitSystem with get() = None and set v = () 
                 member x.waypoints with get() = None and set v = () }
        
        directionService.route(req, fun resp status -> printfn "route %A" resp.routes.Count; 
                                                        directionRenderer.setDirections(resp))
    else
        printfn "no bounds"
        ()   

let onIdle() =
    if mapRef.GetBounds().IsSome then
        route ()
    else
        printfn "no bounds

let map' center markers =
    let googleMapApiKey = Config.variable "GOOGLE_MAP_API_KEY" 
    googleMap [
        MapProperties.ApiKey googleMapApiKey
        MapProperties.MapLoadingContainer "maploadercontainer"
        MapProperties.MapContainer "mapcontainer"
        MapProperties.DefaultZoom 12 
        MapProperties.DefaultCenter !^ center 
        MapProperties.Center !^ center 
        MapProperties.Markers markers
        MapProperties.OnMapMounted onMapMounted
        MapProperties.OnIdle onIdle
    ]

The direction count return correctly. The problem is that error happens sometimes to time. if I comment and uncomment the code several times, it will run.
Any help will be really appreciated. I am sorry to post questions here. (not sure that it is an issue or not) But I try to post this question in f# slack but still got no complete answer yet. So I decide to ask here where the library created.

Thank you very much,
TW

@nojaf
Copy link
Member

nojaf commented May 3, 2021

Hey, this message won't be of much helps but I eventually moved to mapbox instead of google maps exactly for this problem.
The whole dynamic loading of googleMaps really bit me on numerous occasions.
Sometimes just restarting webpack seems to solve things.

Perhaps you can add some extra checks to verify that googleMaps exists.
Something like

open Fable.Core.JsInterop

let isLoading () : bool = emitJsExpr () "window.google && window.google.maps"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants