0

我有以下脚本,当用户单击链接时,它会使用标记填充地图。

可以有 1 到 12 个单独的标记,如果变量 favourite 等于 1,我想做的是使用不同的标记。

同一张地图上可以有多个收藏夹。

我假设在“新的 google.maps.Marker”部分中需要添加一个“if favorite == 1”,但我无法理解它!

<script>
$(document).ready(function() {
$("#map").css({
    height: 900,
    width: 1400
});
var myLatLng = new google.maps.LatLng(<%=dcLatLng%>); //Query database for map center
MYMAP.init('#map', myLatLng, 12);

$("#showmarkers").click(function(e){
    MYMAP.placeMarkers('markers.asp?WisperID=<%=sWisperID%>');
});
});

var MYMAP = {
map: null,
bounds: null
}

MYMAP.init = function(selector, latLng, zoom) {
var myOptions = {
zoom:zoom,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
this.map = new google.maps.Map($(selector)[0], myOptions);
this.bounds = new google.maps.LatLngBounds();
}

MYMAP.placeMarkers = function(filename) {
$.get(filename, function(xml){
    $(xml).find("marker").each(function(){
        var name = $(this).find('name').text();
        var address = $(this).find('address').text();
        var postcode = $(this).find('postcode').text();
        var favourite = $(this).find('favourite').text();
        // create a new LatLng point for the marker
        var lat = $(this).find('lat').text();
        var lng = $(this).find('lng').text();
        var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));

        // extend the bounds to include the new point
        MYMAP.bounds.extend(point);



        var marker = new google.maps.Marker({
            position: point,
            map: MYMAP.map,
            icon: new google.maps.MarkerImage(
            '/images/int.png',
            new google.maps.Size(93, 63),
            new google.maps.Point(0, 0),
            new google.maps.Point(0, 63)
  )
        });




        var infoWindow = new google.maps.InfoWindow();
        var html='<strong>'+name+'</strong.><br />'+address+'<br  />'+postcode+'<br /><p>'+favourite+'</p>';
        google.maps.event.addListener(marker, 'mouseover', function() {
            infoWindow.setContent(html);
            infoWindow.open(MYMAP.map, marker);
        });
        google.maps.event.addListener(marker, 'mouseout', function() {
            infoWindow.setContent(html);
            infoWindow.close(MYMAP.map, marker);
        });
        MYMAP.map.fitBounds(MYMAP.bounds);
    });
});
}
</script>
4

1 回答 1

0

你几乎明白了。我假设这样的事情符合您的要求:

if (favourite == '1') {
        var marker = new google.maps.Marker({
            position: point,
            map: MYMAP.map,
            icon: new google.maps.MarkerImage(
                '/images/favourite.png',  // different image
                new google.maps.Size(93, 63),
                new google.maps.Point(0, 0),
                new google.maps.Point(0, 63)
            )
        });
} else {
        var marker = new google.maps.Marker({
            position: point,
            map: MYMAP.map,
            icon: new google.maps.MarkerImage(
                '/images/int.png',
                new google.maps.Size(93, 63),
                new google.maps.Point(0, 0),
                new google.maps.Point(0, 63)
            )
        });
}
于 2012-10-15T12:44:17.970 回答