gpt4 book ai didi

javascript - 谷歌地图 : add multiple map markers

转载 作者:行者123 更新时间:2023-12-02 17:03:14 25 4
gpt4 key购买 nike

我的设置目前我有一个用于 map 上多个点的自定义 map 标记,但我想根据其 type 属性为 map 上的不同标记添加不同的 map 标记。我当前的设置如下:

HTML

<div class="marker" data-lat="LATITUDE_GOES_HERE" data-lng="LONGITUTE_GOES_HERE" type="library">

jQuery(只是标记函数)

function add_marker( $marker, map ) {

// var
var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );

var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';

// create marker
var marker = new google.maps.Marker({
position : latlng,
map : map,
icon: iconBase + 'schools_maps.png'
});

// add to array
map.markers.push( marker );

// if marker contains HTML, add it to an infoWindow
if( $marker.html() )
{
// create info window
var infowindow = new google.maps.InfoWindow({
content : $marker.html()
});

// show info window when marker is clicked
google.maps.event.addListener(marker, 'click', function() {

infowindow.open( map, marker );

});
}

}

这很好用,但是我如何根据它们具有的 type 属性(如果可能)为 map 上的不同标记提供不同的标记图标?
任何建议将不胜感激!

最佳答案

你的意思是......

var iconName = "";
var type = $marker.attr("type");
if (type == "library")
iconName = "library.png";
else if (type == "school")
iconName = "school.png";
// ...etc...

var marker = new google.maps.Marker({
position : latlng,
map : map,
icon: iconBase + iconName;
});

当然,您必须自己提供(并且可能托管)图标...

关于javascript - 谷歌地图 : add multiple map markers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25495657/

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