1. Fabrik 3.8.1 has been released. It is mostly bug fixes and feature enhancements, but does include two new plugins (push notifications, and the sequence element). As usual we strongly recommend testing the new release on a sandbox if your application is mission critical, and always do an Akeeba backup before updating.
    Dismiss Notice

google map element validation (coordinates)

Discussion in 'Standard Support' started by rtorres, May 17, 2018.

  1. rtorres

    rtorres Member

    Level: Community
    has anyone tryed to validate if the coordinates are IN or OUT of a series of points ? I need to validate that the user moved the indicator

    my first attemp was using the value of the variable that holds the coordinates, but didn work.. Ive already search on the forums but seems like no one has needed to implement this

    any ideas?
     
  2. troester

    troester Administrator Staff Member

    Level: Community
  3. rtorres

    rtorres Member

    Level: Community
  4. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    I actually did something very similar recently. You can do the following in a PHP validation ...

    Code (Text):

    // bust the map element data into coords
    $coords = FabrikString::mapStrToCoords($data);
    // get the maps API key
    $config = JComponentHelper::getParams('com_fabrik');
    $apiKey     = $config->get('google_api_key', '');
    // build the reverse geocode api url
    $url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' . $coords->lat . ',' . $coords->long . '&sensor=false&key=' . $apiKey;
    // make the HTTP request
    $data = @file_get_contents($url);

    // if you are doing this from a local host without certs, you may need to comment out the previous line and uncomment these lines
    //$aContext = array(
    //    'ssl' => array(
    //        'verify_peer' => false,
    //    ),
    //);
    //$cxContext = stream_context_create($aContext);
    //$data = @file_get_contents($url, false, $cxContext);

    // parse the json response
    $data = json_decode($data,true);
    // loop around returned results, return false if we find a country code that isn't 'US'
    foreach ($data['results'] as $result) {
      foreach ($result['address_components'] as $component) {
        foreach ($component['types'] as $type) {
          if ($type === 'country') {
            if ($component['short_name'] !== 'US') {
               return false;
            }
          }
        }
      }
    }
    // we got this far, so ... woo hoo ...
    return true;
     
    The above code should work, although it could use a little error checking on the returned response.

    Replace 'US' with your desired short country code.

    NOTE - you'll need to have your Google maps API key configured in Fabrik's global settings, and enable the reverse geocode API in your Google dashboard.

    -- hugh
     

Share This Page