Script starte automatisk og data over i php

Har du spørsmål om HTML, CSS eller Javascript, post det her.

Script starte automatisk og data over i php

Innlegg leneMarie » 21.03.2012 8:45

Hei

Jeg skal lage en web løsning som skal rapportere inn posisjonen til brukeren med jevne mellomrom. Funnet følgende kode på nett som gjør det jeg leter etter.

Kode: Merk alt
var start_stop_btn, wpid=false, map, z, op, prev_lat, prev_long, min_speed=0, max_speed=0, min_altitude=0, max_altitude=0, distance_travelled=0, min_accuracy=150, date_pos_updated="", info_string="";

// This function just adds a leading "0" to time/date components which are <10

function format_time_component(time_component) {
   if(time_component<10)
      time_component="0"+time_component;
   else if(time_component.length<2)
      time_component=time_component+"0";
      
   return time_component;
}

// This is the function which is called each time the Geo location position is updated

function geo_success(position) {
   start_stop_btn.innerHTML="Stop"; // Set the label on the start/stop button to "Stop"
   
   info_string="";
   var d=new Date(); // Date object, used below for output messahe
   var h=d.getHours();
   var m=d.getMinutes();
   var s=d.getSeconds();
      
   var current_datetime=format_time_component(h)+":"+format_time_component(m)+":"+format_time_component(s);
      
   // Check that the accuracy of our Geo location is sufficient for our needs
   
   if(position.coords.accuracy<=min_accuracy) {
      
      // We don't want to action anything if our position hasn't changed - we need this because on IPhone Safari at least, we get repeated readings of the same location with
      // different accuracy which seems to count as a different reading - maybe it's just a very slightly different reading or maybe altitude, accuracy etc has changed
      
      if(prev_lat!=position.coords.latitude || prev_long!=position.coords.longitude) {
         if(position.coords.speed>max_speed)
            max_speed=position.coords.speed;
         else if(position.coords.speed<min_speed)
            min_speed=position.coords.speed;
            
         if(position.coords.altitude>max_altitude)
            max_altitude=position.coords.altitude;
         else if(position.coords.altitude<min_altitude)
            min_altitude=position.coords.altitude;
         
         
         prev_lat=position.coords.latitude;
         prev_long=position.coords.longitude;
         
         
         info_string="Current positon: lat="+position.coords.latitude+", long="+position.coords.longitude+" (accuracy "+Math.round(position.coords.accuracy, 1)+"m)<br />Speed: min="+(min_speed?min_speed:"Not recorded/0")+"m/s, max="+(max_speed?max_speed:"Not recorded/0")+"m/s<br />Altitude: min="+(min_altitude?min_altitude:"Not recorded/0")+"m, max="+(max_altitude?max_altitude:"Not recorded/0")+"m (accuracy "+Math.round(position.coords.altitudeAccuracy,1)+"m)<br />last reading taken at: "+current_datetime;
      }
   } else
      info_string="Accuracy not sufficient ("+Math.round(position.coords.accuracy, 1)+"m vs "+min_accuracy+"m) - last reading taken at: "+current_datetime;

   if(info_string)
      op.innerHTML=info_string;
}

// This function is called each time navigator.geolocation.watchPosition() generates an error (i.e. cannot get a Geo location reading)

function geo_error(error) {
   switch(error.code) {
      case error.TIMEOUT:
      op.innerHTML="Timeout!";
      break;
   };
}


function get_pos() {
   
   // Set up a watchPosition to constantly monitor the geo location provided by the browser - NOTE: !! forces a boolean response
   // We  use watchPosition rather than getPosition since it more easily allows (on IPhone at least) the browser/device to refine the geo location rather than simply taking the first available position
   // Full spec for navigator.geolocation can be foud here: http://dev.w3.org/geo/api/spec-source.html#geolocation_interface
   
   // First, check that the Browser is capable
   if(!!navigator.geolocation)
      wpid=navigator.geolocation.watchPosition(geo_success, geo_error, {enableHighAccuracy:true, maximumAge:30000, timeout:27000});
   else
      op.innerHTML="ERROR: Your Browser doesnt support the Geo Location API";
}


// Initialiser: This will find the output message div and set up the actions on the start/stop button
function init_geo() {
   op=document.getElementById("output"); // Note the op is defined in global space and is therefore globally available
   
   if(op) {
      start_stop_btn=document.getElementById("geo_start_stop");
      
      if(start_stop_btn) {
         
         start_stop_btn.onclick=function() {
         
            if(wpid) // If we already have a wpid which is the ID returned by navigator.geolocation.watchPosition()
            {
               start_stop_btn.innerHTML="Start";
               navigator.geolocation.clearWatch(wpid);
               wpid=false;
            }
            else // Else...We should only ever get here right at the start of the process
            {
               start_stop_btn.innerHTML="Aquiring Geo Location...";
               get_pos();
            }
         }
         
         
      } else
         op.innerHTML="ERROR: Couldn't find the start/stop button";
   }
}

// Initialise the whole system (above)
window.onload=init_geo;


Kode: Merk alt
<div class="output" id="output"></div>
<div class="geo_controls"><button id="geo_start_stop">Start</button></div>


Koden har jeg fra: http://www.thedotproduct.org/2010/04/ex ... hposition/

Dette scriptet starter ved å trykke på en start knapp og viser koordinatene til brukeren.
Jeg ønsker at scriptet skal kjøre i bakgrunnen uten å måtte starte det ved bruk av knapp og sende dataen til et php script som lagrer dette i databasen.

Problemet er da hvordan få dette til å starte når brukeren logger inn og hvordan sende resultatet til php slik at jeg får lagret det i db?

Takker for alle svar.
leneMarie
Nytt medlem
 
Innlegg: 1
Registrert: 21.03.2012 8:34

Gå til (X)HTML/CSS/JavaScript/XML m.m.

Hvem er i forumet

Brukere som leser i dette forumet: Ingen registrerte brukere og 3 gjester