根据范围在Javascript中创建标记

时间:2014-11-05 16:26:14

标签: javascript google-maps-api-3 google-maps-markers

我知道我的语法不正确,但我似乎无法找出基于范围创建不同颜色标记的正确语法。这就是我现在所拥有的,我的所有标记都显示为绿色(blank_g.png)

  function createMarker(point,info,map,mag) {

        var colorMarker = "blank_g.png"  //for mag < 4 

        if (mag >= 4.0 && mag < 5.0) {
        colorMarker = "blank_b.png";
        }
        else if (mag >= 5.0 && mag < 6.0) {
        colorMarker = "blank_p.png";
        }
        else if (mag >= 6.0 && mag < 7.0) {
        colorMarker = "blank_y.png";
        }
        else if (mag >= 7.0 && mag < 8.0) {
        colorMarker = "blank_o.png";
        }
        else if (mag >= 8.0) {
        colorMarker = "blank_r.png";
        }

1 个答案:

答案 0 :(得分:0)

function CreateMarker (point, info, map, mag)
{
    // Recommended: Check value of mag for range

    var imageNames =
    [
        "blank_g.png",
        "blank_g.png",
        "blank_g.png",
        "blank_g.png",
        "blank_b.png",
        "blank_p.png",
        "blank_y.png",
        "blank_o.png",
        "blank_r.png",
    ];

    var imageIndex = Math.max(0, Math.min(Math.floor(mag), imageNames.length - 1));
    var imgBaseUrl = "/images/"; // Or whatever your path is.
    var imgSrc = imageBaseUrl + imageNames[imageIndex];

    var marker = new google.maps.Marker
    ({
        icon: MarkerIconUrl(imgSrc),
        map: map,
        position: point,
        title: info
    });

    return marker;
}

function MarkerIconUrl (imgSrc)
{
    return new google.maps.MarkerImage(imgSrc);
}

function MarkerIconUrlByRgb (color)
{
    return new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|" + color);
}