javascript - Get position coordinates for an ajax request -


currently working on weather web-app ios. i'm trying coordinates of user , put latitude , longitude in ajax request (on api wunderground) .

here's code (edit):

<script type="text/javascript">        $(document).ready(function() {         navigator.geolocation.getcurrentposition(getlocation, unknownlocation);           function getlocation(pos)          {            var lat = pos.coords.latitude;            var lon = pos.coords.longitude;              $.ajax({                      url : "http://api.wunderground.com/api/apiid/geolookup/conditions/q/"+ lat +","+ lon +".json",                      datatype : "jsonp",                       success : function(parsed_json) {                         var location = parsed_json['location']['city'];                         var temp_f = parsed_json['current_observation']['temp_f'];                         alert("current temperature in " + location + " is: " + temp_f);                      }                   });          }          function unknownlocation()          {            alert('could not find location');          }        });     </script> 

as can see "simply" have create 2 vars lat , lon , add them in request. tried lot of variants can't code working.

any idea of i'm doing wrong ?

thank !

your variable declarations in middle of .ajax call, , making javascript structure invalid. also, i'm not sure why you're using "function($)", jquery $ reserved , shouldn't used function parameter.

   <script type="text/javascript"> $(document).ready(function() {     navigator.geolocation.getcurrentposition(onpositionupdate); });   function onpositionupdate(position) {     var lat = position.coords.latitude;     var lon = position.coords.longitude;     $.ajax({         url : "http://api.wunderground.com/api/apiid/geolookup/conditions/q/"+lat+","+lon+".json",         datatype : "jsonp",         success : function(parsed_json) {             var location = parsed_json['location']['city'];             var temp_f = parsed_json['current_observation']['temp_f'];             alert("current temperature in " + location + " is: " + temp_f);         }     }); } </script> 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -