IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Visualiser le résultat d'une requête PHP en map


Sujet :

JavaScript

  1. #1
    Membre confirmé
    Femme Profil pro
    étudiant master
    Inscrit en
    Février 2014
    Messages
    167
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Algérie

    Informations professionnelles :
    Activité : étudiant master

    Informations forums :
    Inscription : Février 2014
    Messages : 167
    Par défaut Visualiser le résultat d'une requête PHP en map
    Bonjour à tous,

    technologies utilisées : mapserver, bootstraps, openlayers3, javascripts, postgresql
    but: appliquer une requête sur une couche dans postgresql et afficher le résultat sur le map.
    la requête est faite par un code php.

    j'ai essayé avec le code ci-dessous mais ça ne s'affiche rien, aucune erreur:

    Nom : Capture.PNG
Affichages : 267
Taille : 29,1 Ko

    vraiment je me suis bloqué
    pouvez vous m'indiquer où existe-il le problème?

    merci d'avance

  2. #2
    Membre chevronné Avatar de 01001111
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2009
    Messages
    319
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2009
    Messages : 319
    Par défaut
    la variable fields est définie où?
    il semble que ton code est incomplet.

    As-tu vérifié que dataArray contient bien qqch après le split?
    Quel est le retour de console.log(dataArray); si tu le décommentes?

  3. #3
    Membre confirmé
    Femme Profil pro
    étudiant master
    Inscrit en
    Février 2014
    Messages
    167
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Algérie

    Informations professionnelles :
    Activité : étudiant master

    Informations forums :
    Inscription : Février 2014
    Messages : 167
    Par défaut
    Bonjour,

    1. le variable fields est défini au dessus (voir le code complet)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    		var  geojson = {
    		"type": "FeatureCollection",
    		"crs" : {
    		"type": "name",
    		"properties": {
            "name": 'EPSG:30731'
        }},
    		"features": []
    	};
    	var projection = new ol.proj.Projection({
      code: 'EPSG:30731',
      units: 'm'
    });
    var geo;
    var resolution = [800,500];
    var extent = [218470.985603305,3950936.00068151,297327.00560837,4023055.0106858];
    var layer=['mostac'];
          var layers = [
              new ol.layer.Image({
    		  title: 'a',
    		  type: 'base',
                extent: extent,
                source: new ol.source.ImageWMS({
                  url: 'http://localhost/cgi-bin/mapserv.exe?map=C:/ms4w/Apache/htdocs/54/bootstrap-3.3.6-dist/Mosta.map',
                  params: {
                    'LAYERS': layer,
                    'CRS': 'EPSG:30731',
                    'BBOX': '218470.985603305,3950936.00068151,297327.00560837,4023055.0106858',
                    'WIDTH': '800',
                    'HEIGHT': '500',
                    'FORMAT': 'image/png'
                  },
                  serverType: 'mapserver'
                })
              })
            ];
    		var layers2 = [
              new ol.layer.Image({
    		  title: 'b',
     
                extent: extent,
                source: new ol.source.ImageWMS({
                  url: 'http://localhost/cgi-bin/mapserv.exe?map=C:/ms4w/Apache/htdocs/54/bootstrap-3.3.6-dist/Mosta.map',
                  params: {
                    'LAYERS': ['parcellles'],
                    'CRS': 'EPSG:30731',
                    'BBOX': '218470.985603305,3950936.00068151,297327.00560837,4023055.0106858',
                    'WIDTH': '800',
                    'HEIGHT': '500',
                    'FORMAT': 'image/png'
                  },
     
                  serverType: 'mapserver'
                })
              })
            ];	
    var fill = new ol.style.Fill({
    color: 'rgba(0,0,0,0.2)'
    });
    var stroke = new ol.style.Stroke({
    color: 'rgba(0,0,0,0.4)'
    });
    var circle = new ol.style.Circle({
    radius: 6,
    fill: fill,
    stroke: stroke
    }); 
    var vectorStyle = new ol.style.Style({
    fill: fill,
    stroke: stroke,
    image: circle
    });
    	var vectorLoader = function(extent, resolution, projection) {
    	var url = 'http://localhost/cgi-bin/mapserv.exe?map=C:/ms4w/Apache/htdocs/54/bootstrap-3.3.6-dist/m.map?service=WFS&' +
    	'version=1.1.0&request=GetFeature&typename=com&' +
    	'outputFormat=text/javascript&format_options=callback:loadFeatures' + '&srsname=EPSG:30731&bbox=' + extent.join(',') + ',EPSG:30731';
    	$.ajax({
    	url: url,
    	dataType: 'jsonp'
    	});
    	};
    	var loadFeatures = function(response) {
     
    	var features = vectorSource.readFeatures(response);
    	vectorSource.addFeatures(features);
    	};
    	var vectorSource = new ol.source.Vector({
    	strategy: ol.loadingstrategy.tile(new ol.tilegrid.createXYZ({maxZoom: 19})),
    	loader: vectorLoader,
    	projection: 'EPSG:30731',
    	});
    	var serverVector = new ol.layer.Vector({
    	source: vectorSource,
    	style: vectorStyle
    	});
     
     
    		/*var map = new ol.Map({
              layers: layers,
              target: 'map',
              view: new ol.View({
                center: [259470,3989000],
                zoom: 10,
    			projection: projection
              })
            });*/
    		var gl1 =  new ol.layer.Group({
                    'title': 'Base maps',
                    layers: layers				
                });
    		var gl2 = new ol.layer.Group({
                    title: 'Overlays',
                    layers: layers2 	
     
    				});
    				var gl3 = new ol.layer.Group({
                    title: 'Ovserlays',
                    layers: vectorLayer 	
     
    				});
     
     
        var map = new ol.Map({
            target: 'map',
            layers: [
               gl1,
    		   gl2,
    		   gl3
     
            ],
            view: new ol.View({
                center: [259470,3989000],
                zoom: 10,
    			projection: projection
              })
        });
     
        var layerSwitcher = new ol.control.LayerSwitcher({
            tipLabel: 'Légende' // Optional label for button
        });
        map.addControl(layerSwitcher);
    	/*(function() {
    	window.alert(map.getView().getZoom());
       var zoom = map.getView().getZoom();
       if (zoom >=10) {
        window.alert(1);
          gl1.setVisible(false);
       }
       else if (zoom < 10) {
          gl1.setVisible(true);
       }
    })();*/
     
        map.on('moveend', (function() {
    	var ghostZoom = map.getView().getZoom();
            if (ghostZoom > 10 ) {
               gl2.setVisible(true);
                console.log('1');
            }else if (ghostZoom <= 10)
    	{
        gl2.setVisible(false);
    	}
     
        }));
     
     
    /*
    map.events.on({
        zoomend: function(evt) {
    zoom = map.getZoom();
      if (zoom >= 10)
      {
        gl1.setVisible(false);
      }
      else if (zoom < 10)
      {
        gl1.setVisible(true);
      }
     
        }       
    });
    */
    var fields = ["geom"];
    var data = {
    		table: "parcellles",
    		fields: fields
    	};
    $('#submit').submit(
    	$.ajax("get_data.php",{
    		data: {
    			email: "email",
    			pwd: "password"
    		},
    		success: function(data){
    			window.alert("");		
    		}
    	})
    );
     
    $('#submit').submit(
     
    	$.ajax("get_geom.php",{
            data: data,
    		success: function(data){
    				//create geojson container object
     
     
    	//split data into features
    	var dataArray = data.split(", ;");
    	dataArray.pop();
     
       //console.log(dataArray);
     
    	//build geojson features
    	dataArray.forEach(function(d){
    		d = d.split(", "); //split the data up into individual attribute values and the geometry
    		//feature object container
    		var feature = {
    			"type": "Feature",
    			"properties": {}, //properties object container
    			"geometry": JSON.parse(d[fields.length]) //parse geometry
    		};
     
    		for (var i=0; i<fields.length; i++){
    			feature.properties[fields[i]] = d[i];
    		};
    		geojson.features.push(feature);
    	});
    		}
    	}));
    	console.log(geojson);
    	var vectorSource = new ol.source.Vector({
    projection: 'EPSG:30731',
    url: geojson,
    format: new ol.format.GeoJSON()
    });
    var vectorLayer = new ol.layer.Vector({
    source: vectorSource
    });
    2. Oui, data array fonctionne bien
    3. voir le retour de console dans la pièce jointe.

    Nom : Capture.PNG
Affichages : 173
Taille : 9,6 Ko

    ça donne un résultat sur le console mais il n'affiche pas le vecteur sur map.

  4. #4
    Membre chevronné Avatar de 01001111
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2009
    Messages
    319
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2009
    Messages : 319
    Par défaut
    C'est assez dur à comprendre, mais comme ça, à première vue,
    je vois ce qui me semble être une erreur:
    L'objet geojson est construit dans la requête ajax, mais appelé en dehors de celle-ci, hors il s'agit d'une requête asynchrone, donc l'objet n'est pas prêt quand appelé.
    Essayez svp comme ceci:
    Code javascript : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
     
    $('#submit').submit(
    	$.ajax("get_geom.php",{
            	data: data,
    		success: function(data){
    			//create geojson container object
    			//split data into features
    			var dataArray = data.split(", ;");
    			dataArray.pop();
    			//console.log(dataArray);
    			//build geojson features
    			dataArray.forEach(function(d){
    				d = d.split(", "); //split the data up into individual attribute values and the geometry
    				//feature object container
    				var feature = {
    					"type": "Feature",
    					"properties": {}, //properties object container
    					"geometry": JSON.parse(d[fields.length]) //parse geometry
    				};
    				for (var i=0; i<fields.length; i++){
    					feature.properties[fields[i]] = d[i];
    				};
    				geojson.features.push(feature);
    			});
                            console.log(geojson);
    	                var vectorSource = new ol.source.Vector({
    		                   projection: 'EPSG:30731',
    		                   url: geojson,
    		                   format: new ol.format.GeoJSON()
    	                });
    	                var vectorLayer = new ol.layer.Vector({
    	                           source: vectorSource
                            });
    		}
    	})
    );

  5. #5
    Membre confirmé
    Femme Profil pro
    étudiant master
    Inscrit en
    Février 2014
    Messages
    167
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Algérie

    Informations professionnelles :
    Activité : étudiant master

    Informations forums :
    Inscription : Février 2014
    Messages : 167
    Par défaut
    je vous remercie beaucoup 01001111.
    le problème est résolu, il fallait définir le code de geojson et du vectorlayer sous une function ().
    et ça fonctionne bien.

    merci encore

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 18/09/2013, 07h48
  2. [MySQL] Résultat d'une requête php mysql
    Par selimdjaf dans le forum PHP & Base de données
    Réponses: 7
    Dernier message: 20/01/2013, 17h17
  3. Visualiser le résultat d'une requête
    Par thibs5288 dans le forum Access
    Réponses: 2
    Dernier message: 24/05/2011, 04h48
  4. [Conception] Résultat d'une requête PHP dans un tableau avec lien ?
    Par DjMaC dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 25/04/2006, 20h37
  5. Réponses: 2
    Dernier message: 02/12/2005, 21h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo