2013-01-24 10 views
6

ha avuto un problema. Voglio associare le informazioni aggiuntive da geojson a un popup di un volantino. ho cercato alcune informazioni dalla documentazione del volantino ma non funziona.Popup Il volantino con informazioni aggiuntive da GeoJSON

var map = L.map('map').setView([51.9, 7.6], 11); 
         L.tileLayer('http://{s}.tile.cloudmade.com/5e4495ff4b0d454eb0443225198b7e6c/997/256/{z}/{x}/{y}.png', { 
    attribution: 
     'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>', 
     maxZoom: 16 
    }).addTo(map); 

var polygon = { 
    "type": "Feature", 
    "properties": { 
     "name":"City BoundingBox", 
     "style": { 
      "color": "#004070", 
      "weight": 4, 
      "opacity": 1 
     } 
    }, 
    "geometry": { 
     "type": "Polygon", 
     "coordinates": [[ 
      [7.5,52.05],     
      [7.7,51.92], 
      [7.6,51.84], 
      [7.4,51.94], 
      [7.5,52.05] 
     ]] 
    } 
}; 

var myLayer = L.geoJson().addTo(map); 
//myLayer.addData(polygon); 

var popup = L.popup(); 

function onMapClick(e) { 
    popup 
     .setLatLng(e.latlng) 
     .setContent("You clicked the map at " + e.latlng.toString()) 
     .openOn(map); 
} 


map.on('click', onMapClick); 

<?php 
    $mdjson = file_get_contents("http://xxx/ows?service=WFS&version=1.0.0&outputFormat=JSON&request=GetFeature&typeName=xx:yy&maxFeatures=50"); 
    echo "var geojsonMD = ".$mdjson.";";  
?> 

myLayer.addData(geojsonMD); 

L.geoJson(geojsonMD, { 
    style: function (feature) { 
     return {color: feature.properties.color}; 
    }, 
    onEachFeature: function (feature, myLayer) { 
     layer.bindPopup(feature.properties.description); 
    } 
}).addTo(map); 

Spero che tu possa aiutarmi.

Cordiali saluti.

+0

si può fare un po 'più chiaro ciò che si vuole fare? Vuoi unire il geoJson scaricato dal servizio con il poligono creato da te? – flup

risposta

12

Supponendo che il servizio restituisca dati con proprietà simili al poligono, è possibile aggiungerli ad uno stesso livello.

var myLayer = L.geoJson(geojsonMD, { 
    style: function (feature) { 
     return feature.properties.style; 
    }, 
    onEachFeature: function (feature, layer) { 
     layer.bindPopup(feature.properties.name); 
    } 
}) 

myLayer.addData(polygon); 
myLayer.addTo(map); 

http://jsfiddle.net/Wn5Kh/ (senza i dati scaricati, perché io non ho l'URL)

Se il geojsonMD ha diverse proprietà caratteristica, non c'è niente di sbagliato con l'aggiunta di due strati GeoJSON. Uno per i dati che si recuperano dal servizio e uno con il poligono.

1

Come spiegato nella documentazione Scheda tecnica, si dovrebbe usare il "onEachFeature" per allegare un popup con le informazioni desiderate per ogni caratteristica del vostro GeoJSON:

L'opzione onEachFeature è una funzione che viene chiamato su ogni prima di aggiungerlo a un livello GeoJSON. Un motivo comune per usare questa opzione è di allegare un popup per funzioni quando si fa clic

Si può usare in questo modo:

var myLayer = L.geoJson(polygon, { 
    onEachFeature: yourOnEachFeatureFunction 
}).addTo(map); 

function yourOnEachFeatureFunction(feature, layer){ 
    if (feature.properties.name) { 
     layer.bindPopup(feature.properties.name); 
    } 
} 

In questo esempio la comparsa mostrerà il contenuto del proprietà "nome" per ogni caratteristica cliccata

+0

Ma non è quello che vuole. Vuole aggiungere dinamicamente alcuni dati extra a JSON. Credo. – flup

1

Ora funziona. Volevo che il volantino ottenga automaticamente le coordinate e le informazioni sulle funzioni da un wfs e le aggiunga alla mappa.

questo è il codice di lavoro e grazie per il vostro aiuto =)

<?php 
        echo "var geojsonMD = ".$mdjson.";";  
?> 

myLayer.addData(geojsonMD); 

var myLayer = L.geoJson(geojsonMD, { 
style: function (feature) { 
    return feature.properties.style; 
}, 
onEachFeature: function (feature, layer) { 

     var strtype = ''; 

     if (feature.properties.mdtype == 0) { 
      strtype = 'aaa'; 
     } else if (feature.properties.mdtype == 1) { 
      strtype = 'bbb'; 
     } 


    layer.bindPopup('<b>' + feature.properties.mdname + '</b><br>' 
         + strtype + '<br><br>' 
         + feature.properties.mdadress + '<br>' 
         + feature.properties.mdfon); 
    } 
    }) 
     myLayer.addTo(map); 
Problemi correlati