【问题标题】:Different Style Markers On Google Map谷歌地图上的不同风格标记
【发布时间】:2012-10-05 09:45:31
【问题描述】:

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

可以有 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>

【问题讨论】:

    标签: google-maps


    【解决方案1】:

    你几乎得到它。我假设这样的事情符合您的要求:

    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-06-19
    • 2013-09-08
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多