2016-03-29 13 views
0

kullanarak Google Harita API'sı ve Cordova Bir taşıma uygulaması oluşturuyorum, konumu değiştirdikten hemen sonra kullanıcının mevcut konumunu kaydetmek istiyorum MySQL veritabanına ve daha sonra sonucu getirip Harita üzerindeki işaretçiyi çizeceğim. burada geçerli konum içinKullanıcının konumu değiştirdiği anda güncel konumların kaydedilmesi, Intel XDK

var watchid; 
$(document).on("pagecreate", "#map-page", function() { 
        var defaultLatLng = new google.maps.LatLng(34.0983425, -118.3267434);  // Default to Hollywood, CA when no geolocation support 
        if (navigator.geolocation) { 
            function success(pos) { 
                // Location found, show map with these coordinates 
                drawMap(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude)); 
            } 
            function fail(error) { 
                drawMap(defaultLatLng);  // Failed to find location, show default map 
            } 
            // Find the users current position.  Cache the location for 5 minutes, timeout after 6 seconds 
         watchid = navigator.geolocation.watchPosition(success, fail, {maximumAge: 500000, enableHighAccuracy:true, timeout: 6000}); 
        } else { 
            drawMap(defaultLatLng);  // No geolocation support, show default map 
        } 
        function drawMap(latlng) { 
            var myOptions = { 
                zoom: 10, 
                center: latlng, 
                mapTypeId: google.maps.MapTypeId.ROADMAP 
            }; 
            var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions); 
            // Add an overlay to the map of current lat/lng 
            var marker = new google.maps.Marker({ 
                position: latlng, 
                map: map, 
                title: "Greetings!" 
            }); 
        } 


    }); 

cevap

0

Güncelleme her 1 ms saniyede

var options = {enableHighAccuracy: true,timeout: 5000,maximumAge: 0,desiredAccuracy: 0, frequency: 1 }; 
      watchID = navigator.geolocation.watchPosition(onSuccess, onError, options); 

kodum şimdi onSuccess içinde newLocation ve oldLocation değeri etrafında yer

function onSuccess (position) { 
    var newLocation = position.coords; 
    if(newLocation .latitude === oldLocation.latitude && oldLocation.longitude === crd.longitude){ 
    //donot save in DB 
    } else{ 
    //save in db and draw it on map 
    var myLatLng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude ); 
       drawmap(myLatLng); 
      } 

    } 

takas olsun, eski konumunuzun başlangıçta geçerli konum olduğu ve konumdaki her değişiklikten sonra yeni konumun değerini temizledikten sonra konumunuzu eski konum olarak ayarlayın. Umarım Yardım eder.

+0

Teşekkürler çok yardımcı oluyor –

İlgili konular