Tips Creating Interactive Maps with Mapbox GL JS

brownbear762

New member
[TIẾNG VIỆT]:
** Tạo bản đồ tương tác với Mapbox GL JS **

Mapbox GL JS là một thư viện JavaScript mạnh mẽ để tạo bản đồ tương tác.Thật dễ dàng để sử dụng, với một loạt các tính năng và khả năng.Trong bài viết này, chúng tôi sẽ chỉ cho bạn cách tạo bản đồ tương tác đơn giản với Mapbox GL JS.

## Bắt đầu

Để bắt đầu, bạn sẽ cần cài đặt thư viện Mapbox GL JS.Bạn có thể làm điều này bằng cách làm theo các hướng dẫn trên trang web [Mapbox GL JS] (https://docs.mapbox.com/mapbox-l-js/).

Khi bạn đã cài đặt thư viện, bạn có thể tạo một bản đồ mới bằng cách tạo đối tượng `map`.Đối tượng `map` có một vài tham số, bao gồm cả phần tử` container` nơi bạn muốn hiển thị bản đồ và `style` của bản đồ.

`` `JS
const map = new mapboxgl.map ({
Container: 'Bản đồ',
Kiểu: 'Mapbox: // Styles/Mapbox/Street-V11'
});
`` `

## Thêm điểm đánh dấu

Để thêm các điểm đánh dấu vào bản đồ của bạn, bạn có thể sử dụng phương thức `addMarker ()`.Phương thức `addMarker ()` có một vài tham số, bao gồm các tọa độ `lat` và` lng` của điểm đánh dấu và `biểu tượng` của điểm đánh dấu.

`` `JS
map.addmarker ({
Lat: 40.7128,
LNG: -74,0060,
Biểu tượng: 'https://marker.mapbox.com/||40.7128.-74.0060|110x30'
});
`` `

## Thêm hình dạng

Bạn cũng có thể thêm hình dạng vào bản đồ của mình, chẳng hạn như đường, đa giác và vòng tròn.Để thêm một hình dạng, bạn có thể sử dụng phương thức `addShape ()`.Phương thức `addShape ()` có một vài tham số, bao gồm loại hình dạng, tọa độ của hình dạng và các thuộc tính của hình dạng.

`` `JS
map.addshape ({
loại: 'dòng',
tọa độ: [
[-74.0060, 40.7128],
[-73.9960, 40.7128],
[-73.9960, 40.7028],
[-74.0060, 40.7028]
],
của cải: {
màu đỏ',
Chiều rộng: 5
}
});
`` `

## Thêm tương tác

Bạn cũng có thể thêm các tương tác vào bản đồ của mình, chẳng hạn như chú giải công cụ, cửa sổ bật lên và kéo và thả.Để thêm một tương tác, bạn có thể sử dụng phương thức `addInteraction ()`.Phương thức `addInteraction ()` có một vài tham số, bao gồm loại tương tác, bộ chọn cho phần tử kích hoạt tương tác và hàm gọi lại cho tương tác.

`` `JS
map.AddInterAction ('Marker-click', {
bộ chọn: '.marker',
OnTrigger: function (e) {
// Nhận điểm đánh dấu đã được nhấp vào
const đánh dấu = e.target;

// Nhận tọa độ của điểm đánh dấu
const lat = marker.getlatlng (). lat;
const lng = mark.getlatlng (). lng;

// Hiển thị cửa sổ bật lên với tọa độ của điểm đánh dấu
const popup = new mapboxgl.popup ({
neo: 'dưới cùng bên trái',
Offset: [0, -15]
});

popup.sethtml (`
<p> Các tọa độ của điểm đánh dấu này là <b> $ {lat}, $ {lng} </b>. </p>
`);

đánh dấu.setPopup (bật lên);
}
});
`` `

## Phần kết luận

Trong bài viết này, chúng tôi đã chỉ cho bạn cách tạo một bản đồ tương tác đơn giản với Mapbox GL JS.Chúng tôi đã đề cập đến những điều cơ bản của việc tạo một bản đồ, thêm các điểm đánh dấu,

[ENGLISH]:
**Creating Interactive Maps with Mapbox GL JS**

Mapbox GL JS is a powerful JavaScript library for creating interactive maps. It's easy to use, with a wide range of features and capabilities. In this article, we'll show you how to create a simple interactive map with Mapbox GL JS.

## Getting Started

To get started, you'll need to install the Mapbox GL JS library. You can do this by following the instructions on the [Mapbox GL JS website](https://docs.mapbox.com/mapbox-gl-js/).

Once you've installed the library, you can create a new map by creating a `Map` object. The `Map` object takes a few parameters, including the `container` element where you want to render the map, and the `style` of the map.

```js
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11'
});
```

## Adding Markers

To add markers to your map, you can use the `addMarker()` method. The `addMarker()` method takes a few parameters, including the `lat` and `lng` coordinates of the marker, and the `icon` of the marker.

```js
map.addMarker({
lat: 40.7128,
lng: -74.0060,
icon: 'https://marker.mapbox.com/||40.7128,-74.0060|110x30'
});
```

## Adding Shapes

You can also add shapes to your map, such as lines, polygons, and circles. To add a shape, you can use the `addShape()` method. The `addShape()` method takes a few parameters, including the type of shape, the coordinates of the shape, and the properties of the shape.

```js
map.addShape({
type: 'line',
coordinates: [
[-74.0060, 40.7128],
[-73.9960, 40.7128],
[-73.9960, 40.7028],
[-74.0060, 40.7028]
],
properties: {
color: 'red',
width: 5
}
});
```

## Adding Interactions

You can also add interactions to your map, such as tooltips, popups, and drag and drop. To add an interaction, you can use the `addInteraction()` method. The `addInteraction()` method takes a few parameters, including the type of interaction, the selector for the element that triggers the interaction, and the callback function for the interaction.

```js
map.addInteraction('marker-click', {
selector: '.marker',
onTrigger: function(e) {
// Get the marker that was clicked
const marker = e.target;

// Get the coordinates of the marker
const lat = marker.getLatLng().lat;
const lng = marker.getLatLng().lng;

// Show a popup with the coordinates of the marker
const popup = new mapboxgl.Popup({
anchor: 'bottom-left',
offset: [0, -15]
});

popup.setHTML(`
<p>The coordinates of this marker are <b>${lat}, ${lng}</b>.</p>
`);

marker.setPopup(popup);
}
});
```

## Conclusion

In this article, we showed you how to create a simple interactive map with Mapbox GL JS. We covered the basics of creating a map, adding markers,
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top