简体   繁体   English

如何使用Google Maps JavaScript API 3分别填充地图图层?

[英]How do I fillColor map layers individually with Google Maps JavaScript API 3?

How do I fillColor map layers individually? 如何分别填充地图图层? I have a function set up to style building polygons based on a value stored in the GeoJSON. 我设置了一个功能,用于根据存储在GeoJSON中的值来构建多边形样式。 Please excuse my ignorance in terminology. 请原谅我对术语的无知。 I am new to JS and have been at this problem for a solid week, searching the web and watching hours of tutorials, but can't answer this specific question. 我是JS的新手,已经在这个问题上呆了整整一周,在网络上搜索并观看教程时间,但是无法回答这个特定问题。

My functioning code contains to the following to initialize and create the map: 我的运行代码包含以下内容以初始化和创建地图:

    function initialize() {

        var mapOptions = {
            zoom: 16,
            center: new google.maps.LatLng(30.284, -97.7325),
                    mapTypeControlOptions: {
                        mapTypeIds: [google.maps.MapTypeId.ROADMAP, MY_MAPTYPE_ID]
                    },
            mapTypeId: MY_MAPTYPE_ID
        };

        // build the map
        map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);

        // add colors
        stylePolygons();

        //fetch map layers
        $.getJSON( parkingLot, function( data ) {
            loadGeoJSON(data);
        });


        $.getJSON( building, function( data ) {
            loadGeoJSON(data);
        });
};

Later in the script I use this, which works, to color the buildings 稍后在脚本中,我将使用此方法为建筑物着色

// assign colors based on value property of each feature
    function stylePolygons () {
        map.data.setStyle(function(feature) {
            var value = feature.getProperty('value');
            var color = colors[value];
            return {
                fillColor: color,
                strokeWeight: 0.5,
                fillOpacity: 1
            };
        });
    };

How do I style the rest of my layers individually? 如何分别设置其余图层的样式? An inline code or simple function would be good as the layers are simple and I don't want to append values to their tables. 内联代码或简单函数会很好,因为图层很简单,我不想将值附加到表中。

I gather I should use a local variable, or a function targeting specific objects, and assume there is some simple code such as: 我收集我应该使用局部变量或针对特定对象的函数,并假设有一些简单的代码,例如:

parkingLot.fillColr('red');

or 要么

parkingLot(fillColr: 'red');

or 要么

fillColor(parkingLot){
fillColor: 'red'
};

but i'm shooting in the dark. 但是我在黑暗中射击。 Any advice is really appreciated. 任何建议都非常感谢。

Thanks, 谢谢,

it's difficult to say without knowing the schema of your parkinglot geojson, but let's imagine they are something like 在不知道您的Parkinggeojson模式的情况下很难说,但让我们想象一下它们就像

{
    "type": "FeatureCollection",

    "features": [{
        "type": "Feature",
        "properties": {
            "color": "red"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [-116.9933, 34.0557, 14.3]
        }
    }, {
        "type": "Feature",
        "properties": {
            "color": "blue"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [-147.7822, 61.9128, 38.3]
        }
    }, {
        "type": "Feature",
        "properties": {
            "color": "green"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [-123.105, 38.6473, 1.1]
        }
    }, {
        "type": "Feature",
        "properties": {
            "color": "purple"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [-122.7997, 38.8318, 2.3]
        }
    }]
}

Now, you haven't said where are you storing your polygons. 现在,您还没有说要在哪里存储多边形。 Are you instantiating a new google.maps.Data layer? 您要实例化新的google.maps.Data图层吗?

If you are, then every feature in your new data layer is a google.maps.Data.Feature object and it can be styles individualy using the getProperty() method and a function much alike your stylePolygons , iterating on each feature and setting its color as the outcome of feature.getProperty('color') . 如果是这样,那么新数据层中的每个要素都是google.maps.Data.Feature对象,可以使用getProperty()方法和一个非常类似于您的stylePolygons的函数对样式进行个性化设置,在每个要素上进行迭代并设置其颜色作为feature.getProperty('color')

If you're looking to style the whole collection in one step , then you should do just 如果您希望一步一步地对整个系列进行样式设置 ,那么您应该做的只是

parkingLot.setStyle({'color':'red' });

but this is assuming parkingLot is an instance of google.maps.Data. 但这是假设parkingLot是google.maps.Data的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM