更改特定谷歌地图标记的颜色

时间:2014-01-20 16:58:28

标签: json google-maps-api-3

我目前使用以下代码在我的网站上呈现谷歌地图,以显示它并将标记放在地图上。

    // Creating a new map
    //UK Cordinates 55.37911,-3.427734 zoom 9
    var map = new google.maps.Map(document.getElementById("map"), {
            center: new google.maps.LatLng(54.901882,-3.603516),
            zoom: 6,
            panControl: false,
            scrollwheel: false,
      mapTypeControl: false,
      streetViewControlOptions: {position: google.maps.ControlPosition.LEFT_CENTER},
      zoomControlOptions: {position: google.maps.ControlPosition.LEFT_CENTER},
      mapTypeId: google.maps.MapTypeId.ROADMAP


    // Creating the JSON data
    var json = [
       {
            "title": "BLAH",
            "lat": 51.514979,
            "lng": -0.134175,
            "description": "BLAHBLAHBLAH"
        },
        {
            "title": "BLAH",
            "lat": 51.512779,
            "lng": -0.132625,
            "description": "BLAHBALH"

        },
        {
            "title": "BLAH",
        "lat": 51.489025,
            "lng": -0.191840,
            "description": "BLAHBLAH"
        },

我想只让某些标记显示为绿色而不是标准红色。 有很多关于如何在网络上执行此操作的信息,但不是通过放置我的标记的方法(JSON数据)。 以前的设计师做了这个,所以我不确定!

非常感谢!

1 个答案:

答案 0 :(得分:1)

您将需要JSON数据中的某些内容,以指示哪些标记应为绿色,哪些标记应为红色。这取决于你理清。我假设我们在那里有一个新的布尔值。但实际上,您可能更喜欢指定标记图像的URL(例如,如果使用的不仅仅是绿色和红色标记)。

我现在以伪伪代码的形式写这个,直到你向我们提供更多代码,展示你自己是如何实际做的。

loop over json
    // create marker
    marker = new google.maps.Marker({
        ...
    });

    if (json.useGreen) {
        // use the green marker
        marker.setIcon('http://maps.google.co.uk/intl/en_ALL/mapfiles/ms/micons/green.png');    
    }
end-loop
相关问题