Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to create thematic maps using openlayer with kml map. but when i run the code, color and label thematic maps of the polygon can't show up. instead writing out undefined. what's the cause? are attributes of the map can not be read?

here the code :

<script type="text/javascript" src="OpenLayers.js"></script>

        <script type="text/javascript">

            var map;
            var jogja;
            var data= new Array();

            var wilayah= new Array();
            wilayah['3401']="kulon progo";
            wilayah['3403']="gunung kidul";
            wilayah['3471']="kota yogyakarta";

            var batas = [0, 500, 1000, 2000];
            var warna = ["#EEFFFF","#6DD5FF","#19A3FF","#0048FF"];

        function init(){
            map = new OpenLayers.Map('map',{
            controls: [
                new OpenLayers.Control.Navigation(),
                new OpenLayers.Control.PanZoom(),
                new OpenLayers.Control.ScaleLine(),
                new OpenLayers.Control.MousePosition(),

            var context = {
                getColor: function(feature){
                var id = feature.attributes.IDKAB;
                var val=data[id];
                var color = "white";

                var i =0;
                    if (val<batas[i+1]){
                        color = warna[i];
                    if (val !=null && color == "white"){
                        color = warna[batas.length-1];
                    return color;
                getLabel: function (feature){
                    return wilayah[feature.attributes.IDKAB];

            var template = {
                strokeColor: "#808080",
                strokeWidth: 1,
                graphicZIndex: 1,
                fillColor: "${getColor}",
                label: "${getLabel}",
                labelAlign: "cm"


            var style = new OpenLayers.Style(template, {context: context});
            var myStyle = new OpenLayers.StyleMap(style);

            jogja = new OpenLayers.Layer.GML ("KML", "kml/cite-jogja.kml", {format: OpenLayers.Format.KML
                        , isBaseLayer: true, styleMap: myStyle});

  {'loadend': onLoadEnd})


        function onLoadEnd(){var ext = jogja.getDataExtent();


<div id="map"></div>

-->IDKAB is attribute codes in the map file

-->and its the result : enter image description here

-thanks for answering-

share|improve this question
can u add your code to – Aragon Jun 29 '12 at 8:29
Use firebug and add a breakpoint to var id = feature.attributes.IDKAB;. Check how your feature class really looks like and how you can access the attributes. – johanvdw Jun 29 '12 at 8:29

You need to use a context to allow you to write a function to assign a kml attribute to your features.

The problem is that the kml attributes are not simple attributes - they are actually objects. You cannot assign an object to a feature attributes.

See this earlier question:

openlayers attribute substitution failing on kml file

P.S. I was logged in incorrectly, so ignore my other (junk) username

share|improve this answer

I think you should access IDKAB like that :

share|improve this answer
it doesn't work, still show the same output... any other solution?? thanks – disna Jun 29 '12 at 8:24
@johanvdw sorry, I still newbie. I don't understand about your answer.. I've add breakpoint to var id = feature.attributes.IDKAB; but I don't know whats should I do then.. can you explain more?? – disna Jun 29 '12 at 8:55

you could try to get the IDKAB-values via

share|improve this answer

I haven't use Kml format for this, but I am extracting attributes with Layer.WFS.

I have searched the demos and I think you should use a Vector Layer instead and set the format to KML.


best regards, Joaquim Muchaxo.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.