Loading...
Loading...
Compare original and translation side by side
.setMap(map).setMap(map)const map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 37.7749, lng: -122.4194 },
zoom: 12,
mapTypeId: 'roadmap' // or 'satellite', 'hybrid', 'terrain'
});const map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 37.7749, lng: -122.4194 },
zoom: 12,
mapTypeId: 'roadmap' // or 'satellite', 'hybrid', 'terrain'
});mapboxgl.accessToken = 'YOUR_MAPBOX_TOKEN';
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v12', // or satellite-v9, outdoors-v12
center: [-122.4194, 37.7749], // [lng, lat] - note the order!
zoom: 12
});{lat, lng}[lng, lat]mapboxgl.accessToken = 'YOUR_MAPBOX_TOKEN';
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v12', // or satellite-v9, outdoors-v12
center: [-122.4194, 37.7749], // [lng, lat] - 注意顺序!
zoom: 12
});{lat, lng}[lng, lat]| Google Maps | Mapbox GL JS | Notes |
|---|---|---|
| | Coordinate order reversed |
| | Returns LngLat object |
| | Same behavior |
| | Same behavior |
| | Animated pan |
| | Different bound format |
| | Completely different approach |
| | Similar |
| Google Maps | Mapbox GL JS | 说明 |
|---|---|---|
| | 坐标顺序相反 |
| | 返回LngLat对象 |
| | 行为一致 |
| | 行为一致 |
| | 动画平移 |
| | 边界格式不同 |
| | 实现方式完全不同 |
| | 类似 |
| Google Maps | Mapbox GL JS | Notes |
|---|---|---|
| | Simpler syntax |
| | Event property name |
| | Different event names |
| | Different event names |
| | No direct equivalent |
| | Same |
| | Different name |
| Google Maps | Mapbox GL JS | 说明 |
|---|---|---|
| | 语法更简洁 |
| | 事件属性名称不同 |
| | 事件名称不同 |
| | 事件名称不同 |
| | 无直接对应事件 |
| | 一致 |
| | 名称不同 |
const marker = new google.maps.Marker({
position: { lat: 37.7749, lng: -122.4194 },
map: map,
title: 'San Francisco',
icon: 'custom-icon.png'
});
// Remove marker
marker.setMap(null);// Create marker
const marker = new mapboxgl.Marker()
.setLngLat([-122.4194, 37.7749])
.setPopup(new mapboxgl.Popup().setText('San Francisco'))
.addTo(map);
// Remove marker
marker.remove();const marker = new google.maps.Marker({
position: { lat: 37.7749, lng: -122.4194 },
map: map,
title: 'San Francisco',
icon: 'custom-icon.png'
});
// Remove marker
marker.setMap(null);// Create marker
const marker = new mapboxgl.Marker()
.setLngLat([-122.4194, 37.7749])
.setPopup(new mapboxgl.Popup().setText('San Francisco'))
.addTo(map);
// Remove marker
marker.remove();const markers = locations.map(loc =>
new google.maps.Marker({
position: { lat: loc.lat, lng: loc.lng },
map: map
})
);// Same object-oriented approach
const markers = locations.map(loc =>
new mapboxgl.Marker()
.setLngLat([loc.lng, loc.lat])
.addTo(map)
);// Add as GeoJSON source + layer (uses WebGL, not DOM)
map.addSource('points', {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: locations.map(loc => ({
type: 'Feature',
geometry: { type: 'Point', coordinates: [loc.lng, loc.lat] },
properties: { name: loc.name }
}))
}
});
map.addLayer({
id: 'points-layer',
type: 'circle', // or 'symbol' for icons
source: 'points',
paint: {
'circle-radius': 8,
'circle-color': '#ff0000'
}
});const markers = locations.map(loc =>
new google.maps.Marker({
position: { lat: loc.lat, lng: loc.lng },
map: map
})
);// Same object-oriented approach
const markers = locations.map(loc =>
new mapboxgl.Marker()
.setLngLat([loc.lng, loc.lat])
.addTo(map)
);// Add as GeoJSON source + layer (uses WebGL, not DOM)
map.addSource('points', {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: locations.map(loc => ({
type: 'Feature',
geometry: { type: 'Point', coordinates: [loc.lng, loc.lat] },
properties: { name: loc.name }
}))
}
});
map.addLayer({
id: 'points-layer',
type: 'circle', // or 'symbol' for icons
source: 'points',
paint: {
'circle-radius': 8,
'circle-color': '#ff0000'
}
});const infowindow = new google.maps.InfoWindow({
content: '<h3>Title</h3><p>Content</p>'
});
marker.addListener('click', () => {
infowindow.open(map, marker);
});const infowindow = new google.maps.InfoWindow({
content: '<h3>Title</h3><p>Content</p>'
});
marker.addListener('click', () => {
infowindow.open(map, marker);
});// Option 1: Attach to marker
const marker = new mapboxgl.Marker()
.setLngLat([-122.4194, 37.7749])
.setPopup(new mapboxgl.Popup().setHTML('<h3>Title</h3><p>Content</p>'))
.addTo(map);
// Option 2: On layer click (for data-driven markers)
map.on('click', 'points-layer', (e) => {
const coordinates = e.features[0].geometry.coordinates.slice();
const description = e.features[0].properties.description;
new mapboxgl.Popup()
.setLngLat(coordinates)
.setHTML(description)
.addTo(map);
});// Option 1: Attach to marker
const marker = new mapboxgl.Marker()
.setLngLat([-122.4194, 37.7749])
.setPopup(new mapboxgl.Popup().setHTML('<h3>Title</h3><p>Content</p>'))
.addTo(map);
// Option 2: On layer click (for data-driven markers)
map.on('click', 'points-layer', (e) => {
const coordinates = e.features[0].geometry.coordinates.slice();
const description = e.features[0].properties.description;
new mapboxgl.Popup()
.setLngLat(coordinates)
.setHTML(description)
.addTo(map);
});const polygon = new google.maps.Polygon({
paths: [
{ lat: 37.7749, lng: -122.4194 },
{ lat: 37.7849, lng: -122.4094 },
{ lat: 37.7649, lng: -122.4094 }
],
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map
});const polygon = new google.maps.Polygon({
paths: [
{ lat: 37.7749, lng: -122.4194 },
{ lat: 37.7849, lng: -122.4094 },
{ lat: 37.7649, lng: -122.4094 }
],
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map
});map.addSource('polygon', {
type: 'geojson',
data: {
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [[
[-122.4194, 37.7749],
[-122.4094, 37.7849],
[-122.4094, 37.7649],
[-122.4194, 37.7749] // Close the ring
]]
}
}
});
map.addLayer({
id: 'polygon-layer',
type: 'fill',
source: 'polygon',
paint: {
'fill-color': '#FF0000',
'fill-opacity': 0.35
}
});
// Add outline
map.addLayer({
id: 'polygon-outline',
type: 'line',
source: 'polygon',
paint: {
'line-color': '#FF0000',
'line-width': 2,
'line-opacity': 0.8
}
});map.addSource('polygon', {
type: 'geojson',
data: {
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [[
[-122.4194, 37.7749],
[-122.4094, 37.7849],
[-122.4094, 37.7649],
[-122.4194, 37.7749] // Close the ring
]]
}
}
});
map.addLayer({
id: 'polygon-layer',
type: 'fill',
source: 'polygon',
paint: {
'fill-color': '#FF0000',
'fill-opacity': 0.35
}
});
// Add outline
map.addLayer({
id: 'polygon-outline',
type: 'line',
source: 'polygon',
paint: {
'line-color': '#FF0000',
'line-width': 2,
'line-opacity': 0.8
}
});const line = new google.maps.Polyline({
path: [
{ lat: 37.7749, lng: -122.4194 },
{ lat: 37.7849, lng: -122.4094 }
],
strokeColor: '#0000FF',
strokeWeight: 3,
map: map
});const line = new google.maps.Polyline({
path: [
{ lat: 37.7749, lng: -122.4194 },
{ lat: 37.7849, lng: -122.4094 }
],
strokeColor: '#0000FF',
strokeWeight: 3,
map: map
});map.addSource('route', {
type: 'geojson',
data: {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [
[-122.4194, 37.7749],
[-122.4094, 37.7849]
]
}
}
});
map.addLayer({
id: 'route-layer',
type: 'line',
source: 'route',
paint: {
'line-color': '#0000FF',
'line-width': 3
}
});map.addSource('route', {
type: 'geojson',
data: {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [
[-122.4194, 37.7749],
[-122.4094, 37.7849]
]
}
}
});
map.addLayer({
id: 'route-layer',
type: 'line',
source: 'route',
paint: {
'line-color': '#0000FF',
'line-width': 3
}
});const marker = new google.maps.Marker({
position: { lat: 37.7749, lng: -122.4194 },
map: map,
icon: {
url: 'marker.png',
scaledSize: new google.maps.Size(32, 32)
}
});const marker = new google.maps.Marker({
position: { lat: 37.7749, lng: -122.4194 },
map: map,
icon: {
url: 'marker.png',
scaledSize: new google.maps.Size(32, 32)
}
});const el = document.createElement('div');
el.className = 'marker';
el.style.backgroundImage = 'url(marker.png)';
el.style.width = '32px';
el.style.height = '32px';
new mapboxgl.Marker(el)
.setLngLat([-122.4194, 37.7749])
.addTo(map);// Load image
map.loadImage('marker.png', (error, image) => {
if (error) throw error;
map.addImage('custom-marker', image);
map.addLayer({
id: 'markers',
type: 'symbol',
source: 'points',
layout: {
'icon-image': 'custom-marker',
'icon-size': 1
}
});
});const el = document.createElement('div');
el.className = 'marker';
el.style.backgroundImage = 'url(marker.png)';
el.style.width = '32px';
el.style.height = '32px';
new mapboxgl.Marker(el)
.setLngLat([-122.4194, 37.7749])
.addTo(map);// Load image
map.loadImage('marker.png', (error, image) => {
if (error) throw error;
map.addImage('custom-marker', image);
map.addLayer({
id: 'markers',
type: 'symbol',
source: 'points',
layout: {
'icon-image': 'custom-marker',
'icon-size': 1
}
});
});const geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: '1600 Amphitheatre Parkway' }, (results, status) => {
if (status === 'OK') {
map.setCenter(results[0].geometry.location);
}
});const geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: '1600 Amphitheatre Parkway' }, (results, status) => {
if (status === 'OK') {
map.setCenter(results[0].geometry.location);
}
});// Use Mapbox Geocoding API v6
fetch(`https://api.mapbox.com/search/geocode/v6/forward?q=1600+Amphitheatre+Parkway&access_token=${mapboxgl.accessToken}`)
.then(response => response.json())
.then(data => {
const [lng, lat] = data.features[0].geometry.coordinates;
map.setCenter([lng, lat]);
});
// Or use mapbox-gl-geocoder plugin
const geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl
});
map.addControl(geocoder);// Use Mapbox Geocoding API v6
fetch(`https://api.mapbox.com/search/geocode/v6/forward?q=1600+Amphitheatre+Parkway&access_token=${mapboxgl.accessToken}`)
.then(response => response.json())
.then(data => {
const [lng, lat] = data.features[0].geometry.coordinates;
map.setCenter([lng, lat]);
});
// Or use mapbox-gl-geocoder plugin
const geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl
});
map.addControl(geocoder);const directionsService = new google.maps.DirectionsService();
const directionsRenderer = new google.maps.DirectionsRenderer();
directionsRenderer.setMap(map);
directionsService.route({
origin: 'San Francisco, CA',
destination: 'Los Angeles, CA',
travelMode: 'DRIVING'
}, (response, status) => {
if (status === 'OK') {
directionsRenderer.setDirections(response);
}
});const directionsService = new google.maps.DirectionsService();
const directionsRenderer = new google.maps.DirectionsRenderer();
directionsRenderer.setMap(map);
directionsService.route({
origin: 'San Francisco, CA',
destination: 'Los Angeles, CA',
travelMode: 'DRIVING'
}, (response, status) => {
if (status === 'OK') {
directionsRenderer.setDirections(response);
}
});// Use Mapbox Directions API
const origin = [-122.4194, 37.7749];
const destination = [-118.2437, 34.0522];
fetch(`https://api.mapbox.com/directions/v5/mapbox/driving/${origin.join(',')};${destination.join(',')}?geometries=geojson&access_token=${mapboxgl.accessToken}`)
.then(response => response.json())
.then(data => {
const route = data.routes[0].geometry;
map.addSource('route', {
type: 'geojson',
data: {
type: 'Feature',
geometry: route
}
});
map.addLayer({
id: 'route',
type: 'line',
source: 'route',
paint: {
'line-color': '#3887be',
'line-width': 5
}
});
});
// Or use @mapbox/mapbox-gl-directions plugin
const directions = new MapboxDirections({
accessToken: mapboxgl.accessToken
});
map.addControl(directions, 'top-left');// Use Mapbox Directions API
const origin = [-122.4194, 37.7749];
const destination = [-118.2437, 34.0522];
fetch(`https://api.mapbox.com/directions/v5/mapbox/driving/${origin.join(',')};${destination.join(',')}?geometries=geojson&access_token=${mapboxgl.accessToken}`)
.then(response => response.json())
.then(data => {
const route = data.routes[0].geometry;
map.addSource('route', {
type: 'geojson',
data: {
type: 'Feature',
geometry: route
}
});
map.addLayer({
id: 'route',
type: 'line',
source: 'route',
paint: {
'line-color': '#3887be',
'line-width': 5
}
});
});
// Or use @mapbox/mapbox-gl-directions plugin
const directions = new MapboxDirections({
accessToken: mapboxgl.accessToken
});
map.addControl(directions, 'top-left');// Controls are automatic, can configure:
map.setOptions({
zoomControl: true,
mapTypeControl: true,
streetViewControl: false,
fullscreenControl: true
});// Controls are automatic, can configure:
map.setOptions({
zoomControl: true,
mapTypeControl: true,
streetViewControl: false,
fullscreenControl: true
});// Add controls explicitly
map.addControl(new mapboxgl.NavigationControl()); // Zoom + rotation
map.addControl(new mapboxgl.FullscreenControl());
map.addControl(new mapboxgl.GeolocateControl());
map.addControl(new mapboxgl.ScaleControl());
// Position controls
map.addControl(new mapboxgl.NavigationControl(), 'top-right');// Add controls explicitly
map.addControl(new mapboxgl.NavigationControl()); // Zoom + rotation
map.addControl(new mapboxgl.FullscreenControl());
map.addControl(new mapboxgl.GeolocateControl());
map.addControl(new mapboxgl.ScaleControl());
// Position controls
map.addControl(new mapboxgl.NavigationControl(), 'top-right');// Requires MarkerClusterer library
import MarkerClusterer from '@googlemaps/markerclustererplus';
const markers = locations.map(loc =>
new google.maps.Marker({ position: loc, map: map })
);
new MarkerClusterer(map, markers, {
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});// Requires MarkerClusterer library
import MarkerClusterer from '@googlemaps/markerclustererplus';
const markers = locations.map(loc =>
new google.maps.Marker({ position: loc, map: map })
);
new MarkerClusterer(map, markers, {
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});// Built-in clustering support
map.addSource('points', {
type: 'geojson',
data: geojsonData,
cluster: true,
clusterMaxZoom: 14,
clusterRadius: 50
});
// Cluster circles
map.addLayer({
id: 'clusters',
type: 'circle',
source: 'points',
filter: ['has', 'point_count'],
paint: {
'circle-color': [
'step',
['get', 'point_count'],
'#51bbd6', 100,
'#f1f075', 750,
'#f28cb1'
],
'circle-radius': [
'step',
['get', 'point_count'],
20, 100,
30, 750,
40
]
}
});
// Cluster count labels
map.addLayer({
id: 'cluster-count',
type: 'symbol',
source: 'points',
filter: ['has', 'point_count'],
layout: {
'text-field': '{point_count_abbreviated}',
'text-size': 12
}
});
// Unclustered points
map.addLayer({
id: 'unclustered-point',
type: 'circle',
source: 'points',
filter: ['!', ['has', 'point_count']],
paint: {
'circle-color': '#11b4da',
'circle-radius': 8
}
});// Built-in clustering support
map.addSource('points', {
type: 'geojson',
data: geojsonData,
cluster: true,
clusterMaxZoom: 14,
clusterRadius: 50
});
// Cluster circles
map.addLayer({
id: 'clusters',
type: 'circle',
source: 'points',
filter: ['has', 'point_count'],
paint: {
'circle-color': [
'step',
['get', 'point_count'],
'#51bbd6', 100,
'#f1f075', 750,
'#f28cb1'
],
'circle-radius': [
'step',
['get', 'point_count'],
20, 100,
30, 750,
40
]
}
});
// Cluster count labels
map.addLayer({
id: 'cluster-count',
type: 'symbol',
source: 'points',
filter: ['has', 'point_count'],
layout: {
'text-field': '{point_count_abbreviated}',
'text-size': 12
}
});
// Unclustered points
map.addLayer({
id: 'unclustered-point',
type: 'circle',
source: 'points',
filter: ['!', ['has', 'point_count']],
paint: {
'circle-color': '#11b4da',
'circle-radius': 8
}
});stylesstylesconst styledMapType = new google.maps.StyledMapType([
{ elementType: 'geometry', stylers: [{ color: '#242f3e' }] },
{ elementType: 'labels.text.stroke', stylers: [{ color: '#242f3e' }] },
// ... many more rules
], { name: 'Dark' });
map.mapTypes.set('dark', styledMapType);
map.setMapTypeId('dark');// Use pre-built style
map.setStyle('mapbox://styles/mapbox/dark-v11');
// Or create custom style in Mapbox Studio and reference it
map.setStyle('mapbox://styles/yourusername/your-style-id');
// Modify classic styles programmatically
map.setPaintProperty('water', 'fill-color', '#242f3e');const styledMapType = new google.maps.StyledMapType([
{ elementType: 'geometry', stylers: [{ color: '#242f3e' }] },
{ elementType: 'labels.text.stroke', stylers: [{ color: '#242f3e' }] },
// ... many more rules
], { name: 'Dark' });
map.mapTypes.set('dark', styledMapType);
map.setMapTypeId('dark');// Use pre-built style
map.setStyle('mapbox://styles/mapbox/dark-v11');
// Or create custom style in Mapbox Studio and reference it
map.setStyle('mapbox://styles/yourusername/your-style-id');
// Modify classic styles programmatically
map.setPaintProperty('water', 'fill-color', '#242f3e');// Update marker position
marker.setPosition({ lat: 37.7849, lng: -122.4094 });
// Update polygon path
polygon.setPath(newCoordinates);// Update marker position
marker.setPosition({ lat: 37.7849, lng: -122.4094 });
// Update polygon path
polygon.setPath(newCoordinates);// Update source data
map.getSource('points').setData(newGeojsonData);
// Or update specific features
const source = map.getSource('points');
const data = source._data;
data.features[0].geometry.coordinates = [-122.4094, 37.7849];
source.setData(data);// Update source data
map.getSource('points').setData(newGeojsonData);
// Or update specific features
const source = map.getSource('points');
const data = source._data;
data.features[0].geometry.coordinates = [-122.4094, 37.7849];
source.setData(data);const heatmap = new google.maps.visualization.HeatmapLayer({
data: points,
map: map
});map.addLayer({
id: 'heatmap',
type: 'heatmap',
source: 'points',
paint: {
'heatmap-intensity': 1,
'heatmap-radius': 50,
'heatmap-color': [
'interpolate',
['linear'],
['heatmap-density'],
0, 'rgba(0,0,255,0)',
0.5, 'lime',
1, 'red'
]
}
});const heatmap = new google.maps.visualization.HeatmapLayer({
data: points,
map: map
});map.addLayer({
id: 'heatmap',
type: 'heatmap',
source: 'points',
paint: {
'heatmap-intensity': 1,
'heatmap-radius': 50,
'heatmap-color': [
'interpolate',
['linear'],
['heatmap-density'],
0, 'rgba(0,0,255,0)',
0.5, 'lime',
1, 'red'
]
}
});<!-- Replace Google Maps script -->
<script src="https://api.mapbox.com/mapbox-gl-js/v3.18.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v3.18.1/mapbox-gl.css" rel="stylesheet"><!-- Replace Google Maps script -->
<script src="https://api.mapbox.com/mapbox-gl-js/v3.18.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v3.18.1/mapbox-gl.css" rel="stylesheet">new google.maps.Map()new mapboxgl.Map()new mapboxgl.Map()new google.maps.Map()google.maps.event.addListener()map.on()latLnglngLatgoogle.maps.event.addListener()map.on()latLnglngLat// Google Maps
{ lat: 37.7749, lng: -122.4194 }
// Mapbox (REVERSED!)
[-122.4194, 37.7749]// Google Maps
{ lat: 37.7749, lng: -122.4194 }
// Mapbox (反转!)
[-122.4194, 37.7749]// Google Maps
map.on('click', (e) => {
console.log(e.latLng.lat(), e.latLng.lng());
});
// Mapbox
map.on('click', (e) => {
console.log(e.lngLat.lat, e.lngLat.lng);
});// Google Maps
map.on('click', (e) => {
console.log(e.latLng.lat(), e.latLng.lng());
});
// Mapbox
map.on('click', (e) => {
console.log(e.lngLat.lat, e.lngLat.lng);
});// Google Maps - immediate
const marker = new google.maps.Marker({ map: map });
// Mapbox - wait for load
map.on('load', () => {
map.addSource(...);
map.addLayer(...);
});// Google Maps - 立即执行
const marker = new google.maps.Marker({ map: map });
// Mapbox - 等待加载完成
map.on('load', () => {
map.addSource(...);
map.addLayer(...);
});// Google Maps
marker.setMap(null);
// Mapbox - must remove both
map.removeLayer('layer-id');
map.removeSource('source-id');// Google Maps
marker.setMap(null);
// Mapbox - 必须同时移除两者
map.removeLayer('layer-id');
map.removeSource('source-id');| Service | Google Maps | Mapbox | Notes |
|---|---|---|---|
| Geocoding | Geocoding API | Geocoding API | Similar capabilities |
| Reverse Geocoding | ✅ | ✅ | Similar |
| Directions | Directions API | Directions API | Mapbox has traffic-aware routing |
| Distance Matrix | Distance Matrix API | Matrix API | Similar |
| Isochrones | ❌ | ✅ | Mapbox exclusive |
| Optimization | ❌ | ✅ | Mapbox exclusive (TSP) |
| Street View | ✅ | ❌ | Google exclusive |
| Static Maps | ✅ | ✅ | Both supported |
| Satellite Imagery | ✅ | ✅ | Both supported |
| Tilesets | Limited | Full API | Mapbox more flexible |
| 服务 | Google Maps | Mapbox | 说明 |
|---|---|---|---|
| 地理编码 | Geocoding API | Geocoding API | 功能类似 |
| 逆地理编码 | ✅ | ✅ | 类似 |
| 路线规划 | Directions API | Directions API | Mapbox支持基于交通状况的路线规划 |
| 距离矩阵 | Distance Matrix API | Matrix API | 类似 |
| 等时线 | ❌ | ✅ | Mapbox独有 |
| 路径优化 | ❌ | ✅ | Mapbox独有(旅行商问题) |
| 街景 | ✅ | ❌ | Google独有 |
| 静态地图 | ✅ | ✅ | 均支持 |
| 卫星影像 | ✅ | ✅ | 均支持 |
| 瓦片集 | 有限 | 完整API | Mapbox灵活性更高 |
| Google Maps Plugin | Mapbox Alternative |
|---|---|
| MarkerClusterer | Built-in clustering |
| Drawing Manager | @mapbox/mapbox-gl-draw |
| Geocoder | @mapbox/mapbox-gl-geocoder |
| Directions | @mapbox/mapbox-gl-directions |
| - | @mapbox/mapbox-gl-traffic |
| - | @mapbox/mapbox-gl-compare |
| Google Maps插件 | Mapbox替代方案 |
|---|---|
| MarkerClusterer | 内置聚合功能 |
| Drawing Manager | @mapbox/mapbox-gl-draw |
| Geocoder | @mapbox/mapbox-gl-geocoder |
| Directions | @mapbox/mapbox-gl-directions |
| - | @mapbox/mapbox-gl-traffic |
| - | @mapbox/mapbox-gl-compare |
import { GoogleMap, Marker } from '@react-google-maps/api';import Map, { Marker } from 'react-map-gl';
// or
import { useMap } from '@mapbox/mapbox-gl-react';import { GoogleMap, Marker } from '@react-google-maps/api';import Map, { Marker } from 'react-map-gl';
// or
import { useMap } from '@mapbox/mapbox-gl-react';import { GoogleMap } from 'vue3-google-map';import { MglMap } from 'vue-mapbox';mapbox-web-integration-patternsimport { GoogleMap } from 'vue3-google-map';import { MglMap } from 'vue-mapbox';mapbox-web-integration-patterns// Mock mapboxgl
jest.mock('mapbox-gl', () => ({
Map: jest.fn(() => ({
on: jest.fn(),
addSource: jest.fn(),
addLayer: jest.fn()
})),
Marker: jest.fn()
}));// Mock mapboxgl
jest.mock('mapbox-gl', () => ({
Map: jest.fn(() => ({
on: jest.fn(),
addSource: jest.fn(),
addLayer: jest.fn()
})),
Marker: jest.fn()
}));// GOOGLE MAPS
const map = new google.maps.Map(el, {
center: { lat: 37.7749, lng: -122.4194 },
zoom: 12
});
const marker = new google.maps.Marker({
position: { lat: 37.7749, lng: -122.4194 },
map: map
});
google.maps.event.addListener(map, 'click', (e) => {
console.log(e.latLng.lat(), e.latLng.lng());
});
// MAPBOX GL JS
mapboxgl.accessToken = 'YOUR_TOKEN';
const map = new mapboxgl.Map({
container: el,
center: [-122.4194, 37.7749], // REVERSED!
zoom: 12,
style: 'mapbox://styles/mapbox/streets-v12'
});
const marker = new mapboxgl.Marker()
.setLngLat([-122.4194, 37.7749]) // REVERSED!
.addTo(map);
map.on('click', (e) => {
console.log(e.lngLat.lat, e.lngLat.lng);
});// GOOGLE MAPS
const map = new google.maps.Map(el, {
center: { lat: 37.7749, lng: -122.4194 },
zoom: 12
});
const marker = new google.maps.Marker({
position: { lat: 37.7749, lng: -122.4194 },
map: map
});
google.maps.event.addListener(map, 'click', (e) => {
console.log(e.latLng.lat(), e.latLng.lng());
});
// MAPBOX GL JS
mapboxgl.accessToken = 'YOUR_TOKEN';
const map = new mapboxgl.Map({
container: el,
center: [-122.4194, 37.7749], // 反转!
zoom: 12,
style: 'mapbox://styles/mapbox/streets-v12'
});
const marker = new mapboxgl.Marker()
.setLngLat([-122.4194, 37.7749]) // 反转!
.addTo(map);
map.on('click', (e) => {
console.log(e.lngLat.lat, e.lngLat.lng);
});