// это массив ваших "точек"
const locations = [
{ lat: ..., lng: ... },
{ lat: ..., lng: ... },
...
];
const onLoad = map => {
const bounds = new window.google.maps.LatLngBounds();
locations.forEach(n => bounds.extend(n));
map.fitBounds(bounds);
};
<GoogleMap
onLoad={onLoad}
...
>
{locations.map(n => <Marker position={n} />)}
</GoogleMap>
<gmap-map
ref="map"
...
const bounds = new google.maps.LatLngBounds();
массивКоординатВашихЛокаций.forEach(n => bounds.extend(n));
this.$refs.map.fitBounds(bounds);
:style="mapStyles"
data() { return { mapStyles: { styles: [
mapStyles: {
на options: {
:style="mapStyles"
на :options="options"
const markersData = [
{ position: { lat: ..., lng: ... }, content: '...' },
{ position: { lat: ..., lng: ... }, content: '...' },
...
];
const infoWindow = new google.maps.InfoWindow();
const markers = markersData.map(({ position, content }) => {
const marker = new google.maps.Marker({ position, map });
marker.addListener('click', () => {
infoWindow.setContent(content);
infoWindow.open(map, marker);
});
return marker;
});
null
):const setActiveIcon = marker => markers.forEach(n => n.setIcon(n === marker ? iconActive : icon));
function showDotsCountInPolygon(e) {
const dotsCountInPolygon = dots
.filter(n => google.maps.geometry.poly.containsLocation(new google.maps.LatLng(n), this))
.length;
infoWindow.setContent(`Точек в полигоне: ${dotsCountInPolygon}`);
infoWindow.setPosition(e.latLng);
infoWindow.open(map);
}
polygon.addListener('click', showDotsCountInPolygon);
const markerData = [
{ coord: [ ... ], content: '...' },
{ coord: [ ... ], content: '...' },
...
];
markerData.forEach(function(n) {
const marker = new google.maps.Marker({
position: new google.maps.LatLng(...n.coord),
map,
});
marker.addListener('click', function() {
infowindow.setContent(n.content);
infowindow.open(map, marker);
});
});
function codeAddress(address, geocoder, map) {
geocoder.geocode({ address }, function(results, status) {
if (status === 'OK') {
new google.maps.Marker({
map: map,
position: results[0].geometry.location,
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
address.forEach(n => codeAddress(n, geocoder, map));
<gmap-info-window
:options="infoOptions"
:position="infoWindowPos"
:opened="infoWinOpen"
@closeclick="infoWinOpen = false"
>
{{ infoContent }}
</gmap-info-window>
onMarkerClick(e) {
this.infoWindowPos = e.latLng; // задаём положение окна, над кликнутым маркером
this.infoContent = JSON.stringify(e.latLng); // задаём контент окна, передаётся в слот
this.infoWinOpen = true; // открываем окно
},
<gmap-map
ref="map"
v-bind="options"
@click="onMapClick"
>
<gmap-marker
v-for="m in markers"
:key="m.id"
:position="m.position"
:clickable="true"
:draggable="true"
@click="onMarkerClick"
/>
</gmap-map>
data: () => ({
options: {
center: { lat: 45.101637, lng: 38.986345 },
zoom: 15,
},
markers: [],
}),
methods: {
onMapClick(e) {
this.markers.push({
id: 1 + Math.max(0, ...this.markers.map(n => n.id)),
position: e.latLng,
});
},
onMarkerClick(e) {
this.$refs.map.panTo(e.latLng);
// или
// this.options.center = e.latLng;
},
},
Не дублируя код инициализации
<div id="address-list"></div>
const addressList = document.querySelector('#address-list');
addressList.innerHTML = markersData.map((n, i) => `
<div class="address-list-item">
<span data-index="${i}">${n.name}</span>
</div>
`).join('');
addressList.addEventListener('click', e => {
const marker = markersData[e.target.dataset.index];
if (marker) {
map.setCenter(new google.maps.LatLng(marker.lat, marker.lng));
}
});
function onMarkerClick(e) {
map.setZoom(8);
map.panTo(e.latLng);
}
for (const f of features) {
const marker = new google.maps.Marker({
position: f.position,
icon: icons[f.type].icon,
map,
});
marker.addListener('click', onMarkerClick);
}
const info = new google.maps.InfoWindow();
marker.addListener('click', function() {
info.setContent('сюда засовываете свою информацию');
info.open(map, marker);
});
$('#btn').click(function() {
i = (i + 1) % arrLatLng.length;
map.setCenter(arrLatLng[i]);
});
const marker = new google.maps.Marker({
map: this.map,
title: place.name,
position: place.geometry.location,
});
google.maps.event.addListener(marker, 'click', () => {
this.infowindow.setContent(marker.title);
this.infowindow.open(this.map, marker);
});
this.markers.push(marker);
updateMap() {
if (this.polyline) {
this.polyline.setMap(null);
}
this.polyline = new google.maps.Polyline({
path: this.markers.map(n => n.getPosition()),
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2,
map: this.map,
});
},
document.getElementById('map') <...> document.getElementById('inputPlace')