Backend\Modules\Location\Engine\Model::getCoordinates PHP Method

getCoordinates() public static method

Get coordinates latitude/longitude
public static getCoordinates ( string $street = null, string $streetNumber = null, string $city = null, string $zip = null, string $country = null ) : array
$street string
$streetNumber string
$city string
$zip string
$country string
return array Contains 'latitude' and 'longitude' as variables
    public static function getCoordinates($street = null, $streetNumber = null, $city = null, $zip = null, $country = null)
    {
        // init item
        $item = array();
        // building item
        if (!empty($street)) {
            $item[] = $street;
        }
        if (!empty($streetNumber)) {
            $item[] = $streetNumber;
        }
        if (!empty($city)) {
            $item[] = $city;
        }
        if (!empty($zip)) {
            $item[] = $zip;
        }
        if (!empty($country)) {
            $item[] = Intl::getRegionBundle()->getCountryName($country, BL::getInterfaceLanguage());
        }
        // define address
        $address = implode(' ', $item);
        // fetch the geo coordinates
        $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . rawurlencode($address);
        $geocodes = json_decode(file_get_contents($url), true);
        // return coordinates latitude/longitude
        return array('latitude' => array_key_exists(0, $geocodes['results']) ? $geocodes['results'][0]['geometry']['location']['lat'] : null, 'longitude' => array_key_exists(0, $geocodes['results']) ? $geocodes['results'][0]['geometry']['location']['lng'] : null);
    }

Usage Example

Example #1
0
 /**
  * Validate the form
  */
 private function validateForm()
 {
     if ($this->frm->isSubmitted()) {
         $this->frm->cleanupFields();
         // validate fields
         $this->frm->getField('title')->isFilled(BL::err('TitleIsRequired'));
         $this->frm->getField('street')->isFilled(BL::err('FieldIsRequired'));
         $this->frm->getField('number')->isFilled(BL::err('FieldIsRequired'));
         $this->frm->getField('zip')->isFilled(BL::err('FieldIsRequired'));
         $this->frm->getField('city')->isFilled(BL::err('FieldIsRequired'));
         if ($this->frm->isCorrect()) {
             // build item
             $item['language'] = BL::getWorkingLanguage();
             $item['title'] = $this->frm->getField('title')->getValue();
             $item['street'] = $this->frm->getField('street')->getValue();
             $item['number'] = $this->frm->getField('number')->getValue();
             $item['zip'] = $this->frm->getField('zip')->getValue();
             $item['city'] = $this->frm->getField('city')->getValue();
             $item['country'] = $this->frm->getField('country')->getValue();
             // define coordinates
             $coordinates = BackendLocationModel::getCoordinates($item['street'], $item['number'], $item['city'], $item['zip'], $item['country']);
             // define latitude and longitude
             $item['lat'] = $coordinates['latitude'];
             $item['lng'] = $coordinates['longitude'];
             // insert the item
             $item['id'] = BackendLocationModel::insert($item);
             // everything is saved, so redirect to the overview
             if ($item['lat'] && $item['lng']) {
                 // trigger event
                 BackendModel::triggerEvent($this->getModule(), 'after_add', array('item' => $item));
             }
             // redirect
             $this->redirect(BackendModel::createURLForAction('Edit') . '&id=' . $item['id'] . '&report=added&var=' . urlencode($item['title']));
         }
     }
 }
All Usage Examples Of Backend\Modules\Location\Engine\Model::getCoordinates