Search code examples
google-mapsgoogle-app-maker

Address Search for the Google Map widget on App Maker


I see the Google Map widget documentation for App Maker says I can bind a datasource to it so that user can input an address to update the map. Is it possible to bind it to something like a Places search box in App Maker?

It'd be great if I don't need to create my own datasource to enable an address search.


Solution

  • You can build Address Search using Suggest Box, Calculated Datasource and Geocoder Apps Script service

    /**
     * Creates address suggestion record.
     * @param {Object} geocode Geocode object received from Maps Geocoder.
     * @return {AddressSuggestion} Created suggestion record.
     */
    function createAddressSuggestion_(geocode) {
      var record = app.models.AddressSuggestion.newRecord();
      record.Address = geocode.formatted_address;
      return record;
    }
    
    
    /**
     * Gets address suggestions Maps Geocoder.
     * @param {string} term Start of address string to search for.
     * @return {Array<AddressSuggestion>} Array of suggestion records.
     */
    function getAddressSuggestions_(term) {
      // TODO: Uncomment to set clientId and Google Maps API Key
      // Maps.setAuthentication('your_client_id', 'your_signing_key');
    
      var response = Maps.newGeocoder().geocode(term);
      var geocodes = response.results;
    
      return geocodes.map(createAddressSuggestion_);
    }
    

    Datasource

    One more important important thing: you need to say Suggest Box to handle your datasource as whole records otherwise it will filter out all results that have no exact match with your _startsWith term: Record as value