views:

37

answers:

1

I have the following code where the function codeaddress geocodes the text feild value and returns geocoded value , geocoded value is stored in variable example ,how will i return the variable v2 to the function call and post to asmx webservice.

<script type="text/javascript">
    $(document).ready(function() {
        $('#SubmitForm').submit(function() {


  var geocoder;
  var map;
   function codeAddress(state) {
    var address = document.getElementById("state").value;
    geocoder.geocode( { 'address': state}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var v2=results[0].geometry.location;
        alert(example);
        var marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
      return v2;
    });


            var businessname = ($("#businessname").val());
            var keyword = ($("#keyword").val());
            var description = ($("#textarea").val());
            var zipcode = ($("#zipcode").val());
            var streetno = ($("#streetno").val());
            var streetname = ($("#streetname").val());
            var state = $('#state :selected').text();
            var telephone = ($("#telephone").val());
            var email = ($("#email").val());
            var username = ($("#username").val());
            var password = ($("#pass").val());
            var repassword = ($("#pass1").val());


            //data: "{'businessname':" + businessname + "'keyword':" + keyword + "}",

            alert(state); 
           var v2=codeAddress(state);
            alert(example);
            var jsonobject = "{\"businessname\":\"" + businessname + "\",\"keyword\":\"" + keyword + "\",\"description\":\"" + description + "\",\"zipcode\":\"" + zipcode + "\",\"streetno\":\"" + streetno + "\",\"streetname\":\"" + streetname + "\",\"state\":\"" + state + "\",\"telephone\":\"" + telephone + "\",\"email\":\"" + email + "\",\"username\":\"" + username + "\",\"password\":\"" + password + "\",\"repassword\":\"" + repassword + "\"}";
            $.ajax({
                type: "POST",
                url: "/BlockSeek/jsonwebservice.asmx/SubmitList",
                data: jsonobject,
                contentType: "application/json; charset=utf-8",
                success: ajaxCallSucceed,
                dataType: "json",
                failure: ajaxCallFailed
            });
        });

        function ajaxCallFailed(error) {
            alert("error");
        }

        function ajaxCallSucceed(response) {
            if (response.d == true) {
                alert(" sucessfully saved to database");
            }
            else {
                alert("not saved to database");
            }
        }




    });
</script>
A: 

You call the codeAddress method with a callback. Inside codeAddress when you get value of v2, call the callback function passing it v2.

codeAddress(state, 
    function(v2) {
        var jsonobject = "{\"businessname\":\"" + businessname/*.. use v2 in buiding jsonobject..*/;
        $.ajax({
            type: "POST",
            url: "/BlockSeek/jsonwebservice.asmx/SubmitList",
            data: jsonobject,
            contentType: "application/json; charset=utf-8",
            success: ajaxCallSucceed,
            dataType: "json",
            failure: ajaxCallFailed
        });
    }
);

function codeAddress(state, callback) {
    var address = document.getElementById("state").value;
    geocoder.geocode(...);
    // ...
    var v2=results[0].geometry.location;
    callback(v2);
}
Jayesh
Thanks for the reply
mahesh
Thank you very much i made changes to code as you suggested its working fine your post helped me a lot,once again thank you for your answer
mahesh
sure. you can then accept this answer by clicking the tick sign.
Jayesh