Google maps api 3 如何在单击时重新绘制谷歌地图标记?
我有一张谷歌地图,上面有一组地图标记。我选择使用名为pinSymbol()的函数绘制地图标记,而不是使用默认图像 我想在点击pin时更改其颜色,但无法更新。使用当前代码,我可以更改图标的属性,我可以通过console.log(marker)看到这一点,但是它不会更新地图上的颜色 问题:如何在单击时重新绘制图标 这是我的密码Google maps api 3 如何在单击时重新绘制谷歌地图标记?,google-maps-api-3,google-maps-markers,Google Maps Api 3,Google Maps Markers,我有一张谷歌地图,上面有一组地图标记。我选择使用名为pinSymbol()的函数绘制地图标记,而不是使用默认图像 我想在点击pin时更改其颜色,但无法更新。使用当前代码,我可以更改图标的属性,我可以通过console.log(marker)看到这一点,但是它不会更新地图上的颜色 问题:如何在单击时重新绘制图标 这是我的密码 // Go through all restaurants and get facebook info, // then create a marker for each o
// Go through all restaurants and get facebook info,
// then create a marker for each one.
restaurants.forEach(function(restaurant){
getFacebookInfo(restaurant);
}); // end forEach loop
// Get data from Facebook Graph API and create a marker
function getFacebookInfo(restaurant){
$.ajax({
url : '/restaurants/' + restaurant.id,
type : 'GET',
dataType:'json',
success : function(data) {
restaurant.about = data.about;
createMarker(restaurant);
},
error : function(request, error) {
console.log(error);
alert("We're having some trouble getting a restaurant's info from Facebook. " +
"Please check your internet connection and try refreshing the page.")
}
});
}
// Create a marker on the map for a location
function createMarker(restaurant){
var position = restaurant.location;
var infowindow = new google.maps.InfoWindow({
maxWidth: 200
});
restaurant.marker = new google.maps.Marker({
position: position,
map: map,
icon: pinSymbol('#CD212A', '#CD212A'),
name: restaurant.name,
id: restaurant.id,
about: restaurant.about,
animation: google.maps.Animation.DROP
});
// Push the marker to array of markers
markers.push(restaurant.marker);
// Call populateInfoWindow function
populateInfoWindow(restaurant.marker, infowindow);
// Add infowindow as a property to restaurant
// this makes it available for use outside this function.
restaurant.infowindow = infowindow;
这就是我被困的地方:
// Open infowindow when marker is clicked and change color
restaurant.marker.addListener('click', function(){
this.icon = pinSymbol('#EED4D9', '#EED4D9');
console.log(restaurant.marker);
infowindow.open(map, this);
});
}
pinSymbol函数
// Create pin for google map marker
function pinSymbol(color, strokeColor) {
return {
path: 'M 0,0 C -2,-20 -10,-22 -10,-30 A 10,10 0 1,1 10,-30 C 10,-22 2,-20 0,0 z',
fillColor: color,
fillOpacity: 1,
strokeColor: strokeColor,
strokeWeight: 1,
scale: 1,
labelOrigin: new google.maps.Point(0,-29)
};
}
标记没有(记录的)。图标
属性。不要用它。使用记录在案的.setIcon
方法:
// Open infowindow when marker is clicked and change color
restaurant.marker.addListener('click', function() {
this.setIcon(pinSymbol('#EED4D9', '#EED4D9'));
console.log(restaurant.marker);
infowindow.open(map, this);
});
代码片段:
var地理编码器;
var映射;
var标记=[];
函数初始化(){
map=新建google.maps.map(
document.getElementById(“地图画布”){
中心:新google.maps.LatLng(37.4419,-122.1419),
缩放:13,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
创建标记({
名称:“中心”,
id:2,
大约:“,
地点:{
拉脱维亚:37.4419,
液化天然气:-122.1419
}
});
}
google.maps.event.addDomListener(窗口“加载”,初始化);
//在地图上为位置创建标记
功能标志(餐厅){
var位置=餐厅位置;
var infowindow=new google.maps.infowindow({
最大宽度:200
});
restaurant.marker=新的google.maps.marker({
职位:职位,,
地图:地图,
图标:pinSymbol(“#CD212A”、“#CD212A”),
姓名:餐厅。姓名,
id:restaurant.id,
关于:餐馆,
动画:google.maps.animation.DROP
});
//将标记按到标记数组中
推动(餐厅、标记器);
//调用populateInfo窗口函数
PopulateInfo窗口(restaurant.marker、infowindow);
//将infowindow作为属性添加到餐厅
//这使其可在此功能之外使用。
restaurant.infowindow=infowindow;
//单击标记并更改颜色时打开信息窗口
restaurant.marker.addListener('click',function()){
if(this.getIcon().fillColor!=“#EED4D9”){
这个.setIcon(pinSymbol('EED4D9','EED4D9');
}否则{
这个.setIcon(pinSymbol('CD212A','CD212A');
}
控制台。日志(餐厅。标记);
打开(地图,这个);
});
}
//为谷歌地图标记创建pin
功能pinSymbol(颜色、笔划颜色){
返回{
路径:'m0,0c-2,-20-10,-22-10,-30a10,1001,110,-30c10,-222,-20,0z',
fillColor:color,
不透明度:1,
strokeColor:strokeColor,
冲程重量:1,
比例:1,
labelOrigin:new google.maps.Point(0,-29)
};
}
函数populateInfo窗口(标记,信息窗口){
infowindow.setContent(“内容”);
};代码>
html,
身体,
#地图画布{
身高:100%;
宽度:100%;
边际:0px;
填充:0px
}