2015-05-11 18 views
6

voglio solo mostrare una traccia sulla mia mappa ho provato come segue ma il problema è che non voglio caricare il track point in layer da file GPX (perché io non si vuole generare il file da coordinate strega che ricevo da GPSdevice di programmazione)Come visualizzare una traccia su un livello con lat e long

esiste un modo per aggiungere un livello traccia dal lungo e lat

// Add the Layer with the GPX Track 
var lgpx = new OpenLayers.Layer.Vector("Car track", { 
    strategies: [new OpenLayers.Strategy.Fixed()], 
    protocol: new OpenLayers.Protocol.HTTP({ 
     url: "testTrack.GPX", 
     format: new OpenLayers.Format.GPX() 
    }), 
    style: { strokeColor: "green", strokeWidth: 5, strokeOpacity: 0.5 }, 
    projection: new OpenLayers.Projection("EPSG:4326") 
}); 
map.addLayer(lgpx); 

Ecco l'lat e lungo nel file GPX (formato xml)

<?xml version="1.0" encoding="UTF-8"?> 
    <gpx version="1.0"> 
    <name>Example gpx</name> 
    <trk><name>Example gpx</name><number>1</number> 
    <trkseg> 
     <trkpt lat="35.737097" lon="51.314965"></trkpt> 
     <trkpt lat="35.736953" lon="51.317454"></trkpt> 
     <trkpt lat="35.737572" lon="51.317551"></trkpt> 
     <trkpt lat="35.737755" lon="51.315716"></trkpt> 
     <trkpt lat="35.739588" lon="51.316070"></trkpt> 
    </trkseg> 
    </trk> 
</gpx> 
+0

Incolla un campione della struttura dati GPS che hai. –

+0

Hai taggato questo [tag: openlayers] che riguarda Openlayers 2.x che non supporta 'Format.GPX' - è una svista o la causa principale per cui non funziona? –

+0

Nella tua domanda hai scritto che non vuoi lo load da file GPX. Qual è il tuo metodo preferito? A proposito ho postato una risposta su come caricare GPX in ol3 nel caso qualcuno lo stia cercando. –

risposta

1

ho trovato la soluzione, qui è

 lineLayer = new OpenLayers.Layer.Vector("Line Layer"); 
     map.addLayer(lineLayer); 
     map.addControl(new OpenLayers.Control.DrawFeature(lineLayer, OpenLayers.Handler.Path)); 

    var coordinates = [ 
     { lat: "35.737097", lon: "51.314965" }, 
     { lat: "35.736953", lon: "51.317454" }, 
     { lat: "35.737572", lon: "51.317551" }, 
     { lat: "35.737755", lon: "51.315716" }, 
     { lat: "35.739588", lon: "51.316070" } 
    ]; 
function DrawTrack(){ 
     var points = coordinates.map(function (cor) { 
      return new OpenLayers.Geometry.Point(cor.lon, cor.lat) 
          .transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()); 
     }); 

     var style = { 
      strokeColor: '#0000ff', 
      strokeOpacity: 0.5, 
      strokeWidth: 5 
     }; 

     for (var i = 0; i < points.length - 1; i++) { 

      (function (i) { 

       window.setTimeout(function() { 
        var line = new OpenLayers.Geometry.LineString([points[i], points[i + 1]]); 
        var lineFeature = new OpenLayers.Feature.Vector(line, null, style); 
        lineLayer.addFeatures([lineFeature]); 

        map.setCenter(points[i].lon, points[i].lat); 

       }, i * 1000); 

      }(i)); 
     } 
} 
0

Esempio di caricamento dei dati GPX in OpenLayers 3.

var lgpx = new ol.layer.Vector({ 
    title: 'Car track', 
    source: new ol.source.Vector({ 
     url: 'testTrack.gpx', 
     format: new ol.format.GPX() 
    }), 
    style: new ol.style.Style({ 
     stroke: new ol.style.Stroke({ 
      color: 'green', 
      width: 5, 
      opacity: 0.5 
     }) 
    }) 
}); 

map.addLayer(lgpx); 

Lista delle disponibili formats.

+0

non voglio caricare un file gpx come questo infatti ho le coordinate in lat, long format voglio sapere c'è qualche metodo che ci vuole lat lat e tracciare una traccia sulla mappa in base al coordinate qualcosa che posso fare una chiamata ajax e recuperare le coordinate dal server quindi disegno la traccia –

+0

Potresti fornire un campione di dati restituito dal tuo server? –

+0

Puoi indicare 'url' al tuo servizio se è in formato GPX. –

0

La tua risposta è stata chiusa per essere perfetto.

È necessario analizzare il numero di stringa per il float.

lineLayer = new OpenLayers.Layer.Vector("Line Layer"); 
map.addLayer(lineLayer); 
map.addControl(new OpenLayers.Control.DrawFeature(lineLayer, 
    OpenLayers.Handler.Path)); 

var coordinates = [ 
    { lat: "35.737097", lon: "51.314965" }, 
    { lat: "35.736953", lon: "51.317454" }, 
    { lat: "35.737572", lon: "51.317551" }, 
    { lat: "35.737755", lon: "51.315716" }, 
    { lat: "35.739588", lon: "51.316070" } 
]; 
function DrawTrack(){ 
    var points = coordinates.map(function (cor) { 
    return new OpenLayers.Geometry.Point(parseFloat(cor.lon),parseFloat(cor.lat)) 
        .transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()); 
    }); 

    var style = { 
     strokeColor: '#0000ff', 
     strokeOpacity: 0.5, 
     strokeWidth: 5 
    }; 

    for (var i = 0; i < points.length - 1; i++) { 

     (function (i) { 

     window.setTimeout(function() { 
      var line = new OpenLayers.Geometry.LineString([points[i], points[i + 1]]); 
      var lineFeature = new OpenLayers.Feature.Vector(line, null, style); 
      lineLayer.addFeatures([lineFeature]); 

      map.setCenter(points[i].lon, points[i].lat); 

     }, i * 1000); 

     }(i)); 
    } 
} 
Problemi correlati