javascript - 点击时谷歌地图更改标记图标
问题描述
如何在单击标记时(在单击事件上)更改标记图标,并在单击另一个标记时将其恢复为正常图标?
如何加载已单击标记的地图?
此外,如果可以在单击图标时添加一些动画,例如过渡。
function initialize() {
/* Lat. and Lon. of the center of the map */
var myCenter = new google.maps.LatLng(42.8012564, 10.75113);
// Create a map object, and include the MapTypeId to add
// to the map type control.
var mapOptions = {
zoom: 14, //zoom level
center: myCenter, //center position
scrollwheel: false, //zoom when scroll disable
zoomControl: true, //show control zoom
disableDefaultUI: true,
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style'],
}
};
var map = new google.maps.Map(document.getElementById('map'),mapOptions);
var image1 = 'http://puntaala.noonicbeta.com/wp-content/themes/Wordpress/img/marker-grey.svg';
var marker2 = new google.maps.Marker({
position: new google.maps.LatLng(42.80207599824027, 10.756003014830668),
map: map,
icon: image1,
title: 'Hotel',
description: 'Magna exercitation id culpa eiusmod velit veniam. Cillum eiusmod irure dolore officia proident eiusmod Lorem dolore. Enim cupidatat esse laborum minim sint laborum ad duis ea est adipisicing incididunt ut veniam. Aliqua exercitation aute nostrud labore cupidatat velit commodo tempor minim voluptate non quis voluptate. Cupidatat aute velit est reprehenderit eu fugiat aliquip est do […]',
luogo: 'LA MAREMMA',
indirizzo: 'Costigliole della Pescaia',
});
function showClick (marker) {
var title = document.getElementById('title');
var description = document.getElementById('description');
var luogo = document.getElementById('luogo');
var indirizzo = document.getElementById('indirizzo');
title.innerHTML=marker.title;
description.innerHTML=marker.description;
indirizzo.innerHTML=marker.indirizzo;
luogo.innerHTML=marker.luogo;
marker.setIcon("http://puntaala.noonicbeta.com/wp-content/themes/Wordpress/img/marker.svg");
}
google.maps.event.addListener(marker2, 'click', function() {
showClick(marker2);
});
var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(42.8012564, 10.75113),
map: map,
icon: image1,
title: 'Roccaforte',
description: 'ex ex reprehenderit aute exercitation cupidatat deserunt irure cillum sit ad culpa aliqua aliqua cupidatat anim exercitation commodo incididunt proident adipisicing fugiat excepteur elit voluptate incididunt minim esse ea incididunt voluptate amet ipsum quis sunt ullamco non consequat non eiusmod dolore nulla occaecat mollit pariatur cupidatat aliquip adipisicing voluptate nulla esse labore ad ipsum quis […]',
luogo: 'La Maremma',
indirizzo: 'Costigliole della Pescaia',
});
function showClick (marker) {
var title = document.getElementById('title');
var description = document.getElementById('description');
var luogo = document.getElementById('luogo');
var indirizzo = document.getElementById('indirizzo');
title.innerHTML=marker.title;
description.innerHTML=marker.description;
indirizzo.innerHTML=marker.indirizzo;
luogo.innerHTML=marker.luogo;
marker.setIcon("http://puntaala.noonicbeta.com/wp-content/themes/Wordpress/img/marker.svg");
}
google.maps.event.addListener(marker1, 'click', function() {
showClick(marker1);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
解决方案
如何在单击标记时(在单击事件上)更改标记图标,并在单击另一个标记时将其恢复为正常图标?
您可以使用 google maps api 来使用标记的“信息窗口”功能。只有一个活动窗口的处理已经完成。您可以查看信息窗口文档。但是,如果您想自己执行此操作,则可以将所有标记全局保存在一个数组中,并具有“取消选择”除您刚刚选择的标记之外的所有其他标记的功能。就像您的“showClick(marker)”功能一样,反之亦然。
如何加载已单击标记的地图?
为此,您可以在初始化函数的末尾调用您的 showClick(marker1) 函数,或者当您使用谷歌地图 api 中的“信息窗口”时,您可以在最后分别调用 infowindow.open(map, marker) 如果你的初始化函数。
此外,如果可以在单击图标时添加一些动画,例如过渡。
谷歌地图 api 也支持这一点。您可以查看标记动画文档以了解它是如何实现的
推荐阅读
- python - 如果在 Python 中没有立即释放单击,我如何才能使按钮单击不执行?
- jupyter-notebook - 当 Jupyter Hub 中的 Python 文件很大时,无法添加目录扩展
- azure-data-factory - 在 .net 核心中使用 .NET SDK 创建 Azure 数据工厂?
- arrays - Hackerrank 中 numpy 的眼睛和身份功能。为什么测试用例失败了?
- uwp - CalendarDatePicker,TodayDate 在 UWP 中没有突出显示?
- python - 如果该行与DataFrame中的其他行有一定关系,如何删除行?
- node.js - 如何使用 node.js 和 pm2 将 verdaccio 发布到互联网?
- sql - sql中存储过程的嵌套循环不起作用
- c++ - 为什么我在提交leetcode时会报错,但在IDE中使用相同的代码?
- retrofit2 - 带有 Retrofit 2 的 kotlinx 序列化中的构建错误