facebook

Custom use of geocode help: CLOSED

  1. MobiOne Archive
  2.  > 
  3. Getting Help – General
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #340104 Reply

    benl
    Member

    I’ve been plagued by this issue for a few days now and I’m hoping someone here may be able to help. Right now I attempt to determine a users location before a form (login) is submitted using the code below.

    phoneui.preSubmitForm_m1_xapp = function(form) { 
            if(navigator.geolocation) {
               navigator.geolocation.getCurrentPosition(onPositionUpdate, onError,{timeout: 10000})
            }
            else {
                onError()
            }    
            //It is at this point I need to determine if no location was found either via the geo location or zip code form method return false, else return true
            true;
            
    }
                function onPositionUpdate(position)
                {
                    var lat = position.coords.latitude;
                    window.localStorage.setItem("currentlat",lat)
                    var long = position.coords.longitude;
                    window.localStorage.setItem("currentlong",long)
                }                  
            function onError(err) {
                var zipcode=prompt("We couldn't get your location, let's try something else.  Enter your zip code below.","Zip Code");
                var geocoder = new google.maps.Geocoder();
    
                geocoder.geocode( { 'address': zipcode}, function(results, status) {
    
                  if (status == google.maps.GeocoderStatus.OK) {
                       var latitude = results[0].geometry.location.lat();
                    var longitude = results[0].geometry.location.lng();
                    window.localStorage.setItem("currentlat",latitude);
                    window.localStorage.setItem("currentlong",longitude);
                    } 
            }); 
    }    

    I am having trouble returning back to the parent Mobione function whether or not their location was found either from the geo location method or the zip code dialog method. If neither function was a success I need to throw false in the primary function to stop the user from entering the app. Any help would be greatly appreciated!

    #340233 Reply

    benl
    Member

    Anyone have any ideas on this? Essentially I need a way to stop the form from submitting if I can’t get a user’s location. Cincy, wayne, octavio, etc, any help would be greatly appreciated.

    #340277 Reply

    Hi,

    Have you tried to return false if the zipcode search doesn’t return results in the onError function and use that value in your preSubmitForm_m1_xapp? something like this:

    phoneui.preSubmitForm_m1_xapp = function(form) {
    if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(onPositionUpdate, onError,{timeout: 10000})
    }
    else {
    if(!onError()) // if the onError function returns false
    return false;
    }

    geocoder.geocode( { ‘address’: zipcode}, function(results, status) {

    if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();
    window.localStorage.setItem(“currentlat”,latitude);
    window.localStorage.setItem(“currentlong”,longitude);
    }
    else{
    return false;
    }

    });

    #340316 Reply

    benl
    Member

    Octavio,

    Thanks for the reply! That solution worked for the second half of the primary if function and I moved a few things around and I was able to get it to work all the way through. (Of course it was something simple.)

    #340317 Reply

    Hi benl,

    I am glad to help. Closing thread since you have solved the problem.

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: Custom use of geocode help: CLOSED

You must be logged in to post in the forum log in