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.







