K
K
Kerm2019-08-29 16:02:45
JavaScript
Kerm, 2019-08-29 16:02:45

How to center the map using external controls?

A map is displayed on the page, I want to select cities from the drop-down list next to it and so that the map itself is centered on this city, how can I implement this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-08-29
@Kerm

We add an attribute containing the coordinates of the city to the select items:

<option value="[55.751244,37.618423]">Москва</option>

When choosing an item, we get the value, pull setCenter:
select.addEventListener('change', function() {
  map.setCenter(JSON.parse(this.value));
});

jsfiddle.net/mjdkchzs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question