2016-06-12 18 views
1

JSの新機能で、AndroidデバイスからGPS座標を取得してJS関数で使用するPhoneGapでWebアプリケーションを構築しようとしていますが、問題がある。どのように私は合格しなければならない 'LAT' と 'COORDS'js関数の変数を使用して他の関数を使用する

document.addEventListener("deviceready", onDeviceReady, false); 
var lat; 
var lon; 

function onDeviceReady() { 
    navigator.geolocation.getCurrentPosition(onSuccess, onError); 
} 
// onSuccess Geolocation 
// 
function onSuccess(position) {  
    var lat = position.coords.latitude; 
    var lon = position.coords.longitude; 
} 

function onError(error) { 
    alert('code: ' + error.code + '\n' + 
     'message: ' + error.message + '\n'); 
} 

var map; 
function initMap() { 
    var coords = new google.maps.LatLng(lat,lon); 
    map = new google.maps.Map(document.getElementById('map_canvas'), { 
       center: coords, 
       zoom: 15 
      }); 
    var marker = new google.maps.Marker({ 
        position: coords, 
        map: map, 
        title: 'Вие сте тук!' 
       }); 
    } 
    onSuccess(); 
    onError(); 
    initMap(); 

答えて

1

変更あなたのするonSuccess()とinitMap()でそれらを使用する '経度'。あなたのonSuccessはlatとlonをプライベート変数として設定しています。また、これらは非同期呼び出しなので、initMapは必要なデータを得ることができません。そのため、onSuccess()が起動したときにinitMap()を実行する必要があります。

function onSuccess(position) {  
    var lat = position.coords.latitude; 
    var lon = position.coords.longitude; 
    initMap(lat, lon); 
} 

function initMap(lat, lon) { 
    //your implementation 
} 

あなたはここで終わり

+0

を必要とするものですlatとlonのために、initMap(lat、lon)でそれらを警告しようとすると、(NaN)が得られますか? – user2769031

0

でやったことをするonSuccess、onErrorを& initMapを呼び出す必要はありませんこれらを完了したら、私は価値を与えるときに

var app = { 
    // Application Constructor 
    initialize: function() { 
     this.bindEvents(); 
    }, 
    // Bind Event Listeners 
    // 
    // Bind any events that are required on startup. Common events are: 
    // 'load', 'deviceready', 'offline', and 'online'. 
    bindEvents: function() { 
     document.addEventListener('deviceready', this.onDeviceReady, false); 
    }, 
    // deviceready Event Handler 
    // 
    // The scope of 'this' is the event. In order to call the 'receivedEvent' 
    // function, we must explicitly call 'app.receivedEvent(...);' 
    onDeviceReady: function() { 
     // app.receivedEvent('deviceready'); 
     navigator.geolocation.getCurrentPosition(app.onSuccess, app.onError); 
    }, 

    onSuccess: function(position){ 
     var longitude = position.coords.longitude; 
     var latitude = position.coords.latitude; 
     var latLong = new google.maps.LatLng(latitude, longitude); 

     var mapOptions = { 
      center: latLong, 
      zoom: 13, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

     var map = new google.maps.Map(document.getElementById("map"), mapOptions); 

     var marker = new google.maps.Marker({ 
       position: latLong, 
       map: map, 
       title: 'my location' 
      }); 
    }, 

    onError: function(error){ 
     alert("the code is " + error.code + ". \n" + "message: " + error.message); 
    }, 
}; 

app.initialize(); 
関連する問題