Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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();
            data['3401']=663;
            data['3402']=1794;
            data['3403']=472;
            data['3404']=1902;
            data['3471']=11986;

            var wilayah= new Array();
            wilayah['3401']="kulon progo";
            wilayah['3402']="bantul";
            wilayah['3403']="gunung kidul";
            wilayah['3404']="sleman";
            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;
                while(i<batas.length-1){
                    if (val<batas[i+1]){
                        color = warna[i];
                        break;
                    }
                    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});

            jogja.events.on({'loadend': onLoadEnd})

            map.addLayer(jogja);
            map.zoomToMaxExtent();

        function onLoadEnd(){var ext = jogja.getDataExtent();
                             map.zoomToExtent(ext,false);}


    </script>

<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
1  
can u add your code to jsfiddle.net? –  Aragon Jun 29 '12 at 8:29
1  
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
add comment

4 Answers

I think you should access IDKAB like that :

feature.attributes["IDKAB"]
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
add comment

you could try to get the IDKAB-values via
feature.attributes.IDKAB.value

share|improve this answer
add comment

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.

http://openlayers.org/dev/examples/sundials.html

view-source:http://openlayers.org/dev/examples/sundials.html

best regards, Joaquim Muchaxo.

share|improve this answer
add comment

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
add comment

Your Answer

 
discard

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.