Center the map on a clicked symbol

Use events and flyTo to center the map on a symbol.

NPM module setup


npm install --save @maptiler/sdk
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
    container: 'map',
    style: MapStyle.STREETS,
    center: [-90.96, -0.47],
    zoom: 7.5
});

map.on('load', async function () {
    // Add an image to use as a custom marker
    const image = await map.loadImage(
        'https://docs.maptiler.com/sdk-js/assets/custom_marker.png');
    map.addImage('custom-marker', image.data);
    // Add a GeoJSON source with 3 points.
    map.addSource('points', {
        'type': 'geojson',
        'data': {
            'type': 'FeatureCollection',
            'features': [
                {
                    'type': 'Feature',
                    'properties': {},
                    'geometry': {
                        'type': 'Point',
                        'coordinates': [
                            -91.395263671875,
                            -0.9145729757782163
                        ]
                    }
                },
                {
                    'type': 'Feature',
                    'properties': {},
                    'geometry': {
                        'type': 'Point',
                        'coordinates': [
                            -90.32958984375,
                            -0.6344474832838974
                        ]
                    }
                },
                {
                    'type': 'Feature',
                    'properties': {},
                    'geometry': {
                        'type': 'Point',
                        'coordinates': [
                            -91.34033203125,
                            0.01647949196029245
                        ]
                    }
                }
            ]
        }
    });

    // Add a symbol layer
    map.addLayer({
        'id': 'symbols',
        'type': 'symbol',
        'source': 'points',
        'layout': {
            'icon-image': 'custom-marker'
        }
    });

    // Center the map on the coordinates of any clicked symbol from the 'symbols' layer.
    map.on('click', 'symbols', function (e) {
        map.flyTo({
            center: e.features[0].geometry.coordinates
        });
    });

    // Change the cursor to a pointer when the it enters a feature in the 'symbols' layer.
    map.on('mouseenter', 'symbols', function () {
        map.getCanvas().style.cursor = 'pointer';
    });

    // Change it back to a pointer when it leaves.
    map.on('mouseleave', 'symbols', function () {
        map.getCanvas().style.cursor = '';
    });
});
Display a popup on click

Display a popup on click

Examples

Display a popup when a user clicks a symbol.

Create a story map, fly to a location based on the scroll position

Fly to a location based on the scroll position

Examples

Fly to locations on the map by scrolling.

Fit to the bounds of a lineString

Fit to the bounds of a lineString

Examples

Get the bounds of a lineString.

Fly to a location

Fly to a location

Examples

Smoothly interpolate between locations using the flyTo animation.

Was this helpful?