2014-09-03 98 views
0

我想要的是當用戶做鼠標懸停(懸停)然後圖標更改。我的代碼如下:當鼠標懸停或更改圖標標記

handler = Gmaps.build("Google", { 
markers: { 
    maxRandomDistance: null 
} 
}); 
handler.buildMap({ 
provider: {}, 
internal: { 
    id: "map-canvas" 
} 
}, function() { 
var markers; 
markers = handler.addMarkers(ar); 
_.each(ar, function(json, index) { 
    json.marker = markers[index]; 
    $(".location-" + json.id).on("mouseover", function() { 
    json.picture = { 
     url: "http://maps.google.com/mapfiles/ms/icons/green-dot.png", 
     width: 36, 
     height: 36 
    }; 
    json.marker.setMap(handler.getMap()); 
    json.marker.panTo(); 
    handler.removeMarker(json.marker); 
    handler.addMarker(json); 
    }).on("mouseout", function() { 
    json.picture = ''; 
    handler.removeMarker(json.marker); 
    handler.addMarker(json); 
    }); 
}); 
handler.bounds.extendWith(markers); 
handler.fitMapToBounds(); 
}); 

使用此代碼,標記的顏色可以從紅色變爲綠色。但是,當用戶將鼠標從懸停區域移開時,顏色不會變回原始顏色。請任何人都可以在這個問題上建議我?

感謝

+1

後插入變量如果更換'json.picture =「」'什麼'刪除json.picture' – apneadiving 2014-09-03 06:47:10

+0

@apneadiving可以你建議我關於盤旋div元素的懸停代碼? 該示例是https://www.airbnb.com/s/Barcelona--Spain。當懸停時將地圖圖標顏色更改爲藍色 – 2014-09-03 06:57:36

回答

-1

試着這麼做:

_.each(ar, function(json, index) { 

    var json.marker = markers[index]; 
    var initialPicture = json.picture; 
    var hoverPicture = { 
    url: "http://maps.google.com/mapfiles/ms/icons/green-dot.png", 
    width: 36, 
    height: 36 
    } 

    $(".location-" + json.id).on("mouseover", function() { 
    json.picture = hoverPicture; 
    json.marker.setMap(handler.getMap()); 
    json.marker.panTo(); 
    handler.removeMarker(json.marker); 
    handler.addMarker(json); 
    }).on("mouseout", function() { 
    json.picture = initialPicture; 
    handler.removeMarker(json.marker); 
    var newMarker = handler.addMarker(json); 
    json.marker = newMarker; 
    }); 
}); 
+0

無法正常工作。有沒有像handler.setIcons({picture:「green.png」})或handler.setIcons({fillcolor:「green」})的語法? – 2014-09-03 07:27:17

+0

fillColor不是谷歌地圖標記的選項,所以沒有。你可以顯示什麼生成的HTML? – apneadiving 2014-09-03 09:02:46

+0

這是返回一個錯誤'var json.marker = markers [index];' – Batman 2015-11-04 00:32:36

1

海感謝@apneadiving答案。我修改我的代碼,這

hoverPicture = { 
url: "http://maps.google.com/mapfiles/ms/icons/green-dot.png", 
width: 33, 
height: 33 
}; 
handler = Gmaps.build("Google", { 
markers: { 
    maxRandomDistance: null 
} 
}); 
handler.buildMap({ 
provider: {}, 
internal: { 
    id: "map-canvas" 
} 
}, function() { 
var markers; 
markers = handler.addMarkers(ar); 
_.each(ar, function(json, index) { 
    var gr; 
    json.marker = markers[index]; 
    gr = {}; 
    gr.marker = void 0; 
    $(".location-" + json.id).on("mouseover", function() { 
    gr = { 
     lat: json.lat, 
     lng: json.lng, 
     picture: hoverPicture 
    }; 
    json.marker.panTo(); 
    handler.removeMarker(json.marker); 
    gr.marker = handler.addMarker(gr); 
    }).on("mouseout", function() { 
    handler.removeMarker(gr.marker); 
    json.marker = handler.addMarker(json); 
    }); 
}); 
handler.bounds.extendWith(markers); 
handler.fitMapToBounds(); 
}); 

因此,新的圖標需要addMarker

+0

這可行。我認爲關鍵是這一行'gr.marker = handler.addMarker(gr);'所以當你刪除'mouseout'上的標記時,你的目標是你添加的內容。 – Batman 2015-11-04 01:41:54