You are here

class GoogleStaticMaps in Geolocation Field 8.3

Same name and namespace in other branches
  1. 8.2 modules/geolocation_google_maps/modules/geolocation_google_static_maps/src/Plugin/geolocation/MapProvider/GoogleStaticMaps.php \Drupal\geolocation_google_static_maps\Plugin\geolocation\MapProvider\GoogleStaticMaps

Provides Google Maps.

Plugin annotation


@MapProvider(
  id = "google_static_maps",
  name = @Translation("Google Static Maps"),
  description = @Translation("You do require an API key for this plugin to work."),
)

Hierarchy

Expanded class hierarchy of GoogleStaticMaps

File

modules/geolocation_google_maps/modules/geolocation_google_static_maps/src/Plugin/geolocation/MapProvider/GoogleStaticMaps.php, line 18

Namespace

Drupal\geolocation_google_static_maps\Plugin\geolocation\MapProvider
View source
class GoogleStaticMaps extends GoogleMapsProviderBase {

  /**
   * {@inheritdoc}
   */
  public static $googleMapsApiUrlPath = '/maps/api/staticmap';

  /**
   * {@inheritdoc}
   */
  public static function getDefaultSettings() {
    return array_replace_recursive(parent::getDefaultSettings(), [
      'height' => '400',
      'width' => '400',
      'scale' => '1',
      'format' => 'png',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array $settings, array $parents = []) {
    $form = parent::getSettingsForm($settings, $parents);
    $parents_string = '';
    if ($parents) {
      $parents_string = implode('][', $parents) . '][';
    }
    $form['width'] = array_replace($form['width'], [
      '#type' => 'number',
      '#description' => $this
        ->t('Enter width in pixels. Free users maximum 640.'),
      '#process' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'processGroup',
        ],
      ],
      '#pre_render' => [
        [
          '\\Drupal\\Core\\Render\\Element\\Number',
          'preRenderNumber',
        ],
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'preRenderGroup',
        ],
      ],
    ]);
    $form['height'] = array_replace($form['height'], [
      '#type' => 'number',
      '#description' => $this
        ->t('Enter height in pixels. Free users maximum 640.'),
      '#process' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'processGroup',
        ],
      ],
      '#pre_render' => [
        [
          '\\Drupal\\Core\\Render\\Element\\Number',
          'preRenderNumber',
        ],
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'preRenderGroup',
        ],
      ],
    ]);
    $form['scale'] = [
      '#group' => $parents_string . 'general_settings',
      '#type' => 'select',
      '#title' => $this
        ->t('Scale Value'),
      '#options' => [
        '1' => $this
          ->t('1 (default)'),
        '2' => $this
          ->t('2'),
        '4' => $this
          ->t('4 - Google Maps APIs Premium Plan only'),
      ],
      '#default_value' => $settings['scale'],
      '#process' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'processGroup',
        ],
        [
          '\\Drupal\\Core\\Render\\Element\\Select',
          'processSelect',
        ],
      ],
      '#pre_render' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'preRenderGroup',
        ],
      ],
    ];
    $form['format'] = [
      '#group' => $parents_string . 'general_settings',
      '#type' => 'select',
      '#title' => $this
        ->t('Image Format'),
      '#options' => [
        'png' => $this
          ->t('8-bit PNG (default)'),
        'png32' => $this
          ->t('32-bit PNG'),
        'gif' => $this
          ->t('GIF'),
        'jpg' => $this
          ->t('JPEG'),
        'jpg-baseline' => $this
          ->t('non-progressive JPEG'),
      ],
      '#default_value' => $settings['format'],
      '#process' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'processGroup',
        ],
        [
          '\\Drupal\\Core\\Render\\Element\\Select',
          'processSelect',
        ],
      ],
      '#pre_render' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'preRenderGroup',
        ],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function alterRenderArray(array $render_array, array $map_settings, array $context = []) {
    $additional_parameters = [
      'type' => strtolower($map_settings['type']),
      'size' => filter_var($map_settings['width'], FILTER_SANITIZE_NUMBER_INT) . 'x' . filter_var($map_settings['height'], FILTER_SANITIZE_NUMBER_INT),
      'zoom' => $map_settings['zoom'],
      'scale' => (int) $map_settings['scale'],
      'format' => $map_settings['format'],
    ];

    // 0,0 is the default behavior anyway, so just ignore it for fitlocations.
    if (!empty($render_array['#centre']['lat']) || !empty($render_array['#centre']['lng'])) {
      $additional_parameters['center'] = $render_array['#centre']['lat'] . ',' . $render_array['#centre']['lng'];
    }
    $static_map_url = $this
      ->getGoogleMapsApiUrl($additional_parameters);
    $locations = GeolocationMap::getLocations($render_array);
    foreach ($locations as $location) {
      $marker_string = '&markers=';
      if (!empty($location['#icon'])) {
        $marker_string .= 'icon:' . Url::fromRoute('<front>', [], [
          'absolute' => TRUE,
        ])
          ->toString() . $location['#icon'] . urlencode('|');
      }
      $marker_string .= $location['#coordinates']['lat'] . ',' . $location['#coordinates']['lng'];
      $static_map_url .= $marker_string;
    }
    return [
      '#markup' => '<img src="' . $static_map_url . '">',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
GoogleMapsProviderBase::$googleMapsApiUrlBase public static property Google maps url.
GoogleMapsProviderBase::$googleMapsApiUrlBaseChina public static property Google maps url from PR China.
GoogleMapsProviderBase::$hybrid public static property Google map style - Hybrid.
GoogleMapsProviderBase::$roadmap public static property Google map style - Roadmap.
GoogleMapsProviderBase::$satellite public static property Google map style - Satellite.
GoogleMapsProviderBase::$terrain public static property Google map style - Terrain.
GoogleMapsProviderBase::getGoogleMapsApiParameters public function Return all module and custom defined parameters. 1
GoogleMapsProviderBase::getGoogleMapsApiUrl public function Return the fully build URL to load Google Maps API.
GoogleMapsProviderBase::getMapTypes private function An array of all available map types.
GoogleMapsProviderBase::getSettings public function Provide map provider specific settings ready to handover to JS. Overrides MapProviderBase::getSettings 1
GoogleMapsProviderBase::getSettingsSummary public function Provide a summary array to use in field formatters. Overrides MapProviderBase::getSettingsSummary
GoogleStaticMaps::$googleMapsApiUrlPath public static property Google maps url from PR China. Overrides GoogleMapsProviderBase::$googleMapsApiUrlPath
GoogleStaticMaps::alterRenderArray public function Alter render array. Overrides MapProviderBase::alterRenderArray
GoogleStaticMaps::getDefaultSettings public static function Provide a populated settings array. Overrides GoogleMapsProviderBase::getDefaultSettings
GoogleStaticMaps::getSettingsForm public function Provide a generic map settings form array. Overrides GoogleMapsProviderBase::getSettingsForm
MapProviderBase::$mapFeatureManager protected property Map feature manager.
MapProviderBase::alterCommonMap public function Alter common map build array. Overrides MapProviderInterface::alterCommonMap 5
MapProviderBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
MapProviderBase::getControlPositions public static function Return available control positions. Overrides MapProviderInterface::getControlPositions 5
MapProviderBase::validateMapFeatureForms public function Validate form.
MapProviderBase::__construct public function Constructs a new GeocoderBase object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.