2010-12-08 11 views
6

मुझे पता है कि मैं आसानी से उपयोगकर्ता को ओपनलेयर में एकाधिक सुविधाएं/ज्यामिति चुनने की अनुमति देता हूं लेकिन फिर मैं उपयोगकर्ता को सभी चयनित सुविधाओं को आसानी से खींच/स्थानांतरित करने में सक्षम बनाना चाहता हूं।एकाधिक चयनित सुविधाओं को खींचें/स्थानांतरित करें - ओपनलेयर

ModifyFeature नियंत्रण के साथ यह केवल एक ही समय में एक सुविधा को स्थानांतरित करता है ... क्या उस परत पर सभी चयनित सुविधाओं को स्थानांतरित करने के लिए आसानी से इस नियंत्रण (या जो भी काम करता है) को बढ़ाने का कोई तरीका है?

उत्तर

12

ठीक है, ModifyFeature नियंत्रण को छोड़ दें और चयनित सुविधाओं का ट्रैक रखने के लिए बस SelectFeature नियंत्रण में हुक करें और फिर चयनित बिंदुओं को एक ही समय में बदलने के लिए DragControl का उपयोग करें। नियंत्रण इन्स्टेन्शियशन की

उदाहरण:

var drag = new OpenLayers.Control.DragFeature(vectors, { 
    onStart: startDrag, 
    onDrag: doDrag, 
    onComplete: endDrag 
}); 
var select = new OpenLayers.Control.SelectFeature(vectors, { 
    box: true, 
    multiple: true, 
    onSelect: addSelected, 
    onUnselect: clearSelected 
}); 

घटना का उदाहरण से निपटने कार्य:

/* Keep track of the selected features */ 
function addSelected(feature) { 
    selectedFeatures.push(feature); 
} 

/* Clear the list of selected features */ 
function clearSelected(feature) { 
    selectedFeatures = []; 
} 

/* Feature starting to move */ 
function startDrag(feature, pixel) { 
    lastPixel = pixel; 
} 

/* Feature moving */ 
function doDrag(feature, pixel) { 
    for (f in selectedFeatures) { 
     if (feature != selectedFeatures[f]) { 
      var res = map.getResolution(); 
      selectedFeatures[f].geometry.move(res * (pixel.x - lastPixel.x), res * (lastPixel.y - pixel.y)); 
      vectors.drawFeature(selectedFeatures[f]); 
     } 
    } 
    lastPixel = pixel; 
} 

/* Featrue stopped moving */ 
function endDrag(feature, pixel) { 
    for (f in selectedFeatures) { 
     f.state = OpenLayers.State.UPDATE; 
    } 
} 
+0

इस के लिए धन्यवाद - यह मेरे लिए भी काम करता है, उम्मीद है कि एक बार सुविधाओं घसीटा गया है, वे अब चयन बॉक्स से" चयन "हैं, और फिर से स्थानांतरित नहीं किया जा सकता है। क्या आपको यह समस्या है? –

+0

स्पष्ट उदाहरण के लिए धन्यवाद, ड्रैगिंग और घटनाओं पर एक साधारण उदाहरण खोजने के लिए मुझे लगभग एक दिन लग गया। यह बहुत अच्छा है! मैं इसे कई विशेषताओं के लिए उपयोग नहीं कर रहा हूं लेकिन 1. – Marco

+0

के लिए इसे मिला, इसका इस्तेमाल किया और यह काम करता है :)। तो आप एक बड़ा धन्यवाद! – elrado

0

हम्म ...

मैं ऊपर कोड की कोशिश की, और नहीं कर सका यह चलेगा। दो मुद्दे: 1) प्रत्येक सुविधा को स्थानांतरित करने के लिए, आपको उस सुविधा की मूल स्थिति का उपयोग करने की आवश्यकता है, और ड्रैग कंट्रोल अपने आप से चारों ओर घूम रहा है (यानी फीचर-पैरामीटर को डूड्रैग) से "ड्रैग वेक्टर" जोड़ना होगा। 2) ड्रैगफैचर्स के कोड के आखिर में पिक्सेल = पिक्सेल को ड्रैग पर कॉल करने से पहले, लाइन कॉलिंग मूव() सुविधा को (0,0) पर ले जायेगी।

मेरे कोड इस तरह दिखता है:

var lastPixels; 
function startDrag(feature, pixel) { 
    // save hash with selected features start position 
    lastPixels = []; 
    for(var f=0; f<wfs.selectedFeatures.length; f++){ 
     lastPixels.push({ fid: layer.selectedFeatures[f].fid, 
          lastPixel: map.getPixelFromLonLat(layer.selectedFeatures[f].geometry.getBounds().getCenterLonLat()) 
         }); 
    } 
} 

function doDrag(feature, pixel) { 
    /* because DragFeatures own handler overwrites dragSelected.lastPixel with pixel before this is called, calculate drag vector from movement of "feature" */ 
    var g = 0; 
    while(lastPixels[g].fid != feature.fid){ g++; } 
    var lastPixel = lastPixels[g].lastPixel; 

    var currentCenter = map.getPixelFromLonLat(feature.geometry.getBounds().getCenterLonLat()); 
    var dragVector = { dx: currentCenter.x - lastPixel.x, dy: lastPixel.y - currentCenter.y }; 

    for(var f=0; f<layer.selectedFeatures.length; f++){ 
     if (feature != layer.selectedFeatures[f]) { 
      // get lastpixel of this feature 
      lastPixel = null; 
      var h = 0; 
      while(lastPixels[h].fid != layer.selectedFeatures[f].fid){ h++; } 
      lastPixel = lastPixels[h].lastPixel; 

      var newPixel = new OpenLayers.Pixel(lastPixel.x + dragVector.dx, lastPixel.y - dragVector.dy); 
      // move() moves polygon feature so that centre is at location given as parameter 
      layer.selectedFeatures[f].move(newPixel); 
     } 
    } 
} 
+1

स्टैक ओवरफ़्लो में आपका स्वागत है! यदि आपके पास कोई नया प्रश्न है, तो कृपया [पूछें प्रश्न] (http://stackoverflow.com/questions/ask) बटन पर क्लिक करके इसे पूछें। यदि आपके पास पर्याप्त प्रतिष्ठा है, [आप अपवॉट कर सकते हैं] (http://stackoverflow.com/privileges/vote-up) प्रश्न। वैकल्पिक रूप से, इसे पसंदीदा के रूप में "स्टार" करें और आपको किसी भी नए उत्तर की अधिसूचना दी जाएगी। – jjnguy

0

मैं एक ऐसी ही समस्या थी और DragFeature के moveFeature समारोह अधिभावी और अंदर this.lastPixel = पिक्सेल रख कर इसे हल पाश के लिए भीतर सभी सुविधाओं में चले जाने के लागू होने वाला मेरी परत वेक्टर। जब तक मैंने इसे नहीं हटाया .lastPixel = लूप के अंदर पिक्सेल, खींचने वाले को छोड़कर सभी सुविधाओं को पागलपन से विकृत कर दिया गया।

`OpenLayers.Control.DragFeature.prototype.moveFeature = function (pixel) { 

     var res = this.map.getResolution();   
     for (var i = 0; i < vector.features.length; i++) { 
      var feature = vector.features[i]; 
      feature .geometry.move(res * (pixel.x - this.lastPixel.x), 
       res * (this.lastPixel.y - pixel.y)); 
      this.layer.drawFeature(feature); 
      this.lastPixel = pixel; 
     } 
     this.onDrag(this.feature, pixel); 
    }; 

`

संबंधित मुद्दे