Setting up geolocation in 1C-Bitrix

Our company is engaged in the development, support and maintenance of Bitrix and Bitrix24 solutions of any complexity. From simple one-page sites to complex online stores, CRM systems with 1C and telephony integration. The experience of developers is confirmed by certificates from the vendor.
Our competencies:
Development stages
Latest works
  • image_website-b2b-advance_0.png
    B2B ADVANCE company website development
    1189
  • image_bitrix-bitrix-24-1c_fixper_448_0.png
    Website development for FIXPER company
    813
  • image_bitrix-bitrix-24-1c_development_of_an_online_appointment_booking_widget_for_a_medical_center_594_0.webp
    Development based on Bitrix, Bitrix24, 1C for the company Development of an Online Appointment Booking Widget for a Medical Center
    564
  • image_bitrix-bitrix-24-1c_mirsanbel_458_0.webp
    Development based on 1C Enterprise for MIRSANBEL
    747
  • image_crm_dolbimby_434_0.webp
    Website development on CRM Bitrix24 for DOLBIMBY
    657
  • image_crm_technotorgcomplex_453_0.webp
    Development based on Bitrix24 for the company TECHNOTORGKOMPLEKS
    976

Setting up Geolocation on 1C-Bitrix

Geolocation in Bitrix is needed for two fundamentally different tasks: determine physical user location by IP (server-side) or get accurate GPS coordinates from their device (browser-side). Both tools are used together in e-commerce — first for preliminary city determination, second for "find nearest store".

Server-side region determination by IP

Bitrix doesn't include GeoIP database in distribution. Options:

MaxMind GeoIP2 — industry standard. Free GeoLite2-City database updates weekly, Russia accuracy — 85-90% at city level. Paid GeoIP2-City — more accurate. Installation: download .mmdb file, connect through composer require geoip2/geoip2, use in PHP.

Sypex Geo — Russian database, works better for CIS. Has PHP class without composer dependencies, convenient for old installations.

DaData API — cloud service, doesn't require local database. API request for each new IP. Suitable for low traffic.

Integration into Bitrix through init.php:

AddEventHandler("main", "OnPageStart", function() {
    if (!isset($_SESSION['USER_REGION'])) {
        $ip = $_SERVER['REMOTE_ADDR'];
        // Remove IPv6 mapped IPv4
        $ip = str_replace('::ffff:', '', $ip);

        $reader = new \GeoIp2\Database\Reader('/path/to/GeoLite2-City.mmdb');
        try {
            $record = $reader->city($ip);
            $_SESSION['USER_REGION'] = [
                'city' => $record->city->name,
                'city_ru' => $record->city->names['ru'] ?? '',
                'lat' => $record->location->latitude,
                'lng' => $record->location->longitude,
            ];
        } catch (\Exception $e) {
            $_SESSION['USER_REGION'] = ['city' => 'Moscow'];
        }
    }
});

Result is cached in session — don't make GeoIP request on every hit.

Integration with Bitrix Location module

sale module contains b_sale_location table with hierarchy: country → region → city. This is Bitrix internal reference for delivery calculation. When determining city by GeoIP, need to find corresponding b_sale_location entry:

$city = \Bitrix\Sale\Location\Search\Finder::find([
    'select' => ['ID', 'NAME.NAME', 'CODE'],
    'filter' => ['NAME.NAME' => $detectedCityName, 'NAME.LANGUAGE_ID' => 'ru'],
]);

If match found — save LOCATION_CODE to session/cookie. This code is used in component bitrix:sale.location.selector.system and in delivery cost calculation.

Browser geolocation: Geolocation API

HTML5 Geolocation API — for precise positioning. Requires user permission and works only over HTTPS:

navigator.geolocation.getCurrentPosition(
    function(position) {
        const lat = position.coords.latitude;
        const lng = position.coords.longitude;
        // Send coordinates to server
        fetch('/local/ajax/set_location.php', {
            method: 'POST',
            body: JSON.stringify({ lat, lng }),
            headers: { 'Content-Type': 'application/json' }
        });
    },
    function(error) {
        // User declined — use IP geolocation
    }
);

On server, do reverse geocoding by coordinates through Yandex Geocoder or Google Maps Geocoding API. Yandex Geocoder is preferable for Russian addresses.

City confirmation popup

Standard UX: determine city automatically, show popup "Your city — %city%? Yes / No, choose another". On "Yes" set cookie user_city with long TTL (30 days). On "No" — show city search.

In Bitrix component bitrix:sale.location.selector.system provides ready selector. But its visual design often requires template customization to match site design.