Nous utilisons des cookies pour vous garantir la meilleure expérience sur notre site. Si vous continuez à utiliser ce dernier, nous considèrerons que vous acceptez l'utilisation des cookies. J'ai compris ! ou En savoir plus !.
banniere

Le portail francophone de la géomatique


Toujours pas inscrit ? Mot de passe oublié ?
Nom d'utilisateur    Mot de passe              Toujours pas inscrit ?   Mot de passe oublié ?

Annonce

Printemps des cartes 2024

#1 Wed 26 March 2014 19:00

Code93Zero
Juste Inscrit !
Date d'inscription: 26 Mar 2014
Messages: 2

PHP, GeoJSON Aucun affichage ...

Bonjour,
j'essaie d'afficher des requêtes de plus court chemin sur une carte OSM avec l'API OpenLayers dans une application, alors j'utilise POSTGIS et PHP et GEOJSON mais quand j'ajoute la couche GeoJSON a ma carte rien ne s'affiche à l'écran.
voici les codes que j'utilise :

JavaScript :

Code:

function init()
{
    var epsg4326 = new OpenLayers.Projection("EPSG:4326");
    var map = new OpenLayers.Map('Map');
    var osmLayer = new OpenLayers.Layer.OSM();
    
    map.addControl(new OpenLayers.Control.LayerSwitcher());
    map.addControl(new OpenLayers.Control.Navigation());
    map.addControl(new OpenLayers.Control.OverviewMap());
    map.addControl(new OpenLayers.Control.MousePosition({displayProjection : epsg4326}));
    map.addLayer(osmLayer);

    var gsat = new OpenLayers.Layer.Google("Google Satellite",{type: google.maps.MapTypeId.SATELLITE, numZoomLevels: 22,sphericalMercator: true});
    map.addLayer(gsat);
    var vector = new OpenLayers.Layer.Vector("GeoJSON", {
    projection: "EPSG:4326",
    strategies: [new OpenLayers.Strategy.Fixed()],
    protocol: new OpenLayers.Protocol.HTTP({
        url: "../php/BDConnexion.php",
        format: new OpenLayers.Format.GeoJSON()
    })
},{
styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
strokeColor: "#ff9933",
strokeWidth: 3
}))
});
     map.addLayer(vector);

    map.setCenter(new OpenLayers.LonLat(3.1824,36.7142).transform(epsg4326, map.getProjectionObject()), 16);

          
    
}

PHP :

Code:

<?php
$BD = pg_connect("host=localhost port=5432 dbname=USTHB user=postgres password=bakayaro");
if (!$BD) {
  echo "An error occurred.\n";
  exit;
}
$sql = "SELECT gid , ST_ASGeoJSON(geom) as geojson, length FROM pgr_dijkstra('usthb_routing',286,363);";
$query = pg_query($BD,$sql);  

// Renvoit un chemin au format GeoJSON
$geojson = array("type" => "FeatureCollection",
"features" => array()
);

// Ajouter un tronçon au tableau GeoJSON
while($edge=pg_fetch_assoc($query)) 
{
$feature = array("type" => "Feature",
"geometry" => json_decode($edge["geojson"], true),
"crs" => array(
"type" => "EPSG",
"properties" => array("code" => "4326")
),
"properties" => array(
"gid" => $edge["gid"],
"length" => $edge["length"]
)
);
// Ajouter un tableau d"éléments au tableau de collection d"éléments
array_push($geojson["features"], $feature);
}
// Fermeture de la connexion
pg_close($BD);
// Renvoyer le résultat
header("Content-type : application/json",true);
echo json_encode($geojson);
?>

malgré que si je visualise le résultat de ma requête php seulement j’obtiens ça :

Code:

{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1836101,36.7167552],[3.1838019,36.7170158]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"365","length":"0.006728924"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1842234,36.7161175],[3.1837585,36.7166528],[3.1836101,36.7167552]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"851","length":"0.017995942"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1841053,36.7160531],[3.1842234,36.7161175]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"820","length":"0.0025463197"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1833033,36.7156281],[3.1841053,36.7160531]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"819","length":"0.017138954"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1818613,36.7148557],[3.1833033,36.7156281]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"818","length":"0.030917615"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1820634,36.7146064],[3.1818613,36.7148557]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"593","length":"0.006612024"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1818129,36.7144803],[3.1820634,36.7146064]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"608","length":"0.0052732434"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1818129,36.7144803],[3.18187,36.7141191],[3.1818795,36.7138022]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"653","length":"0.015146522"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1818795,36.7138022],[3.1819202,36.7135445]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"654","length":"0.0057767173"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1819202,36.7135445],[3.1819941,36.7132896]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"655","length":"0.0058198357"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1819941,36.7132896],[3.1819959,36.7130845]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"656","length":"0.0045613227"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1819959,36.7130845],[3.1819444,36.7129231],[3.1818787,36.7127733],[3.1819348,36.7125869]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"657","length":"0.011500573"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1819348,36.7125869],[3.1820331,36.7124055]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"658","length":"0.0043983636"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1820331,36.7124055],[3.1819588,36.7122857],[3.181884,36.7121297]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"659","length":"0.0066921017"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.181884,36.7121297],[3.1818649,36.71213]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"660","length":"0.0003407106"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1806534,36.7114643],[3.1818649,36.71213]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"583","length":"0.026185295"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1806534,36.7114643],[3.1805682,36.7114186]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"723","length":"0.0018275969"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1805682,36.7114186],[3.1806355,36.7113329]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"398","length":"0.0022520286"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1807192,36.711229],[3.1806355,36.7113329]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"397","length":"0.002750569"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1807572,36.7111844],[3.1807192,36.711229]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"396","length":"0.0012011473"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.1807572,36.7111844],[3.180147,36.7108595]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"103","length":"0.013059606"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[3.180147,36.7108595],[3.1803033,36.7106661]]},"crs":{"type":"EPSG","properties":{"code":"4326"}},"properties":{"gid":"421","length":"0.0051247478"}}]}

Hors ligne

 

#2 Wed 26 March 2014 20:00

Code93Zero
Juste Inscrit !
Date d'inscription: 26 Mar 2014
Messages: 2

Re: PHP, GeoJSON Aucun affichage ...

Résolu : Mauvais cheminement vers le fichier BDConnexion.php :p

Hors ligne

 

Pied de page des forums

Powered by FluxBB