简体   繁体   中英

New to OpenLayers, issue with zoom, attributes and advice with hit detection

I am new to client-side programming. Thus far I've been writing only asp and php based solutions. But now I need to retrieve data from json and plot on a map (I don't know how to do that yet, but this is later).

After days of searching, I think OpenLayers can give me what I need.

I have gone through the Examples on dev.openlayers site, (such as this one http://dev.openlayers.org/releases/OpenLayers-2.13.1/examples/vector-features-with-text.html ), and also searched (and found some) solutions on stackoverflow, but they don't offer solutions to my problems).

Please view what I've done so far: http://www.nusantech.com/bangkuujian/openlayer.html

The canvas.js is as follows:

// create some sample features
var Feature = OpenLayers.Feature.Vector;
var Geometry = OpenLayers.Geometry;
var features = [
    new Feature(new Geometry.Point(-220, -60),attributes = { name: "Mercury",align: "cm",xOffset:10,yOffset:50 }), 
    new Feature(new Geometry.Point(-70, 120),attributes = { name: "Venus" }),
    new Feature(new Geometry.Point(0, 0),attributes = { name: "Earth" }),
    new Feature(new Geometry.Point(160, -100),attributes = { name: "Mars",align: "cm",xOffset:10,yOffset:50 })];

// create rule based styles
var Rule = OpenLayers.Rule;
var Filter = OpenLayers.Filter;
var style = new OpenLayers.Style({
    pointRadius: 10,
    strokeWidth: 3,
    strokeOpacity: 0.7,
    strokeColor: "#ffdd77",
    fillColor: "#eecc66",
    fillOpacity: 1,
    label : "${name}",
    fontColor: "#f0f0f0",
    fontSize: "12px",
    fontFamily: "Calibri, monospace",
    labelAlign: "${align}",
    labelXOffset: "${xOffset}",
    labelYOffset: "${yOffset}",
    labelOutlineWidth : 1
    }, 
    {
    rules: [

        new Rule({
            elseFilter: true,
            symbolizer: {graphicName: "circle"}
            })
           ]
    });

var layer = new OpenLayers.Layer.Vector(null, {
                    styleMap: new OpenLayers.StyleMap({'default': style,
                                    select: {
                                        pointRadius: 14,
                                        strokeColor: "#e0e0e0",
                                        strokeWidth: 5
                                        }
                                  }),
                    isBaseLayer: true,
                    renderers: ["Canvas"]
    });

layer.addFeatures(features);

var map = new OpenLayers.Map({
    div: "map",
    layers: [layer],
    center: new OpenLayers.LonLat(50, 45),
    zoom: 0
});

var select = new OpenLayers.Control.SelectFeature(layer);
map.addControl(select);
select.activate();

What I have problems with:

  1. Label offset In the samples, the labels should offset from the centre by labelXOffset: "(xvalue)", labelYOffset: "(yvalue)", but this is not happening in my page. Is there something I forgot?

  2. Zoom-in When I click the + button on the map, all the features look like they are zoomed in, however, the sizes of the features stay the same. How do I enlarge the features (circles) too?

  3. Hit Detection i) When I click on a circle, it is selected as designed. However, is it possible when I select a circle, I also change the right side (now there is a red "text here") and fill it up with html? Can you show me an example how to change the red "text here" to the label-name of the selected circle with a different colour? ii) Secondly, after I select a circle, how do I add a label under all the other circles denoting the distance between each circle and the selected circle?

Thank you in advance, hopefully these questions are not too much.

I have another question about retrieving an array of coordinates from json to plot the circles, but I will do more research on that. If you can point me in the right direction with regards to this, it would be much appreciated too.

I know how to do them server-side asp or php, but client side is very new to me. However client-side can do all of this much-much faster and can reduce a lot of load.

Cheers, masCh

I think I have managed to most of it.

  1. Labels not offsetting

Not sure what I did, but I declared a WMS layer and made a few changes to offset and now it is offsetting correctly.

var wms = new OpenLayers.Layer.WMS("NASA Global Mosaic",
                                   "http://hendak.seribudaya.com/starmap.jpg",
                                   {
                                       layers: "modis,global_mosaic",
                                   }, {
                                       opacity: 0.5,
                                       singleTile: true
                                   });

  var context = {
              getSize: function(feature) {
                  return feature.attributes["jejari"] / map.getResolution() * .703125;
              }
          };
  var template = {
              pointRadius: "${getSize}", // using context.getSize(feature)
              label : "\n\n\n\n${name}\n${jarak}",
              labelAlign: "left",
              labelXOffset: "${xoff}",
              labelYOffset: "${yoff}",
              labelOutlineWidth : 0
      };
  var style = new OpenLayers.Style(template, {context: context});

And I declared xoff & yoff under new OpenLayers.Geometry.Point(x,y), { jejari:5, xoff: -10, yoff: -15 }

2) Zoom in on point features.

This was a weird problem. Anyway, I declared a radius called jejari as in the code above next to xoff and yoff. Then modified pointRadius from a static number to "${getSize}" And then added the getSize function to var template which retrieves the current radius. I think that was all I did for that. But the labels were running all over the place, I still haven't solved that.

3) Hit detection and changing another in html

This adds what happens to the once a point feature has been selected

  layer.addFeatures(features); layer.events.on({ "featureselected": function(e) { kemasMaklumat('maklumat', "<FONT FACE='Calibri' color='#f0f0f0' size=5><center>"+ e.feature.attributes.name+ "<p>This is displayed text when a feature has been selected"; maklumat.style.color="black"; layer.redraw(); } }); map.addLayers([layer]); 

And in the html the and the kemasMaklumat function is declared as

  <script type="text/javascript"> function kemasMaklumat(id,content) { var container = document.getElementById(id); container.innerHTML = content; } </script> <td valign="top"><div id="maklumat" style="border-radius:25px; background-color:#000000;box-shadow: 8px 8px 4px #686868;"> Write Something Here<P> </div></td> 

The second part of this question was changing the labels of all the UNselected features, ie modifying attributes of all features that weren't the selected one. To do this, I added a for loop through all the features and check if it has the same label as the feature that was selected, this was done under the layer.events.on "featureselected" as was done in the above part 1 of this question.

  layer.addFeatures(features); layer.events.on({ "featureselected": function(e) { kemasMaklumat('maklumat', "<FONT FACE='Calibri' color='#f0f0f0' size=5><center>"+ e.feature.attributes.name+ "<p>This is displayed text when a feature has been selected"; maklumat.style.color="black"; for (var i = 0, l = layer.features.length; i < l; i++) { var feature = layer.features[i]; if (feature.attributes.name!=e.feature.attributes.name) { feature.attributes.name="I was not selected"; }} layer.redraw(); } }); map.addLayers([layer]); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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