谷歌地图上的不同风格标记

时间:2012-10-15 12:03:20

标签: google-maps

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

可以有1到12个单独的标记,如果变量favour等于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>

1 个答案:

答案 0 :(得分: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)
            )
        });
}