gpt4 book ai didi

arrays - 如何使用select过滤一个数组中的Google Maps标记?

转载 作者:行者123 更新时间:2023-12-04 16:27:21 24 4
gpt4 key购买 nike

我在我的网站中使用一个数组实现了Google map ,如下所示:

var gmarkers1 = [];
var markers1 = [];

markers1 = [
['0', 'Title', 52.4357808, 4.991315699999973],
['1', 'Title', 52.4357808, 4.991315699999973],
['2', 'Title', 52.4555687, 5.039231599999994],
];
在此示例中,有3个标记,但实际上还有更多标记。就我的目的而言,这是做到这一点的唯一方法(一个数组)。
问题:我希望能够使用选择框在此数组内进行过滤。因此,用户从一个选择中选择了“汽车”,而Google Maps仅显示带有属性(?)“汽车”的标记。
标记显示如下:
for (i = 0; i < markers1.length; i++) {
var category;
var pos = new google.maps.LatLng(markers1[i][2], markers1[i][3]);
var content = markers1[i][1];
bounds.extend(pos);
marker1 = new google.maps.Marker({
position: pos,
map: map,
icon: image1
});

gmarkers1.push(marker1);
// [START]-Event listener to center view and go to position
google.maps.event.addListener(marker1, 'click', (function(marker1, content) {
return function() {
console.log('Gmarker 1 gets pushed')
infowindow.setContent(content);
infowindow.open(map, marker1);
map.panTo(this.getPosition());
map.setZoom(15);
}
})(marker1, content));
}

最佳答案

您应该将类​​别添加为标记属性。

markers1 = [
['0', 'Title 1', 52.4357808, 4.991315699999973, 'car'],
['1', 'Title 2', 52.4357808, 4.981315699999973, 'third'],
['2', 'Title 3', 52.4555687, 5.039231599999994, 'car'],
['3', 'Title 4', 52.4555687, 5.029231599999994, 'second']
];

创建标记。标记是对象,因此将类别添加为属性。
var category = markers1[i][4];
var pos = new google.maps.LatLng(markers1[i][2], markers1[i][3]);
marker1 = new google.maps.Marker({
position: pos,
map: map,
category: category,
icon: image1
});

然后在选择更改时,调用函数检查类别是否与所选的相同。
/**
* Function to filter markers by category
*/

filterMarkers = function(category)
{
for (i = 0; i < gmarkers1.length; i++) {
marker = gmarkers1[i];

// If is same category or category not picked
if(marker.category == category || category.length == 0)
{
marker.setVisible(true);
}
// Categories don't match
else
{
marker.setVisible(false);
}
}
}

工作示例

var gmarkers1 = [];
var markers1 = [];
var infowindow = new google.maps.InfoWindow({
content: ''
});

// Our markers
markers1 = [
['0', 'Title 1', 52.4357808, 4.991315699999973, 'car'],
['1', 'Title 2', 52.4357808, 4.981315699999973, 'third'],
['2', 'Title 3', 52.4555687, 5.039231599999994, 'car'],
['3', 'Title 4', 52.4555687, 5.029231599999994, 'second']
];

/**
* Function to init map
*/

function initialize() {
var center = new google.maps.LatLng(52.4357808, 4.991315699999973);
var mapOptions = {
zoom: 12,
center: center,
mapTypeId: google.maps.MapTypeId.TERRAIN
};

map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
for (i = 0; i < markers1.length; i++) {
addMarker(markers1[i]);
}
}

/**
* Function to add marker to map
*/

function addMarker(marker) {
var category = marker[4];
var title = marker[1];
var pos = new google.maps.LatLng(marker[2], marker[3]);
var content = marker[1];

marker1 = new google.maps.Marker({
title: title,
position: pos,
category: category,
map: map
});

gmarkers1.push(marker1);

// Marker click listener
google.maps.event.addListener(marker1, 'click', (function (marker1, content) {
return function () {
console.log('Gmarker 1 gets pushed');
infowindow.setContent(content);
infowindow.open(map, marker1);
map.panTo(this.getPosition());
map.setZoom(15);
}
})(marker1, content));
}

/**
* Function to filter markers by category
*/

filterMarkers = function (category) {
for (i = 0; i < gmarkers1.length; i++) {
marker = gmarkers1[i];
// If is same category or category not picked
if (marker.category == category || category.length === 0) {
marker.setVisible(true);
}
// Categories don't match
else {
marker.setVisible(false);
}
}
}

// Init map
initialize();
#map-canvas {
width: 500px;
height: 500px;
}
<script src="https://maps.googleapis.com/maps/api/js?key=&v=3.0&sensor=true&language=ee&dummy=dummy.js"></script>
<div id="map-canvas"></div>
<select id="type" onchange="filterMarkers(this.value);">
<option value="">Please select category</option>
<option value="second">second</option>
<option value="car">car</option>
<option value="third">third</option>
</select>



在标记上按多个类别过滤

@Myoji评论的 编辑

要在每个标记上使用多个类别,只需将它们添加为 array并在 if上编辑 filterMarkers条件。
markers1 = [
['0', 'Title 1', 52.4357808, 4.991315699999973, ['car', 'second']],
['1', 'Title 2', 52.4357808, 4.981315699999973, ['third']],
['2', 'Title 3', 52.4555687, 5.039231599999994, ['car', 'third']],
['3', 'Title 4', 52.4555687, 5.029231599999994, ['second']]
];

filterMarkers将是
/**
* Function to filter markers by category
*/

filterMarkers = function(category)
{
for (i = 0; i < gmarkers1.length; i++) {
marker = gmarkers1[i];

// If is same category or category not picked
if((typeof marker.category == 'object' && marker.category.indexOf(category) >= 0) || category.length == 0){
{
marker.setVisible(true);
}
// Categories don't match
else
{
marker.setVisible(false);
}
}
}

工作示例

var gmarkers1 = [];
var markers1 = [];
var infowindow = new google.maps.InfoWindow({
content: ''
});

// Our markers
markers1 = [
['0', 'Title 1', 52.4357808, 4.991315699999973, ['car', 'second']],
['1', 'Title 2', 52.4357808, 4.981315699999973, ['third']],
['2', 'Title 3', 52.4555687, 5.039231599999994, ['car', 'third']],
['3', 'Title 4', 52.4555687, 5.029231599999994, ['second']]
];

/**
* Function to init map
*/

function initialize() {
var center = new google.maps.LatLng(52.4357808, 4.991315699999973);
var mapOptions = {
zoom: 12,
center: center,
mapTypeId: google.maps.MapTypeId.TERRAIN
};

map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
for (i = 0; i < markers1.length; i++) {
addMarker(markers1[i]);
}
}

/**
* Function to add marker to map
*/

function addMarker(marker) {
var category = marker[4];
var title = marker[1];
var pos = new google.maps.LatLng(marker[2], marker[3]);
var content = marker[1];

marker1 = new google.maps.Marker({
title: title,
position: pos,
category: category,
map: map
});

gmarkers1.push(marker1);

// Marker click listener
google.maps.event.addListener(marker1, 'click', (function (marker1, content) {
return function () {
console.log('Gmarker 1 gets pushed');
infowindow.setContent(content);
infowindow.open(map, marker1);
map.panTo(this.getPosition());
map.setZoom(15);
}
})(marker1, content));
}

/**
* Function to filter markers by category
*/

filterMarkers = function (category) {
for (i = 0; i < gmarkers1.length; i++) {
marker = gmarkers1[i];
// If is same category or category not picked
if((typeof marker.category == 'object' && marker.category.indexOf(category) >= 0) || category.length == 0){
marker.setVisible(true);
}
// Categories don't match
else {
marker.setVisible(false);
}
}
}

// Init map
initialize();
#map-canvas {
width: 500px;
height: 500px;
}
<script src="https://maps.googleapis.com/maps/api/js?key=&v=3.0&sensor=true&language=ee&dummy=dummy.js"></script>
<div id="map-canvas"></div>
<select id="type" onchange="filterMarkers(this.value);">
<option value="">Please select category</option>
<option value="second">second</option>
<option value="car">car</option>
<option value="third">third</option>
</select>



过滤后拟合边界

编辑@bluantinoo评论
/**
* Function to filter markers by category
*/

filterMarkers = function(category)
{
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < gmarkers1.length; i++) {
marker = gmarkers1[i];

// If is same category or category not picked
if(marker.category == category || category.length == 0)
{
marker.setVisible(true);
bounds.extend(marker.getPosition());
}
// Categories don't match
else
{
marker.setVisible(false);
}
map.fitBounds(bounds);
}
}

工作示例

var gmarkers1 = [];
var markers1 = [];
var infowindow = new google.maps.InfoWindow({
content: ''
});

// Our markers
markers1 = [
['0', 'Title 1', 52.4357808, 4.991315699999973, 'car'],
['1', 'Title 2', 52.4357808, 4.981315699999973, 'third'],
['2', 'Title 3', 52.4555687, 5.039231599999994, 'car'],
['3', 'Title 4', 52.4555687, 5.029231599999994, 'second']
];

/**
* Function to init map
*/

function initialize() {
var center = new google.maps.LatLng(52.4357808, 4.991315699999973);
var mapOptions = {
zoom: 12,
center: center,
mapTypeId: google.maps.MapTypeId.TERRAIN
};

map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
for (i = 0; i < markers1.length; i++) {
addMarker(markers1[i]);
}
}

/**
* Function to add marker to map
*/

function addMarker(marker) {
var category = marker[4];
var title = marker[1];
var pos = new google.maps.LatLng(marker[2], marker[3]);
var content = marker[1];

marker1 = new google.maps.Marker({
title: title,
position: pos,
category: category,
map: map
});

gmarkers1.push(marker1);

// Marker click listener
google.maps.event.addListener(marker1, 'click', (function (marker1, content) {
return function () {
console.log('Gmarker 1 gets pushed');
infowindow.setContent(content);
infowindow.open(map, marker1);
map.panTo(this.getPosition());
map.setZoom(15);
}
})(marker1, content));
}

/**
* Function to filter markers by category
*/

filterMarkers = function(category)
{
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < gmarkers1.length; i++) {
marker = gmarkers1[i];

// If is same category or category not picked
if(marker.category == category || category.length == 0)
{
marker.setVisible(true);
bounds.extend(marker.getPosition());
}
// Categories don't match
else
{
marker.setVisible(false);
}
map.fitBounds(bounds);
}
}

// Init map
initialize();
#map-canvas {
width: 500px;
height: 500px;
}
<script src="https://maps.googleapis.com/maps/api/js?key=&v=3.0&sensor=true&language=ee&dummy=dummy.js"></script>
<div id="map-canvas"></div>
<select id="type" onchange="filterMarkers(this.value);">
<option value="">Please select category</option>
<option value="second">second</option>
<option value="car">car</option>
<option value="third">third</option>
</select>

关于arrays - 如何使用select过滤一个数组中的Google Maps标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22323073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com