谷歌API多个标记与不同的颜色取决于一个类

Google API multiple markers with different colours depending on a class

本文关键字:取决于 颜色 一个 API 谷歌      更新时间:2023-09-26

下面的代码是CMS系统使用邮政编码和城市创建的标记。一切都很好,但我要做的是改变每个针的颜色,这取决于一个类,我有3个类,红色,绿色和蓝色,这个标记应该根据在CMS系统中选择的类而改变。实现这一目标的最佳方法是什么?

$(document).ready(function () {
        var map;
        var elevator;
        var myOptions = {
            zoom: 3,
            center: new google.maps.LatLng(50, -30),
            mapTypeId: 'terrain'
        };
        map = new google.maps.Map($('#map_canvas')[0], myOptions);
        var styles = [{"featureType":"water","stylers":[{"color":"#46bcec"},{"visibility":"on"}]},{"featureType":"landscape","stylers":[{"color":"#f2f2f2"}]},{"featureType":"road","stylers":[{"saturation":-100},{"lightness":45}]},{"featureType":"road.highway","stylers":[{"visibility":"simplified"}]},{"featureType":"road.arterial","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"administrative","elementType":"labels.text.fill","stylers":[{"color":"#444444"}]},{"featureType":"transit","stylers":[{"visibility":"off"}]},{"featureType":"poi","stylers":[{"visibility":"off"}]}];
        map.setOptions({styles: styles});
        var iterator = 0;       
        var addresses = [   

       ['<div class="red"></div>New York', ' 10007'],

       ['<div class="green"></div>Mexico', ' 10100'],

       ['<div class="blue"></div>Spain', ' 04830'],
    ];

            for (var x = 0; x < addresses.length; x++) {
                    MarkMe();
            }

        function MarkMe() {
            var add = addresses[iterator];
            $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+addresses[x]+'&sensor=false', null, function (data) {
                var p = data.results[0].geometry.location
                var o = data.results[0]
                var latlng = new google.maps.LatLng(p.lat, p.lng);
                var pinColor = "red";
                var pinImage = new google.maps.MarkerImage('/assets/images/pin-' + pinColor + '.png',
                new google.maps.Size(22, 31));
                var content = '<div style="width: 150px;">' + add + '<br/></div>';
                var marker =  new google.maps.Marker({
                    position: latlng,
                    map: map,
                    clickable: true,
                    icon: pinImage,
                    title: o + '<br/>' + add,
                     });
    function getInfoCallback(map, content) {
                        var infowindow = new google.maps.InfoWindow({ content: content });
                        return function () {
                            infowindow.setContent(content);
                            infowindow.open(map, this);
                        };
                    };
                    google.maps.event.addListener(marker, 'click', getInfoCallback(map, content));
                });
                iterator++;
           }
        })

你可以创建一个类型为(restaurants,hotels)的数组或者设置validate

的默认值

,试试下面的代码:

var marker_new = new Array();
var url = "";
if(a == "hotels"){
   url = "http://exampl.com/yellow.png";   
}else{
   url = "http://exampl.com/green.png"
}
marker_new[i] = google.maps.Marker({
                                 icon: _url;
                                 position: place.geometry.location,
                                 map: map
                               });

如何使用

    marker_new[i] = new google.maps.Marker({
      position: place.geometry.location,
      map: map
    });
    iw = new google.maps.InfoWindow({
      content: getIWContent(place)
    });
    iw.open(map, marker_new[i]);     

如何使用

          marker_new[i] = new google.maps.Marker({
            position: results[i].geometry.location,
            animation: google.maps.Animation.DROP
          });
          google.maps.event.addListener(marker_new[i], 'click', getDetails(results[i], i));

或阅读更多:我如何修改标记?