2016-03-29 4 views
0

私はトランスポートアプリケーションを作成しています。位置を変更するとすぐにユーザーの現在の位置を保存しますMySQLデータベースに保存し、後で結果を取得してマップ上にマーカーをプロットします。 はここに現在の場所についてユーザーが位置を変更するとすぐに現在の場所をデータベースに保存します。インテルXDKを使用してGoogle Map APIとCordova

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!" 
            }); 
        } 


    }); 

答えて

0

の更新ごとに1ミリ秒

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

私のコードで今するonSuccessにnewLocationとoldLocationの値の周りに場所

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); 
      } 

    } 

スワップを取得し、 oldLocationが最初は現在の場所で、locationが変更されるたびにnewLocationの値をフラッシュし、locationをoldLocationとして設定します。 希望があれば幸いです。

+0

ありがとうございます –

関連する問題