---
title: "Geolokasi dengan GeoIP2"
version: 1.11
locale: id
source: https://docs.djangoproject.com/id/1.11/ref/contrib/gis/geoip2/
canonical: https://djangodocs.dev/id/1.11/ref/contrib/gis/geoip2/
---
# Geolokasi dengan GeoIP2

Obyek  [`GeoIP2`](#django.contrib.gis.geoip2.GeoIP2) dibugnkus untuk [MaxMind geoip2 Python library](https://geoip2.readthedocs.io/). [^1]

In order to perform IP-based geolocation, the [`GeoIP2`](#django.contrib.gis.geoip2.GeoIP2) object requires
the [geoip2 Python library](https://pypi.python.org/pypi/geoip2) and the GeoIP Country and/or City [datasets
in binary format](http://dev.maxmind.com/geoip/geoip2/geolite2/) (the CSV files will not work!). Grab the
`GeoLite2-Country.mmdb.gz` and `GeoLite2-City.mmdb.gz` files and unzip them
in a directory corresponding to the [`GEOIP_PATH`](#std-setting-GEOIP_PATH) setting.

Additionally, it is recommended to install the [libmaxminddb C library](https://github.com/maxmind/libmaxminddb), so
that `geoip2` can leverage the C library's faster speed.

## Contoh

Ini adalah sebuah contoh penggunaannya:

```
>>> from django.contrib.gis.geoip2 import GeoIP2
>>> g = GeoIP2()
>>> g.country('google.com')
{'country_code': 'US', 'country_name': 'United States'}
>>> g.city('72.14.207.99')
{'city': 'Mountain View',
'country_code': 'US',
'country_name': 'United States',
'dma_code': 807,
'latitude': 37.419200897216797,
'longitude': -122.05740356445312,
'postal_code': '94043',
'region': 'CA'}
>>> g.lat_lon('salon.com')
(39.0437, -77.4875)
>>> g.lon_lat('uh.edu')
(-95.4342, 29.834)
>>> g.geos('24.124.1.80').wkt
'POINT (-97 38)'
```

## Acuan API

#### `class GeoIP2(path=None, cache=0, country=None, city=None)`

Obyek `GeoIP` tidak membutuhkan parameter apapun untuk menggunakan pengaturan awalan. Bagaimanapun, pada pengaturan [`GEOIP_PATH`](#std-setting-GEOIP_PATH)  sangat sedikit harus disetel dengan jalur dari tempat dari kumpulan data GeoIP anda. Kata kunci inisialisasi berikut mungkin digunakan untuk menyesuaikan apapun dari awalan.

| Argumen Katakunci | Deskripsi |
| --- | --- |
| `path` | Base directory to where GeoIP data is located or the<br>full path to where the city or country data files<br>(`.mmdb`) are located. Assumes that both the city and<br>country datasets are located in this directory;<br>overrides the [`GEOIP_PATH`](#std-setting-GEOIP_PATH) setting. |
| `cache` | The cache settings when opening up the GeoIP datasets. May<br>be an integer in (0, 1, 2, 4, 8) corresponding to the<br>`MODE_AUTO`, `MODE_MMAP_EXT`, `MODE_MMAP`, and<br>`GEOIP_INDEX_CACHE` `MODE_MEMORY` C API settings,<br>respectively. Defaults to 0 (`MODE_AUTO`). |
| `country` | The name of the GeoIP country data file. Defaults<br>to `GeoLite2-Country.mmdb`. Setting this keyword<br>overrides the [`GEOIP_COUNTRY`](#std-setting-GEOIP_COUNTRY) setting. |
| `city` | The name of the GeoIP city data file. Defaults to<br>`GeoLite2-City.mmdb`. Setting this keyword overrides<br>the [`GEOIP_CITY`](#std-setting-GEOIP_CITY) setting. |

## Cara

### Instantiating

#### `classmethod GeoIP2.open(path, cache)`

This classmethod instantiates the GeoIP object from the given database path
and given cache setting.

### Meminta

All the following querying routines may take either a string IP address
or a fully qualified domain name (FQDN). For example, both
`'205.186.163.125'` and `'djangoproject.com'` would be valid query
parameters.

#### `GeoIP2.city(query)`

Returns a dictionary of city information for the given query. Some
of the values in the dictionary may be undefined (`None`).

#### `GeoIP2.country(query)`

Returns a dictionary with the country code and country for the given
query.

#### `GeoIP2.country_code(query)`

Mengembalikan kode negara terkait pada permintaan

#### `GeoIP2.country_name(query)`

Mengembalikan nama negara terkait pada permintaan

### Pengambilan Kordinat

#### `GeoIP2.coords(query)`

Mengembalikan sebuah tuple kordinat dari (longitude, latitude).

#### `GeoIP2.lon_lat(query)`

Mengembalikan sebuah tuple kordinat dari (longitude, latitude).

#### `GeoIP2.lat_lon(query)`

Mengembalikan sebuah tuple kordinat dari (latitude, longitude),

#### `GeoIP2.geos(query)`

Mengembalikan obyek [`Point`](/id/1.11/ref/contrib/gis/geos/#django.contrib.gis.geos.Point) terkait pada permintaan

## Pengaturan

### `GEOIP_PATH`

Sebuah string menentukan direktori dimana berkas data GeoIP ditempatkan. Pengaturan ini adalah *diwajibkan* meskipun secara manual ditentukan dengan kata kunci `path` ketika menginisialisasi obyek [`GeoIP2`](#django.contrib.gis.geoip2.GeoIP2).

### `GEOIP_COUNTRY`

Namadasar untuk digunakan berkas data negara GeoIP. Nilai awal `'GeoLite2-Country.mmdb'`.

### `GEOIP_CITY`

Nama dasar digunakan untuk berkas data kota GeoIP. Awalan pada `'GeoLite2-City.mmdb'`.

## Pengecualian

#### `exception GeoIP2Exception`

The exception raised when an error occurs in a call to the underlying
`geoip2` library.

**Catatan kaki**

[^1]: GeoIP(R) adalah merek dagang terdaftar dari MaxMind, Inc.
