8

我正在尝试这样做,以便当将 html 元素鼠标悬停在 google maps api v3 中标记的颜色代码上时会发生变化。

这是谷歌地图代码:

$(document).ready(function(){
var markers;
var map;
var infowindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
markers = new Array();

var mapOptions = {
    zoom: 0, //Set to 0 because we are using auto formatting w/ bounds
    disableDefaultUI: true,
    zoomControl: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    };

map = new google.maps.Map(document.getElementById("map"), mapOptions);
map.fitBounds(bounds);

$("#map_list ul li").each(function(index) {
    var markerLatLng = new google.maps.LatLng($(this).children(".marker_lat").text(), $(this).children(".marker_long").text());
    var marker = new google.maps.Marker({
    position: markerLatLng,
    map: map,
    animation: google.maps.Animation.DROP,
    title : $(this).children(".marker_title").text(),
    brief: $(this).children(".marker_brief").text(),
    icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld='+$(this).children(".marker_number").text()+'|00aeef|000000'
    });

    markers.push(marker);
    //add to bounds for auto center and zoom
    bounds.extend(markerLatLng);    
});

});

它在网页中从我的 html 动态构建标记,如下所示:

<div id="map_list">
<ul>             
     <li id="0">
        <div class="marker_number">1</div>
        <div class="marker_title">The Wedding Center</div>
        <div class="marker_lat">45.361885</div>
        <div class="marker_long">-122.599007</div>
      </li>
      <li id="1">
        <div class="marker_number">2</div>
        <div class="marker_title">The Reception Place</div>
        <div class="marker_lat">45.417870</div>
        <div class="marker_long">-122.658531</div>
      </li>
</ul>
 </div>

我怎样才能做到这一点,当我将鼠标悬停在#map_list ul li 上时,它会将颜色代码 00aeef 更改为 ff0000?

4

2 回答 2

8

翻译自 Mike Williams 的 v2 教程的示例(只需更改侧边栏中 HTML 元素鼠标悬停时的标记图标)。

在 mouseover/mouseout 上更改标记的代码:

// A function to create the marker and set up the event window function 
function createMarker(latlng,name,html,color) {
  var contentString = html;
  var marker = new google.maps.Marker({
    position: latlng,
    icon: gicons[color],
    map: map,
    title: name,
    zIndex: Math.round(latlng.lat()*-100000)<<5
  });

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(contentString); 
    infowindow.open(map,marker);
  });
  // Switch icon on marker mouseover and mouseout
  google.maps.event.addListener(marker, "mouseover", function() {
    marker.setIcon(gicons["yellow"]);
  });
  google.maps.event.addListener(marker, "mouseout", function() {
    marker.setIcon(gicons["blue"]);
  });
  gmarkers.push(marker);
  // add a line to the side_bar html
  var marker_num = gmarkers.length-1;
  side_bar_html += '<a href="javascript:myclick(' + marker_num + ')" onmouseover="gmarkers['+marker_num+'].setIcon(gicons.yellow)" onmouseout="gmarkers['+marker_num+'].setIcon(gicons.blue)">' + name + '<\/a><br>';
}

使用 KML/geoxml3 的示例

于 2012-10-25T00:47:17.713 回答
6

你可以试试这个:

$(document).ready(function(){
    var map, infowindow = new google.maps.InfoWindow(),
    bounds = new google.maps.LatLngBounds(), markers=[], 
    alternateMarkers=[], markersIcon=[];
    var mapOptions = {
        zoom: 0, //Set to 0 because we are using auto formatting w/ bounds
        disableDefaultUI: true,
        zoomControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
    };

    map = new google.maps.Map(document.getElementById("map"), mapOptions);
    map.fitBounds(bounds);

    $("#map_list ul li").each(function(index) {
        var mTxt=$(this).children(".marker_number").text();
        var markerLatLng = new google.maps.LatLng($(this).children(".marker_lat").text(), $(this).children(".marker_long").text());
        var markImg=new google.maps.MarkerImage('http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld='+mTxt+'|00aeef|000000');
        var altMarkImg=new google.maps.MarkerImage('http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld='+mTxt+'|ff0000');
        var marker = new google.maps.Marker({
            position: markerLatLng,
            map: map,
            animation: google.maps.Animation.DROP,
            title : $(this).children(".marker_title").text(),
            brief: $(this).children(".marker_brief").text(),
            icon: markImg
    });
        markers.push(marker);
        markersIcon.push(markImg);
        alternateMarkers.push(altMarkImg);
        //add to bounds for auto center and zoom
        bounds.extend(markerLatLng);
    });
    $("#map_list ul li").on('mouseenter', function(){
        var id=$(this).attr('id');
        markers[id].setIcon(alternateMarkers[id]);
    }).on('mouseleave', function(){
        var id=$(this).attr('id');
        markers[id].setIcon(markersIcon[id]);      
    });    
});​

演示小提琴

于 2012-10-25T01:41:27.820 回答