0

I'm new to JS and I'm trying to build a web app with PhoneGap that takes GPS coordinates from an Android device and uses them in some JS functions, but I'm having trouble. How should I pass 'lat' and 'lon' to use them in 'coords'

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

2 Answers 2

1

Change your onSuccess() and initMap(). Your onSuccess is setting lat and lon as its private variable. Also, as these are asynchronous calls, your initMap may not end up in getting the data you need. That is why you need to initMap() when your onSuccess() fires.

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

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

Once you've done these there's no need of calling onSuccess, onError & initMap that you've done in the end

Sign up to request clarification or add additional context in comments.

1 Comment

when i give value for lat and lon, and try to alert them in initMap(lat, lon) i get (NaN) ?
0

Here is what you need

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.